fscanf copies a line from file twice in C - c

while(1<2) {
nscan=fscanf(infile, "%s %s %d%c",temp.name, temp.surname,&temp.code,&termch);
if(nscan==EOF) break;
if(nscan!=4 || termch!='\n')
printf("Error\n");
RecBSTInsert(&a,temp);
}
for some reason nscan //if(nscan==EOF) break; does not get executed when it is supposed to and it runs one more time giving the binary tree one more value, which is same with the last one.

fscanf:
Upon successful completion, these functions shall return the number of
successfully matched and assigned input items.
fscanf does not return the input provided. That is why you are not seeing EOF like you are testing for.
See: manpage for *scanf functions.
To answer your other question, try looping through like this:
while (fscanf(infile, "%s %s %d%c, temp.name, temp.surname, &temp.code, &termch) == 4)
{
//...
}
Edit again: I threw together a small program to simulate what I think you are doing. Here is my implementation, which takes from a file "testfile.txt" which looks like:
Bill Person 1
Bob Dog 2
Andrew Cat 3
With only one newline between each line. It matches the pattern %s %s %d%c (where \n is the %c).
Program to use this file:
#include <stdio.h>
#include <stdlib.h>
struct test {
char name[64];
char surname[64];
int code;
};
int main()
{
struct test temp;
char endchar;
FILE *infile = fopen("./testfile.txt", "r+"); // works with "r" as well, just a habit
if (infile == NULL)
return -1;
while (fscanf(infile, "%s %s %d%c", temp.name, temp.surname,
&temp.code, &endchar) == 4)
{
printf("Got one!\n");
}
return 0;
}
Of course, the printf exists in place of whatever logic you want to do on the current "temp" input.

Related

Read struct array in files and printf specific line on certain condition [c language]

I have learned about File processing in C programming recently. And I was given homework which
call me to read data on a .txt files and printf out the data
The problem I'm facing with is my output appear random alien word*(smth like this ╝ c 0.00
6?φ↨ê■` 0.00)* when i enter my selection.BUT I think I code it properly (fopen and fclose the files, read the files with fread) and I just don't get it why my programm come into an error. I spend almost 3 days on youtube and google everything but I still failed on it and it almost reach the due date.
can someone please help me? Rlly thank you.
also if you're free, please show me a correct code of this program so that I could make it as a reference. If you're not free its okay then :D
//Is my system flow correct , if i wanna read the files, and printf specific line from the files at certain condition. ( e.g. defining struct > open > if-else statement > do -while loop >end) ? or we have other flowchart which is more smooth
//is it possible that i read all lines of the files, but I only printf out one single specific line?if yes, how can we do this?
Here is the question
car.txt file shows variety of car maker, model, color and price. Design a program that read car maker, model, color and price from car.txt. List down the price options of for user to select from. The program will be able to display to the screen of particular car maker, model, color and price based on price range selection.
below is the .txt file
Toyota Altis Silver 120000.00
Toyota Vios Black 90000.00
Honda Accord Black 152000.00
Honda Civic Silver 118000.00
Nissan Cefiro Black 151000.00
Nissan Sylphy Silver 121000.00
Proton Perdana Black 110000.00
Proton Waja Blue 70000.00
//this was my code//
#include <stdio.h>
struct CarType
{
char carmaker[10],model[10],colour[10];
float price;
};
int main ()
{
char option;
FILE *fp;
struct CarType car[8];
if((fp = fopen("carM.txt", "r")) == NULL)
printf("file cant be open");
else
{
while (!feof(fp))
{
fread(&car[8], sizeof(struct CarType), 8, fp);
printf("\nChoose your option");
printf("\n1-List car price equal or above RM120,000");
printf("\n2-List car price RM120,000-RM149,999");
printf("\n3-List car price RM50,000-RM119,999");
printf("\n4-End program");
printf("\n>> ");
do
{
scanf("%c",&option);
if (option == '1')
{
printf("\nCar price equal or above RM120,000");
printf("\n %s %s %s %.2f",car[0].carmaker, car[0].model, car[0].colour, car[0].price);
printf("\n %s %s %s %.2f",car[2].carmaker, car[2].model, car[2].colour, car[2].price);
printf("\n %s %s %s %.2f",car[4].carmaker, car[4].model, car[4].colour, car[4].price);
printf("\n %s %s %s %.2f",car[5].carmaker, car[5].model, car[5].colour, car[5].price);
break;
}
if (option == '2')
{
printf("\nCar price RM120,000-RM149,999");
printf("\n %s %s %s %.2f",car[0].carmaker, car[0].model, car[0].colour, car[0].price);
printf("\n %s %s %s %.2f",car[5].carmaker, car[5].model, car[5].colour, car[5].price);
break;
}
if (option == '3')
{
printf("\nCar price RM50,000-RM119,999");
printf("\n %s %s %s %.2f",car[1].carmaker, car[1].model, car[1].colour, car[1].price);
printf("\n %s %s %s %.2f",car[3].carmaker, car[3].model, car[3].colour, car[3].price);
printf("\n %s %s %s %.2f",car[6].carmaker, car[6].model, car[6].colour, car[6].price);
printf("\n %s %s %s %.2f",car[7].carmaker, car[7].model, car[7].colour, car[7].price);
break;
}
if (option == '4')
{
printf("\nEnd pf Program");
}
else
{
printf("Error input");
}
}while(option!= '4');
}fclose (fp);
}
return 0;
}
This is a logical error, which just so happened to have altered the rest of your program.
fread(&car[8], sizeof(struct CarType), 8, fp);
Firstly, it is a buffer overflow. It writes memory starting at the end of the allocated buffer. You were probably thinking of this:
fread(car, sizeof(struct CarType), 8, fp);
Secondly, you are assuming each line is exactly the size of your struct (34 bytes). So, I would avoid using fread in this case, since you don't know the size of each line.
I recommend using this instead:
int fscanf(FILE *stream, const char *format, ...);
Example:
FILE* txtFile = fdopen("test.txt","r");
char a[10],b[10];
fscanf(txtFile, "%s %s\n", a, b);
To summarize, you should read one line at a time with fscanf and once you are finished parsing all the data, then you should loop through all the cars to print out all the cars that satisfy the correct pricing ranges.
Suggestion: It might be helpful to create a print function for your struct. Something with this kind of prototype:
void printCar(CarType* car);
Also, man pages are your friend. If you want more info on fscanf do man fscanf in your terminal or look up fscanf man pages on Google.
No wonder you get strange input. You are attempting a binary read of text from a file into a struct -- that won't work. Instead, you need to read a entire line of data with fgets() or POSIX getline() and then separate (parse) the needed values from the filled array with sscanf(). While your member array size of 10 will work, buy yourself a little more room. I would use 16 at a minimum.
When you declare your struct, you can add a typedef an avoid having to write struct cartype each time a type is needed, writing simply cartype instead. you could do something similar to:
#include <stdio.h>
#define MAXC 16 /* if you need a constant, #define one (or more) */
#define LINE 1024
typedef struct { /* a typedef allows you to refer to the type without struct */
char carmaker[MAXC],
model[MAXC],
colour[MAXC];
double price;
} cartype;
In main(), you need your array of struct, a counter and a buffer (character array) to hold each line read from your data file.
int main (int argc, char **argv) {
char buf[LINE]; /* buffer to hold line */
size_t n = 0; /* counter */
cartype car[MAXC] = {{.carmaker = ""}}; /* array of cartype (MAXC of them) */
(note: the declaration of int main (int argc, char **argv). Do not hardcode filenames in your code, instead pass the filename to read as an argument to your program. You should not have to recompile your code just to read from a different file)
You can read from the filename provide as the 1st argument to your program on the command line (or read from stdin by default if no argument is given) with:
/* use filename provided as 1st argument (stdin by default) */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}
Above, a simple ternary is used to open the file given on the command line, or assign stdin to fp by default if no argument is given. A ternary is just a shorthand if .. else .. statement with the form test ? if_true : if_false. Where the test can be any conditional, and if the condition tests true the if_true part of the statement is used, otherwise the if_false part is used.
Reading each line from the file and separating the values into your stuct members couldn't be easier. You just read a line with fgets() and then parse the values with sscanf() validating the return of sscanf() to confirm all values were successfully parsed from the line. On success, you just update your counter. You use the counter as a test condition in the while() loop to ensure you do not attempt to store more values than your array can hold, e.g.
/* while array not full, read line of input */
while (n < MAXC && fgets (buf, LINE, fp)) { /* parse values with sscanf() */
if (sscanf (buf, "%15s %15s %15s %lf", /* always use field-width */
car[n].carmaker, car[n].model,
car[n].colour, &car[n].price) == 4) { /* validate 4 conversions */
n++; /* increment counter */
}
}
(note: when using any scanf() family of functions for string input, you must use the field-width modifier to ensure you do not attempt to store more characters to an array than it can hold. Without the field-width modifier, scanf()/sscanf() are no better than gets() See: Why gets() is so dangerous it should never be used!)
That's it. All you need to do is close your input file and output your values, e.g.
if (fp != stdin) /* close file if not stdin */
fclose (fp);
for (size_t i = 0; i < n; i++)
printf ("car[%2zu] : %-16s %-16s %-16s %10.2f\n",
i, car[i].carmaker, car[i].model, car[i].colour, car[i].price);
}
Putting it altogether, you would have:
#include <stdio.h>
#define MAXC 16 /* if you need a constant, #define one (or more) */
#define LINE 1024
typedef struct { /* a typedef allows you to refer to the type without struct */
char carmaker[MAXC],
model[MAXC],
colour[MAXC];
double price;
} cartype;
int main (int argc, char **argv) {
char buf[LINE]; /* buffer to hold line */
size_t n = 0; /* counter */
cartype car[MAXC] = {{.carmaker = ""}}; /* array of cartype (MAXC of them) */
/* use filename provided as 1st argument (stdin by default) */
FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;
if (!fp) { /* validate file open for reading */
perror ("file open failed");
return 1;
}
/* while array not full, read line of input */
while (n < MAXC && fgets (buf, LINE, fp)) { /* parse values with sscanf() */
if (sscanf (buf, "%15s %15s %15s %lf", /* always use field-width */
car[n].carmaker, car[n].model,
car[n].colour, &car[n].price) == 4) { /* validate 4 conversions */
n++; /* increment counter */
}
}
if (fp != stdin) /* close file if not stdin */
fclose (fp);
for (size_t i = 0; i < n; i++)
printf ("car[%2zu] : %-16s %-16s %-16s %10.2f\n",
i, car[i].carmaker, car[i].model, car[i].colour, car[i].price);
}
Example Use/Output
With your sample input int the file dat/imports.txt, you would pass the filename on the command line as the first argument, e.g.
$ ./bin/import_cars dat/imports.txt
car[ 0] : Toyota Altis Silver 120000.00
car[ 1] : Toyota Vios Black 90000.00
car[ 2] : Honda Accord Black 152000.00
car[ 3] : Honda Civic Silver 118000.00
car[ 4] : Nissan Cefiro Black 151000.00
car[ 5] : Nissan Sylphy Silver 121000.00
car[ 6] : Proton Perdana Black 110000.00
car[ 7] : Proton Waja Blue 70000.00
Look things over and let me know if you have further questions.
If you created the file using NotePad, or any text editor for that matter, you need make sure it saved the text as ASCII or UTF-8 no-BOM. Otherwise, you'll have to deal with code point conversions, as the codes for storing text vary widely. See Wikipedia Character encoding, the history is tightly entangled with how C processes strings of text.
Your text appears to be what we call a space delimited file. That means each line is a record and each field in the record is delimited by whitespace. Your struct however is an abstraction over physical memory that defines the fields and their types. You need to read the text file and convert each record into a struct.
Read up on the following:
fgets
fscanf
strtof
strtok
strcpy
You have options. You can read each line of the file into your struct using fscanf, or read each line into a string buffer using fgets and then use strtok to iterate over each token in the buffer and either strcpy, in the case of the string fields, and strtof for the float.
You'll find lots of examples of how others have solved similar problems in these search results: https://stackoverflow.com/search?q=%5Bc%5D+convert+string+to+struct%3F
Since this is a homework assignment, I won't just hand you code. Go study, pick a path and start writing code. As soon as you run into a problem, do a quick search here for any possible answers, and start a new question if you don't find the answer.
owwwwwwwwwwwwwwwww yeahhhhhhhhhhhhh
thx everyone, My problem was solved.
and here is my code
#include <stdio.h>
#include <stdlib.h>
struct carType
{
char maker[10],model[10],colour[10];
float price;
}carType;
int main()
{
int n;
int option;
FILE *fp;
struct carType car[8];
if((fp = fopen("car.txt", "r")) == NULL)
{
printf("File can't be opened.");
}
else
{
for(n=0;n<8;n++)
{
fscanf(fp,"%s %s %s %f",car[n].maker,car[n].model,car[n].colour,&car[n].price);
}
printf("Choose your option\n");
printf("1-List car price equal or above RM120,000\n");
printf("2-List car price RM120,000 - RM149,999\n");
printf("3-List car price RM50,000 - RM119,999\n");
printf("4-End program\n");
printf("?");
while (!feof(fp))
{
do
{
scanf("%d",&option);
if (option == 1)
{
printf("\nCar price equal or above RM120,000");
for(n=0;n<8;n++)
{
if(car[n].price>=120000)
{
printf("\n%-10s %-10s %-10s %.0f",car[n].maker, car[n].model, car[n].colour, car[n].price);
}
}
printf("\n\n");
break;
}
else if (option == 2)
{
printf("\nCar price RM120,000 - RM149,999");
for(n=0;n<8;n++)
{
if(car[n].price>=120000 && car[n].price<=149999)
{
printf("\n%-10s %-10s %-10s %.0f",car[n].maker, car[n].model, car[n].colour, car[n].price);
}
}
printf("\n\n");
break;
}
else if (option == 3)
{
printf("\nCar price RM50,000 - RM119,999");
for(n=0;n<8;n++)
{
if(car[n].price>=50000 && car[n].price<=119999)
{
printf("\n%-10s %-10s %-10s %.0f",car[n].maker, car[n].model, car[n].colour, car[n].price);
}
}
printf("\n\n");
break;
}
else if (option == 4)
{
printf("End of Program\n");
return 0;
}
else
{
printf("Error input...");
}
}while(option!= 4);
}fclose (fp);
}
return 0;
}
:D

After reading from text file, results are displayed incorrectly

I've written a program that reads four variables (three strings and one character) every line from a text file. But when I display the variables, an unexpected character pops up at the end of each line. (I've ensured that the lengths of the variables are large enough).
Why is this? (Overflowing buffers, again?) And how do I fix this?
Text file contents:
M0001 Cool Name F 123-456789
M0002 Name Cool M 987-654321
Code:
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *text;
char id[6], name[101], gender, contact[13];
text = fopen("test.txt", "r");
while (fscanf(text, "%s %[^\n]s %c %s\n", id, name, &gender, contact) != EOF)
printf("%s %s %c %s\n", id, name, gender, contact);
fclose(text);
return 0;
}
The output I expect:
M0001 Cool Name F 123-456789
M0002 Name Cool M 987-654321
What I get instead:
M0001 Cool Name F 123-456789 1⁄4
M0002 Name Cool M 987-654321 1⁄4
in the call to fscanf(), the format string: "%s %[^\n]s %c %s\n" is not correct.
the '[^\n]' will read to the end of the line (which will overflow the input buffer: `name'. Then the next char is NOT an 's' because the next character is the newline.
should compare the returned value to 4, not EOF
the input/format specifiers '%[...]' and '%s' have no problem overflowing the input buffer, so should ALWAYS have a MAX_CHARACTERS modifier that is one less than the length of the input buffer (those format specifiers always append a NUL byte to the input
The following proposed code:
cleanly compiles
documents why each header file is included
performs the desired functionality
splits the 'name' into 'firstname' and 'lastname' for easier handling and to match the format of the input data
properly checks the returned value from fscanf()
properly checks for any error from fopen() and if an error is returned, properly outputs the error message and the text indicating why the system thinks the function failed to stderr
uses an appropriate format string for the calls to fscanf() and printf()
replaces 'magic' numbers with meaningful names via a enum statement
And now the proposed code:
#include <stdio.h> // fopen(), fclose(), fscanf(), perror(), printf()
#include <stdlib.h> // exit(), EXIT_FAILURE
enum{
MAX_ID_LEN = 6,
MAX_NAME_LEN = 20,
MAX_CONTACT_LEN = 13
};
int main( void )
{
char id[ MAX_ID_LEN ];
char firstname[ MAX_NAME_LEN ];
char lastname[ MAX_NAME_LEN ];
char gender;
char contact[ MAX_CONTACT_LEN ];
FILE *text = fopen("test.txt", "r");
if( !text )
{
perror( "fopen to read 'test.txt' failed" );
exit( EXIT_FAILURE );
}
// implied else, fopen successful
while (5 == fscanf(text, "%5s %19s %19s %c %12s",
id, firstname, lastname, &gender, contact) )
{
printf("%s %s %s %c %s\n",
id, firstname, lastname, gender, contact);
}
fclose(text);
return 0;
}
%[^\n]s eats up everything from that point on and puts it in name. So only id and name are filled. gender and contact have 'random' contents coming from the program stack (as they are not initialized).
By accident the your stack had 1/4 in gender + contact.
On my machine, the program crashes.
As the number of space-delimited words in your name apparently is variable, you can only use %[^\n]s to grab "as much as possible" – but that will also eat up any and all following relevant data. A quick solution would be to re-design the input format and place the name at the very end; then, your fscanf argument would be:
"%s %c %s %s\n", id, &gender, contact, name
Alternatively, rewrite the code to use less fscanf and more 'manual' parsing:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main (void)
{
FILE *text;
char id[6], name[101], gender, contact[13];
char *lookback;
int result;
unsigned int line_number = 0;
text = fopen ("test.txt", "r");
if (text == NULL)
{
printf ("file not found!\n");
return EXIT_FAILURE;
}
do
{
result = fscanf(text, "%s %[^\n]s\n", id, name);
line_number++;
if (result == EOF)
break;
if (result != 2)
{
printf ("error in data file on line %u (expected at least 2 items)\n", line_number);
break;
}
/* at this point, 'name' also contains 'gender' and 'contact' */
lookback = strrchr (name, ' ');
if (lookback == NULL || strlen(lookback+1) > 12)
{
printf ("error in data file on line %u (expected 'contact')\n", line_number);
break;
}
/* lookback+1 because lookback itself points to the space */
strcpy (contact, lookback+1);
/* cut off at lookback */
*lookback = 0;
lookback = strrchr (name, ' ');
if (lookback == NULL || strlen(lookback+1) != 1)
{
printf ("error in data file on line %u (expected 'gender')\n", line_number);
break;
}
/* lookback now points to the space before the gender */
gender = toupper(lookback[1]);
if (gender != 'F' && gender != 'M')
{
printf ("error in data file on line %u (expected 'M' or 'F')\n", line_number);
break;
}
/* cut off again at lookback; now name is complete */
*lookback = 0;
printf ("%s %s %c %s\n", id, name, gender, contact);
} while (1);
fclose(text);
return EXIT_SUCCESS;
}
This method does have a couple of associated drawbacks. One of the perks of scanf is that it normalizes whitespace; multiple spaces and tabs (or even returns) will silently be translated to a single space before scanning. This code, on the other hand, explicitly checks for a single space character. If there is variation in the whitespace in your data file, you must account for that as well.
With the last two items 'manually' processed, you can opt to not use fscanf at all. You can read an entire line of text at once with fgets (which also has a line length check built in) and look for spaces using strchr and strrchr. To counter possible whitespace problems, search the line for tabs and double spaces and change these to a single space.

Why ftell prints -1 as value of file pointer? And why errno prints out "INVALID ARGUMENT"?

I have these 2 functions in a project which loads and saves user's information into a file. Each user is saved in a new line of the file. My problem is that the program crashes when I try to use ftell(f). When I print ftell(f) it prints -1 after opening the file with fopen(). I tried to see in errno the error, but it prints "NO ERROR" after fopen() but "INVALID ARGUMENT" once I use fseek to modify the file pointer f position.
My problem is in my Load_File function, but I show the Save_File function too for checking I write correctly in the file.
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
LIST Load_File(LIST L){
//PRE: receive a void list (L==NULL)
//POST: returns an user's loaded from file
USER user; //node of list
char str[150];
//User's structure
char name[30];
char CI[10];
char email[30];
char city[30];
char password[30];
errno=0;
FILE *f;
if(f=fopen("DATOS.txt","r")==NULL){
printf("File not found. \n");
return L;
}
//Code for verify what's happening
printf("FTELL: %d/n", ftell(f)); //prints -1
printf("ERRNO: %s\n", strerror(errno)); //prints NO ERROR
fseek(f, 0, SEEK_CUR);
printf("FTELL: %d\n", ftell(f)); //still prints -1
printf("ERRNO: %s\n", strerror(errno)); //prints INVALID ARGUMENT
printf("FEOF: %d\n",feof(f)); //CRASHES! (a)
while(feof(f)==0){ //CRASHES when (a) line is deleted
//Load line of file in user's structure
fgets(str, 150, f);
sscanf(str,"%s %s %s %s %s ",name, CI, email, city, password);
//Copy into structure's parts
strcpy(user->name, name);
strcpy(user->CI, CI);
strcpy(user->email, email);
strcpy(user->city, city);
strcpy(user->password, password);
Add_user_to_list(L, user);
}
if(fclose(f)!=0) printf("\n\n FILE NOT PROPERLY ClOSED \n\n");
}
void Save_File(LIST L){
//PRE: receive an user's list
//POST: saves a new list in file
FILE *f;
int flag=0;
f=fopen("DATOS.txt","w");
if(f==NULL){
printf("Error opening file f\n");
}
if(!L_Void(L)){
L=L_First(L);
do{
if(flag) L=L_Next(L);
flag=1;
fprintf(f,"%s %s %s %s %s \n",L_InfoM(L)->name,L_InfoM(L)->CI, L_InfoM(L)->email, L_InfoM(L)->city, L_InfoM(L)->password);
}while(!L_Final(L));
}else printf("List is void, then nothing was saved.\n");
if(fclose(f)!=0) printf("\n\n FILE NOT PROPERLY COSED \n\n");
}
This code is wrong:
if(f=fopen("DATOS.txt","r")==NULL){
Binary operators - such as == - have higher precedence than assignment operators - such a =.
So your code is parsed as:
if(f=( fopen("DATOS.txt","r")==NULL ) ){
The result of the logical == comparison is assigned to f.
Why are you stuffing the assignment into the if statement? This is much clearer, as well as being a lot less bug-prone:
FILE *f = fopen( "DATOS.txt", "r" );
if ( NULL == f ) {
...
The more you do on one line, the more likely you'll make a mistake. Programming correctly is hard enough. Don't do things that make it harder - like try to see how much code you can stuff into one line.

Can't get txt file to print correctly

This is a homework assignment for my C Programming class.
I am given a text file, with two data columns; the first column is the age; the second column is the avgprice. I'm able to read and print the values fine. However, for some reason the age and avgprice are flipped in the output. I have no clue why.
Here is the code
#include "stdafx.h"
#include <stdio.h>
int main() {
double age, avgprice; //age = 1st column, avgprice = 2nd column
FILE *corolla; //ptr for file
char eof; //needed for end of file check
corolla = fopen("C:/Users/Nate/Downloads/DataFiles/corolla.txt", "r");
if (corolla == NULL) { //makes sure the file exists
printf("File does not exist!\n");
return 0; //prevents crashing
}
else {
printf("Age \t\t Average Price\n"); //header for data when printed
/*prints values until we're at the end of the file*/
while (fscanf(corolla, "%c", &eof) != EOF) {
fscanf(corolla, "%lf %lf", &age, &avgprice); //scans in data from file
printf("%.1f \t\t $%.2f\n", age, avgprice); //prints data from file
}
}
fclose(corolla); //closes file
return 0;
}
This is what the output looks like
It's puzzling to me because I have used this exact format to do the same thing with other data files--no issues. For some reason, this one file is having difficulty.
Here is the datafile I'm supposed to be reading. I've uploaded it to my Dropbox that way you can inspect the formatting if necessary. Corolla.txt
This line:
while (fscanf(corolla, "%c", &eof) != EOF)
reads a character from the file. The first character in the file is 1 so it reads that 1 into eof.
Your next line is:
fscanf(corolla, "%lf %lf", &age, &avgprice);
which reads the next two entries from the file, which are 13990 and 2, in that order. So the first age is 13990 and the first avgprice is 2.
After that, the file pointer is now pointing to the blank space after the 2. When you go:
fscanf(corolla, "%c", &eof)
it reads a space into eof.
Then when you get to:
fscanf(corolla, "%lf %lf", &age, &avgprice);
It reads the next two values, 13495 and 3 respectively. And so on.
To fix this you should stop doing fscanf(corolla, "%c", &eof). I don't know what you are expecting this to do exactly , but it does not test whether you're at the end of the file or not. Instead it reads a character, ignores the character, and checks the return value of fscanf.
To fix your code:
while (2 == fscanf(corolla, "%lf %lf", &age, &avgprice))
{
printf("%.1f \t\t $%.2f\n", age, avgprice); //prints data from file
}
The return value of fscanf is the number of items successfully read (if it succeeded). When it returns something other than 2 you know you must have hit the end of the file.
Your input file uses a line-based format. fscanf reads the input chunk by chunk. A chunk is usually something separated by white space, which can be space, tabs or even the new-line. Therefore fscanf is not suited to read line-based formats.
In my opinion, it is better to read the input in two steps: first, read a line with fgets, then read the data from that line with sscanf. For example:
#include <stdlib.h>
#include <stdio.h>
int main()
{
FILE *f;
int line = 0;
f = fopen("kk", "r");
if (f == NULL) {
printf("File does not exist!\n");
return 0;
}
printf("%20s%20s\n", "age", "avg. price ($)");
for (;;) {
char buffer[80];
int age, price;
if (fgets(buffer, sizeof(buffer), f) == NULL) break;
line++;
if (sscanf(buffer, "%d %d", &age, &price) < 2) {
printf("(Skipping bad input in line %d).\n", line);
} else {
printf("%20d%20d\n", age, price);
}
}
fclose(f);
return 0;
}
This also gives you a kind of low-level error reporting.
Also, there's usually no need to do extra checking for EOF. The file input functions return a special value when the end of the file is reached. fscanf and getc return EOF; fgets returns NULL. It is usually always better to stop reading based on these return values.
In your case, the fscanf("%c", &oef) eats up the first character in your file, the digit 1. Luckily, after that it only feeds on new-line so your input doesn't get tripped up worse. (But change your scan format to "%lf %lf " for a drastic price reduction.)

reading primitives from file in C

I am new to C, and want to read some data from a file.
Actually, I find many reading functions, fgetc, fgets, etc..
But I don't know which one/combination is the best to read a file with the following format:
0 1500 100.50
1 200 9
2 150 10
I just need to save each row above into a struct with three data members.
I just need to know the best practice to do that, hence I am new to C programming.
Thanks.
Try reading each line using fgets. With each line, you can then use sscanf.
FILE* f = fopen("filename.txt", "r");
if (f) {
char linebuff[1024];
char* line = fgets(linebuff, 1024, f);
while (line != NULL) {
int first, second;
float third;
if (sscanf(line, "%d %d %g", &first, &second, &third) == 3) {
// do something with them..
} else {
// handle the case where it was not matched.
}
line = fgets(linebuff, 1024, f);
}
fclose(f);
}
This may have errors, but it's just meant to give you an example of how you might use the functions. Be sure to validate what sscanf returns you.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static void
read_file(const char *fname)
{
FILE *f;
char line[1024];
int lineno, int1, int2, nbytes;
double dbl;
if ((f = fopen(fname, "r")) == NULL) {
perror("fopen");
exit(EXIT_FAILURE);
}
for (lineno = 1; fgets(line, sizeof line, f) != NULL; lineno++) {
int fields = sscanf(line, " %d %d %lg %n", &int1, &int2, &dbl, &nbytes);
if (fields != 3 || (size_t) nbytes != strlen(line)) {
fprintf(stderr, "E: %s:%d: badly formatted data\n", fname, lineno);
exit(EXIT_FAILURE);
}
/* do something with the numbers */
fprintf(stdout, "number one is %d, number two is %d, number three is %f\n", int1, int2, dbl);
}
if (fclose(f) == EOF) {
perror("fclose");
exit(EXIT_FAILURE);
}
}
int main(void)
{
read_file("filename.txt");
return 0;
}
Some notes on the code:
The fscanf function is quite difficult to use. I had to experiment a while until I got it right. The space characters between the %d and %lg are necessary so that any white-space between the numbers is skipped. This is especially important at the end of the line, where the newline character must be read.
Most of the code is concerned with checking errors thoroughly. Almost every return value of a function call is checked whether it succeeded or not. In addition, the number of fields and the number of characters that have been read are compared to the expected values.
The format strings for fscanf and fprintf differ in subtle details. Be sure to read the documentation for them.
I used the combination of fgets to read one line at a time and sscanf to parse the fields. I did this because it seemed impossible to me to match a single \n using fscanf.
I used the GNU C Compiler with the standard warning flags -Wall -Wextra. This helped to avoid some easy mistakes.
Update: I forgot to check that each invocation of fgets reads exactly one line. There might be lines that are too long to fit into the buffer. One should check that the line always ends with \n.

Resources