issue with fscanf

Hi,
please help with below program.

it is just reading a file and printing it.
while reading the file i am copying last two fields into character arrays.
- after reading the second record in fscanf
- character array dump_bill_date which is copied using first record printing null value


sample records in the file.

115786488,1,1,0,0,0,20140819,20140818
171327481,1,1,-2000,4000,0,20130527,20130527


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include<stdio.h>
#include<string.h>
int main()
{
short seq;
long ban;
long rank;
char ch;
long long a, b,c;
char bill_date[8];
char s_date[8];

char dump_bill_date[8];
char dump_bill_due_date[8];

FILE* fptr=NULL;
fptr=fopen("bill.txt","r");

while( fscanf(fptr,"%ld,%hu,%ld,%lld,%lld,%lld,%8s,%8s",&ban,&seq,&rank,&a,&b,&c,bill_date,s_date) != EOF )
{
printf( "dump_bill_date = %-.8s\n",dump_bill_date);
printf( "dump_bill_due_date = %-.8s\n",dump_bill_due_date);

printf( "Ban = %ld \n ",ban);
printf( "Seq = %hu \n",seq);
printf( "rank = %ld \n",rank);
printf( "a = %lld \n",a);
printf( "b = %lld \n",b);
printf( "c = %lld \n",c);
printf( "bill = %-.8s \n",bill_date);
printf( "s_date = %-.8s \n",s_date);
strncpy(dump_bill_date,bill_date,8);
strncpy(dump_bill_due_date,s_date,8);

printf( "dump_bill_date = %-.8s\n",dump_bill_date);
printf( "dump_bill_due_date= %-.8s\n",dump_bill_due_date);

}
fclose(fptr);
}

###output##

dump_bill_date =
dump_bill_due_date = À
Ban = 115786488
Seq = 1
rank = 1
a = 0
b = 0
c = 0
bill = 20140819
s_date = 20140818
dump_bill_date = 20140819
dump_bill_due_date = 20140818
dump_bill_date =
dump_bill_due_date = 20140818
Ban = 171327481
Seq = 1
rank = 1
a = -2000
b = 4000
c = 0
bill = 20130527
s_date = 20130527
dump_bill_date = 20130527
dump_bill_due_date = 20130527
Last edited on
You need to edit your post and repaste your code in code tags: http://www.cplusplus.com/forum/articles/16853/
1
2
char bill_date[8];
char s_date[8];

Your buffers are too small. They can store only 7 chars + '\0',
but you try to copy 8 chars into them.
@tpb
Hi
i edited the code in the between the tags.

@thomas
in the first iteration it is printing the 8 characters correctly
after reading the second record dump_bill_date is not printing any values.
char arrays MAY work if you mess them up. They may NOT work if you mess them up. You are seeing this here.

s_date = 20140818
^^^^^ THIS IS 9 CHARACTERS LONG. IT WON'T FIT IN 8.
You are forgetting that there *must* be space for the extra 0 character.

worse, once you compromise memory, other variables become damage. For example, lets say bill date and s date are next to each other in memory, with bill date first.
if you put 9 characters in bill date, the FIRST character of sdate now contains the end of string marker that belonged to bill date, because you overflowed. so now sdate comes out as an empty string, because its first character is end of string. Such corruption is unrecoverable as it propagates through the code until you finally damage some memory location that is critical and the program crashes, or the program just gets more and more gibberish data and spews a stream of nonsense an end, one or the other usually. Fix the sizes, and it will clear up.

i edited the code in the between the tags.

Thank you for that. :-)
In the future, make sure you paste your original code in the code tags because that saves the indentation.
And there's also an output tag (same as code tag but with the word output) that you could put your output in which also retains the spacing.
Ok here is a dumb question - my current bit of non-working code is very similar to this. I wonder if my system (a Unix box) recognizes "EOF" as end of file? The books that I have read use those characters but I have thought that I needed to replace them in code with some end of file marker (control d) but that does not work??? The books say to put in an EOF test but they say that Unix uses control d and Windows uses control z

I read the lines of input and put them into a structure by the way. I have a separate discussion in the Unix discussions.
Last edited on
Topic archived. No new replies allowed.