I have a text file that contains simple account information like this:
1234567890
234567
345678.90
First line is a 10-digit account number. Second line is the 6-digit account PIN (it's a simple program, so no encryption needed yet, whatsoever), and the last is the account balance. Now, when I try to read the text file, it skips the first line (account number), only the PIN and the balance are printed out.
Also, I read in some articles that fscanf() returns a number "every time it reads a line in a document" (correct me if I'm wrong). So, for example:
fscanf(fp, "%s\n%s\n%ld", account, pin, balance);
If it reads the sample lines in the text file above, it would return 1, right (because it read 1 line that matched the format in the fscanf())? And will return a -1 once the EOF is reached, right?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
void loadAccount(){
char account[10];
char pin[10];
float balance;
FILE *fp
fp = fopen("/acctinfo.txt", "r+");
if (fp != NULL) {
while(fscanf(fp, "%s", account) != -1){
fscanf(fp, "%s\n%s\n%f", account, pin, balance);
printf("%s\n", account);
printf("%s\n", pin);
printf("%f\n", balance);
}
} else {
printf("Account info not found.");
}
fclose(fp);
getch();
}
|
EDIT:
I think the problem is with my while() condition. I removed it (turned into a comment) and was able to retrieve the right information from the text, with the exception of the balance. Here was the output:
1234567890
234567
345678.906250
Not sure where the .006250 came from.
EDIT 2:
I changed the while condition back to !feof(fp) and I was able to fetch the right content from the text file. But. I'd really want to implement this without using !feof() because, as said by many people from the forums here, it's wrong to use !feof()...