Thread: reading a new line

  1. #1
    Registered User
    Join Date
    Apr 2003
    Posts
    88

    reading a new line

    Say I have read from a file.
    And at a certain point in the middle of the line, I need to skip to the next line. What is the best way to do this, without creating a bunch of excess buffer trash?

  2. #2
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    How do you read from the file? Some code...

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    I was afraid of that
    Code:
    FILE *readfile;
    
    int lexInit(char* filename)
    {
    	readfile = fopen(filename,"r");
    	if(readfile == 0)
    	{
    		printf("\nfile could not be opened\n\n");
    	}
    	else
    	{
    		printf("\n File opened\n");
    	}
    	return 0;
    }
    
    int readchar()
    {
    	char ch;
    	ch = fgetc(readfile);
    	return ch;
    }
    
    int SkipWhiteSpace()
    {
    	char ch;
    	ch = readchar();
    	while(isspace(ch) || ch == '/')
    	{
    		if(ch == '/')
    		{
    			ch = readchar();
    			if (ch == '/')
    			{
    				printf("skip line1\n");
    				SkiptoEndofLine();
    				printf("skip line2\n");
    			}
    			else
    			{
    				pushback('/');
    				pushback(ch);
    				break;
    			}
    		}
    		//ch = readchar();
    	}
    	pushback(ch);
    	return 0;
    }
    this is what I have so far but it doesn't work and doesn't seem wholly efficent
    Code:
    int SkiptoEndofLine()
    {
    	char *trash, line[100];
    	trash = NULL;
    	fgets(trash, sizeof(line), readfile);
    	return 0;
    }
    If I could just figure out a way to skip to the next line, everytime
    SkiptoTheEndofLine is called, I'd be gravy...
    Well not literally

  4. #4
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    Why don't you read a line at once form the file and process every char in that line until you need to skip to the next line? Wouldn't be simpler this way?

  5. #5
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    it would be simpler, but due to the nature of the program, I must process char by char
    There is simply no way around that

  6. #6
    Unregistered User
    Join Date
    Nov 2004
    Posts
    25
    I guess you want something like this:
    Code:
    int SkiptoEndofLine()
    {
      while (readchar()!='\n')
      {
        ;
      }
    }

  7. #7
    Registered User
    Join Date
    Apr 2003
    Posts
    88
    I appreciate all of your help!
    I can go to bed now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading a file line by line
    By Raskalnikov in forum C Programming
    Replies: 8
    Last Post: 03-18-2009, 11:44 PM
  2. Replies: 1
    Last Post: 05-20-2006, 11:17 PM
  3. Replies: 6
    Last Post: 04-28-2006, 12:06 PM
  4. Reading a user specificed line from a file
    By hslee16 in forum C++ Programming
    Replies: 3
    Last Post: 03-13-2004, 06:58 PM
  5. Greenhand want help!
    By leereg in forum C Programming
    Replies: 6
    Last Post: 01-29-2002, 06:04 AM