So I am currently getting a Segmentation fault for my code and am trying to narrow down what It could be. I am unsure if the fgetc
function's starting point follows the same positioning as fprintf
and scanf
.
ie If I have used scanf
on a file and then use fgetc
would it start from the very beginning or would it continue where scanf
left off? If it is the former how would I go about manipulating the start point?
//reads the second line of the input file in order to grab the ten numbers in the line
int numRead = fscanf(Data, "\n %d %d %d %d %d %d %d %d %d %d", &zero, &one,
&two, &three, &four, &five, &six, &seven, &eight, &nine);
while(EOF != (c = fgetc(Data)))
{
message[i] = c;
i++;
}
input file:
0 1 2 3 4 5 6 7 8 9
6 7 8 0 1 9 5 2 3 4
If I were reincarnated, I'd want to come back a
buzzard. Nothing hates him or envies him or wants
him or needs him. He is never bothered or in
danger, and he can eat anything.
-- Mark Twain
The fgetc
calls in the loop will very much start off where the fscanf
finished but you should be aware that this point will be at the next character after the last item scanned. Assuming it worked okay, that will be the \\n
character at the end of the second line (assuming you were at the start of that line before-hand, which seems to be the case from your code comments).
Hence the first fgetc
will give you the afore-mentioned \\n
, the next will get you the 'I'
at the start of the third line, and so on.
If you're getting a crash, there's a few things I'd be checking straight away.
The first is that c
is an int
type rather than char
. This is needed so that you can receive any valid char
type from it plus the EOF
indicator.
The second is that message
is big enough to hold the data.
The third is that i
is initialised to zero.
You should probably also check that your scans work to read the ten numbers, just to be safe.
Look over the following complete program to get an idea on how to do this. You'll note I'm also checking to make sure the buffer does not overflow due to too much data being in the file:
#include<stdio.h>
int main(void)
{
// Open file for reading.
FILE *Data = fopen ("qq.in", "r");
if (Data == NULL) {
puts ("Cannot open qq.in");
return 1;
}
// Skip first and second line (twenty numbers).
int zero, one, two, three, four, five, six, seven, eight, nine;
int numRead = fscanf(Data, "%d %d %d %d %d %d %d %d %d %d", &zero, &one,
&two, &three, &four, &five, &six, &seven, &eight, &nine);
if (numRead != 10) {
puts ("Could not read first ten integers");
fclose (Data);
return 1;
}
numRead = fscanf(Data, "%d %d %d %d %d %d %d %d %d %d", &zero, &one,
&two, &three, &four, &five, &six, &seven, &eight, &nine);
if (numRead != 10) {
puts ("Could not read second ten integers");
fclose (Data);
return 1;
}
// Loop for reading rest of text (note initial newline here).
int c, i = 0;
char message[1000];
while(EOF != (c = fgetc(Data))) {
if (i >= sizeof(message)) {
puts ("Too much data");
fclose (Data);
return 1;
}
message[i++] = c;
}
fclose (Data);
printf ("[%*.*s]\n", i, i, message);
return 0;
}
When run, this produces:
[
If I were reincarnated, I'd want to come back a
buzzard. Nothing hates him or envies him or wants
him or needs him. He is never bothered or in
danger, and he can eat anything.
-- Mark Twain
]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.