Thread: Read more than one line in a file

  1. #1
    Registered User
    Join Date
    Aug 2007
    Location
    Barbados
    Posts
    31

    Read more than one line in a file

    Is there a way to read from a text file, more than the first line?
    I tried fgets, but I don't know how to continue on the next line, fscanf screws up if it encounters a space ' '. I just need to find a way to get this done.
    can anyone help?
    Last edited by sea_4_ever; 08-26-2007 at 12:43 AM. Reason: missed a word

  2. #2
    Registered User
    Join Date
    Aug 2007
    Location
    Barbados
    Posts
    31
    Don't answer all at once!
    really.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    this isn't irc, have patience.

    why not just call fgets again?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Wow, 28 minutes, and you complain you didn't get an answer. Might I echo the same sentiments as robwhit and suggest some patience?

    Or how about something better. If you're going to complain that we haven't done your work for you in less than half an hour, how about you apply your energy and impatience to researching it youself?

    What's wrong with calling fgets() in a loop? Or as the condition?

    Code:
    char szBuffer[BUFSIZ];
    FILE *somefile;
    
    ...  /* Assume somefile has been initialized and is a valid, open file for reading. */
    
    while(fgets(szBuffer, sizeof(szBuffer), somefile))
    {
    	/* process data read into szBuffer */
    }
    If you want to quit when you read a certain line, break when you encounter it.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    If you want to read the whole file, just use fread():
    Code:
    #include <stdio.h>
    #include <malloc.h>
    
    int main()
    {
    	long length;
    	char* buf;
    	size_t bytes;
    	FILE* file = fopen( "c:/file.txt", "rb" );
    
    	if ( file == NULL )
    	{
    		printf( "Error opening file!" );
    		return 1;
    	}
    
    	if ( fseek( file, 0, SEEK_END ) != 0 )
    	{
    		fclose( file );
    		printf( "fseek() failed!" );
    		return 2;
    	}
    
    	if ( (length = ftell( file )) < 0 )
    	{
    		fclose( file );
    		printf( "ftell() failed!" );
    		return 3;
    	}
    
    	if ( fseek( file, 0, SEEK_SET ) != 0 )
    	{
    		fclose( file );
    		printf( "fseek() failed!" );
    		return 2;
    	}
    
    	if ( (buf = malloc( length + 1 )) == NULL )
    	{
    		fclose( file );
    		printf( "malloc() failed!" );
    		return 4;
    	}
    
    	if ( (bytes = fread( buf, 1, length, file )) != (size_t)length )
    	{
    		free( buf );
    		fclose( file );
    		printf( "fread() didn't read &#37;d bytes!", length );
    		return 5;
    	}
    
    	fclose( file );
    	buf[length] = '\0';
    	printf( "Text file contents:\n%s", buf );
    	free( buf );
    
    	return 0;
    }
    Last edited by cpjust; 08-26-2007 at 03:17 PM. Reason: Oops, forgot to free( buf )...

  6. #6
    Registered User
    Join Date
    Aug 2007
    Location
    Barbados
    Posts
    31
    Alright, I have a something working, thanks for all the help.
    Now, I JUST figured out the "get input without pressing enter" thing, and have a good idea of how it works, now to practice it, I need to make some kind of game using the directional keys to navigate.
    Any ideas on an easy game to make?
    I was thinking some kind of Maze, or tic-tac-toe.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Code:
    #include <malloc.h>
    use stdlib.h instead. malloc.h is not standard.
    Code:
        FILE* file = fopen( "c:\\file.txt", "rb" );
    Quote Originally Posted by sea_4_ever View Post
    I need to make some kind of game using the directional keys to navigate.
    http://faq.cprogramming.com/cgi-bin/...&id=1043284392

  8. #8
    Registered User
    Join Date
    Aug 2007
    Location
    Barbados
    Posts
    31
    robwhit, I already know how.
    I just need to think of what game to make.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OPen a file and read it from the last line
    By c_geek in forum C Programming
    Replies: 14
    Last Post: 01-26-2008, 06:20 AM
  2. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  3. getline function to read 1 line from a text file
    By kes103 in forum C++ Programming
    Replies: 3
    Last Post: 10-21-2004, 06:21 PM
  4. how can i read i line from a file
    By condorx in forum C Programming
    Replies: 2
    Last Post: 05-07-2003, 02:47 PM
  5. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM