Thread: read amount of columns per line

  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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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,732
    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
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    if (word = 'i');
    are you sure?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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 01:36 AM.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    O! And the ; at the end
    it expands in
    Code:
    word = 'i';
    if (word)
    { 
       ;
    }
    {
       word = 'o';
       col++;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

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, 08:47 AM