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