Thread: File I/O

  1. #1
    Registered User
    Join Date
    Jan 2010
    Posts
    69

    File I/O

    Hi, I am new here and this is my first time posting. I am a usually a java student but i have to take this C class.
    What I want my program to do is open the text file that is in the same directory and read in two floats ( using scanf ), then use fgets to read the name as everything until end of line. Then go to the next line and do the same thing until end of file. My program needs to read from stdin and terminate at end of file.

    here is the code
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    	char buf[80];
    	FILE * fp;
    	char * ptr;
    	float x, y;
    
    	do
    	{
    		fp = fopen ( "file.txt", "rw+" );
    		fgets ( buf, 80, fp );
    		if ( ( ptr = strchr ( buf, '\n' ) ) )
    		{
    			*ptr = 0;
    		}
    		fprintf( stdout, "%s\n", buf );
    	}
    	while ( 2 == scanf("%f %f", &x, &y) && fgets(buf, sizeof(buf), stdin) != NULL);
    	fclose ( fp );		
    	return 0;
    }
    this is the text file
    Code:
    38.5 12.75 Bob, Joe
    17.5 9.25 Bob, Jolene
    20.0 9.65 Smith, Janet 
    25.0 8.25 Public, John Q.
    40.0 8.75 Doe, Jane 
    15.0 6.25 Ling, Grunt

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while( fgets( ... ) != EOF )
    {
        if( sscanf( buf, "%f %f %[^\n]", &f1, &f2, buf2 ) != 3 )
            ...error...
        else
            ...winnar...
    }
    Make life easy on yourself. Read the whole line, parse it. You could also just do something like read the whole line, strtok off the first two words and convert them to numbers, and then just use the remainder of the line as your last string. There are many ways to do this.


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

  3. #3
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    this is what i have now but i still can't get it to go to the next line in the text file it just keeps repeating the first line.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main()
    {
    	char buf[80];
    	FILE * fp;
    	char * ptr;
    	float x, y;
    
    	while( fgets( buf, sizeof(buf), stdin ) != NULL )
    	{
    		fp = fopen ( "file.txt", "rw+" );
    		fgets ( buf, sizeof(buf), fp );
    		if( sscanf( buf, "%f %f %[^\n]", &x, &y, buf ) != 3 )
    	       {	
    			printf("error");
    		}
    		else
    		{
            		if ( ( ptr = strchr ( buf, '\n' ) ) )
    			{
    				*ptr = 0;
    			}
    			fprintf( stdout, "%s %f %f\n", buf, x, y );
    		}	
    	}	
    
    	fclose ( fp );		
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    First, I havent really read this thread, just your last post.
    ...it just keeps repeating the first line.
    Code:
    while( fgets( buf, sizeof(buf), stdin ) != NULL )
    	{
    		fp = fopen ( "file.txt", "rw+" );
    		fgets ( buf, sizeof(buf), fp );
    		// ...
    	}
    Each iteration of the "while" loop you re-open the file, and read whatever-many bytes. You probably want to only open the file once, just before the loop.

  5. #5
    Registered User
    Join Date
    Jan 2010
    Posts
    69
    its always the little things thats get me. its works perfect now Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  3. 2 questions surrounding an I/O file
    By Guti14 in forum C Programming
    Replies: 2
    Last Post: 08-30-2004, 11:21 PM
  4. File I/O problems!!! Help!!!
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 05-17-2002, 08:09 PM
  5. advice on file i/o
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 11-29-2001, 05:56 AM