User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 397,809 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,512 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 6119 | Replies: 6 | Solved
Reply
Join Date: Dec 2006
Posts: 14
Reputation: DynamitMsk is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
DynamitMsk DynamitMsk is offline Offline
Newbie Poster

reading a file using fscanf

  #1  
Dec 21st, 2006
Hello,

I'm trying to read in a txt file that looks like:

AAAA BBBB 1234
CCCC DDDD 4321
....

and here's what I've done so far:

    char a[4];
    char b[4];
    int c;
    
    FILE *input= fopen("input.txt", "r+");
    if (input==NULL) 
        perror ("Error opening file");
    else{
        while(feof(input)== 0){
    
        fscanf(input,"%4s",a);
        fscanf(input,"%4s",b);
        fscanf(input,"%d",&c);
        }
        fclose(input);
    }

but after it reads the last string I get "access violation writing location" error. Anyone can point me in a right direction to solve this problem? Thank you!!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2005
Posts: 3,340
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 378
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: reading a file using fscanf

  #2  
Dec 21st, 2006
1. AAAA needs 5 characters of storage, not 4
2. Unlike fgets, the count you supply to scanf does not take into account the need for a \0.
3. Trying to use sizeof() with scanf is horrible. There is no easy way to automatically tell scanf what the size of the array is.
4. while(feof(input)== 0) using feof() to control a loop is bad.
http://faq.cprogramming.com/cgi-bin/...&id=1043284351

Try something like
char a[5];  // etc
char buff[BUFSIZ];
while ( fgets( buff, sizeof buff, input ) != NULL ) {
  if ( sscanf( buff, "%4s %4s %d", a, b, &c ) == 3 ) {
  }
}
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Dec 2006
Posts: 14
Reputation: DynamitMsk is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
DynamitMsk DynamitMsk is offline Offline
Newbie Poster

Re: reading a file using fscanf

  #3  
Dec 21st, 2006
Problem is somewhere with End Of File check, but I can't figure out what it is
Reply With Quote  
Join Date: Dec 2005
Posts: 3,340
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 378
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: reading a file using fscanf

  #4  
Dec 21st, 2006
Perhaps you didn't read my post.

You're putting 5 characters into an array with only room for 4
Where does the extra one go?

Two guesses
- over the return address from the function
- over another variable
Neither of these is going to make your code nice and functional.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Join Date: Dec 2006
Posts: 14
Reputation: DynamitMsk is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
DynamitMsk DynamitMsk is offline Offline
Newbie Poster

Re: reading a file using fscanf

  #5  
Dec 21st, 2006
Salem, thank you!!! All I've done is change the size of

char a[5];
char b[5];

and it seems to be working nicely. Could you please tell me why four characters (like, AAAA or BBBB) require an array of size 5? Thank you!

P.S. Also, I noticed your status, maybe you can tell me what's wrong with void main()? I know that it has something to do with the way compiler interpretes it, but I don't know what exactly the problem is.
Last edited by DynamitMsk : Dec 21st, 2006 at 1:39 pm.
Reply With Quote  
Join Date: Jun 2006
Location: India
Posts: 6,806
Reputation: ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold ~s.o.s~ is a splendid one to behold 
Rep Power: 23
Solved Threads: 338
Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Rebellion Revamped

Re: reading a file using fscanf

  #6  
Dec 21st, 2006
Because when you are reading data from the file, it is read in the form of string and in your case, its C string.

The thing about C strings is that other than the characters it holds, it reserves a extra space at the end of the character array for the null character ('\0') which signifies the end of the C style or character array style string.

So when you are reading "AAAA" in your case, its actually is :
AAAA'\0'
As you can see the last character is the null character which is automatically appended at the end of character arrays so that they can be treated as strings. This null character even doesn't show up when you query the length of the string using strlen( ) and hence is a common mistake made by beginners to assume that C strings only require the size equivalent to the number of characters they contain.

So in your previous code, you were using an array of size 4 which caused the null character to be copied to a location which doesn't belong to you and it overwrote the memory which you don't own. This normally leads to what is known as "Segmentation Error" i.e. modifying or touching memory which doesn't belong to you.

Hence changing the size from 4 to 5 caused your code to function correctly. Also consider changing from three sscanf( ) statements to a single fgets( ) and sscanf( ) which leads to a more robust and elegeant solution.
Last edited by ~s.o.s~ : Dec 21st, 2006 at 1:43 pm.
"I don't accept change. I don't deserve to live."

"Working a real job is a win if you're lazy, greedy, or unmotivated. If you're average, you fit right in. And if you're above average, the basic terms of employment and premise of the arrangement is against your interests."
Reply With Quote  
Join Date: Dec 2005
Posts: 3,340
Reputation: Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of Salem has much to be proud of 
Rep Power: 20
Solved Threads: 378
Colleague
Salem's Avatar
Salem Salem is offline Offline
void main'ers are DOOMed

Re: reading a file using fscanf

  #7  
Dec 21st, 2006
> P.S. Also, I noticed your status, maybe you can tell me what's wrong with void main()?
http://faq.cprogramming.com/cgi-bin/...&id=1043284376

You basically have two choices
- learn dialect C, which is specific to your current compiler, and which has to be re-learnt for every new compiler you come across. And also endure the constant nagging of many people to do the "right thing".

- learn ANSI C, which will work exactly the same on any ANSI-C compiler you will come across.

You've already seen that compilers do not trap every possible problem, so you should never regard the absense of warnings as the absense of bugs (or poor portability, style or what have you).
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
Do not PM me for help; You'll be ignored, or told to learn to read.
Do not ask me if I'm muslim - I'm not. Nor do I care about yours or anyone else's mysticism. Religion is a matrix, take the RED PILL.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C Forum

All times are GMT -4. The time now is 6:23 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC