Thread: reading a file

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    569

    reading a file

    How do I check if input is just an empty line??

    Code:
    input = fopen("test.dat", "r");
    while (!feof(input)){
            	
            	if(fscanf(input, ....................
       	 }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The same way you check to see if it's an empty line in notepad. Look at it and see what's there.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Code:
    #include <stdio.h>
    
    int main( void )
    {
              char buffer[20] = { 0 };
              int a, charsread = 0;
    
              input = fopen("test.dat", "r");
              while (!feof(input))
              {
                       charsread = fscanf(input, "%20s", buffer );
                       for( a = 0; a < charsread; a++ ) //a++ is preferred because it looks better ;)
                       {
                                 if( buffer[a] == '\n' && buffer[a + 1] == '\n' )
                                 {
                                            printf("Empty Line found\n");
                                 }
                       }
              }
              fclose(input);
              return 0;
    }
    You should be able to figure out what it does, it just makes sure that there are two lines because if there is a character preceding the newline character than that line is not empty.
    Last edited by DeadPlanet; 05-06-2009 at 06:00 AM.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4

    Smile

    Quote Originally Posted by -EquinoX- View Post
    How do I check if input is just an empty line??

    Code:
    input = fopen("test.dat", "r");
    while (!feof(input)){
            	
            	if(fscanf(input, ....................
       	 }
    Hi!
    Try this:
    Code:
    char c;
    
    input = fopen("test.dat", "r");
    do
    {
      c = fgetc(input);
      if(c != ' ' && c!=EOF)
      {
          printf("File not only empty line \n");
          exit(0);
       }
    }while (c!=EOF);
    
    printf("File only empty line \n");

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It depends on your definition of blank. Like I said, look at it and decide if it's blank.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM