read amount of columns per line

This is a discussion on read amount of columns per line within the C Programming forums, part of the General Programming Boards category; I need to read the total amount of columns per line from a file. I have absolutely no clue what ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    13

    read amount of columns per line

    I need to read the total amount of columns per line from a file. I have absolutely no clue what to put in the loop. Any help is welcome. Thanks.

    Code:
    void numofcol (int input)
    {
    	FILE* spData;
    	FILE* spOut;
    	
    	float line;
    
    	spData = fopen ("lab7a.txt", "r");
    	spOut = fopen ("output.txt", "a");
    
    	while ( (fscanf (spData, "%f", &line)) == EOF)
    	{
    				
    
    	}
    	
    	return;
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    You can help us help you:

    What is a column? In vim and some other editors, a column is one character wide. If that is satifactory, read a line with fgetc and increment a counter.

    It's pretty extensible to anything else a column could be: just define how wide it is, read that much, and do some arithmetic.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    sorry i wasn't more clear, a column in this case contains floating numbers, separated by about 6 spaces. Thanks for quick reply.

    edit: also, each line has a different amount of columns.

  4. #4
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    So read a line using a fgets function and the parse though the line to get different floating point values in that line, which basically means u are getting the values of each line.


    look through the fgets function and sscanf function now

    ssharish2005

  5. #5
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Ok, been working on this for a few days now... Just to find out that I can't use string functions, only basic read/write from files. Please dont send me running around in circles and give me some examples or something...anything! Thx.

  6. #6
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    Since when are fgets and sscanf string functions? I hate to break your balls, but fgets and sscanf are "basic read/write from files"-type functions.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Since when are fgets and sscanf string functions?
    I don't think it is his own decisn...
    I think the tutor wants them to fullfil the assignment using basic i/o functions like fscanf

    Please dont send me running around in circles
    We don't provide the final answers - only the directions...

    try something like
    Code:
    int insideColumn = 0;
    int c;
    int counter = 0 ;
    for(;;)
    {
       read character
       if(end of file) 
          exit loop
       if(end of line)
          exit loop
       if(space)
       {
          if(insideColumn)
             insideColumn = 0;
       }
       else
       {
          if(!insideColumn)
          {
             insideColumn = 1;
             counter ++;
          }
       }
    }
    print counter
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    *sigh*... now i'm hella lost.
    fgets and sscanf are in the chapter labled "string" in my book. Not only that, that is 4 chapters away from what we are studying right now. The functions we are studying right now are fscanf, fgetc, fprintf, and fputc. I'm pretty sure those are the only functions I can use in my program. Thx for your reply.

  9. #9
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Thx Vart. I'm not asking for final answers, just something to base on. I want to learn this stuff
    I'll give it a try.

  10. #10
    Registered User
    Join Date
    Nov 2006
    Posts
    13
    Ok i got it to work, but not quite correct. It reads every character instead of every floating number. It also doesn't read the last line.

    Code:
    int columns (int input)
    {
    	FILE* spData;
    	FILE* spOut;
    	
    	int cur, closeOut;
    	int col = 0;
    	int line = 0;
    	char word = 'o';
    
    	if ((spData = fopen ("lab7a.txt", "r")) == NULL)
    	{
    		printf (ERR2);
    		return 1;
    	}
    	if ((spOut = fopen ("output.txt", "a")) == NULL)
    	{
    		printf (ERR3);
    		return 1;
    	}
    
    	while ( (cur = fgetc(spData)) != EOF) 
    	{
    		if (cur == '\t' || cur == ' ')
    		{
    			word = 'o';
    		}
    		else if (cur == '\n')
    		{
    			word = 'o';
    			line++;	
    			printf ("line %d has %d column\n", line, col);
    			col = 0;
    		}
    		else 
    		{
    			if (word = 'i');
    			{
    				word = 'o';
    				col++;
    			}
    		}	
    				
    	}
    		
    		
    //	fprintf (spOut, "line %d has %d column\n", line, col);
    
    	closeOut = fclose (spOut);
    	fclose (spData);
    
    	return 0;
    }

  11. #11
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    if (word = 'i');
    are you sure?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  12. #12
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    I'm pretty sure that regardless of what state your finite state machine is in, andone, word will always be 'o'. Isn't that bad for your parsing?

    > if ( word = 'i' ) ;
    and this is an assignment which subsequently does nothing, like vart said. Turn up your compiler warnings.
    Last edited by whiteflags; 12-01-2006 at 12:36 AM.

  13. #13
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    O! And the ; at the end
    it expands in
    Code:
    word = 'i';
    if (word)
    { 
       ;
    }
    {
       word = 'o';
       col++;
    }
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 06-17-2008, 11:38 AM
  2. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  3. Read each line of text file.
    By Mithoric in forum C++ Programming
    Replies: 1
    Last Post: 06-25-2003, 11:53 AM
  4. how read line by line from textbox
    By lostai in forum Windows Programming
    Replies: 1
    Last Post: 02-14-2003, 07:47 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21