•
•
•
•
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
![]() |
•
•
Join Date: Dec 2006
Posts: 14
Reputation:
Rep Power: 2
Solved Threads: 0
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:
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!!!
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!!!
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
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.
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.
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.
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.
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.
•
•
Join Date: Dec 2006
Posts: 14
Reputation:
Rep Power: 2
Solved Threads: 0
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.
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.
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 :
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.
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'
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."
"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."
> 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).
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.
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.
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- First year assigment on reading file, sorting and outputting invoice (C++)
- Error Message Concerning Reading File From A Drive (C++)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- reading a file into code (Java)
- reading and printing a file to screen in C (C)
- File parsing in 'C' (C)
- problems with reading random access line from a file (C++)
Other Threads in the C Forum
- Previous Thread: Changing textcolor
- Next Thread: Help to get time zone between GMT & new york



Linear Mode