I am facing problems looping a fscanf. As in the code below (focus on the part where the while loop starts), I am looping the fscanf until it reached EOF. As you can see from the 2nd part below, the .txt file to fscanf from has only 6 strings,so the fscanf should only loop 6 times and then it reaches EOF. However, as you can see from the program output (2nd part belowe), the fscanf is looped 7 times. Since my program displays the missilenames in reverse order, I assume the while loop looped 1 additional time at the end, resulting at the blank line output on the first line of 3rd picture.
Can someone tell me how to fix this problem pls?
C CODE
while(fscanf(readmissiles,"\n %s \n",missilename)!=EOF)
{
missilename=malloc(20*sizeof(char));
insertvalue(LL,missilename);
missilenum++;
}
TEXT FILE TO FSCANF FROM
single
splash
single
V-Line
h-line
Single
OUTPUT/DISPLAY
/there is a blank line displayed before the Single/
Single
h-line
V-Line
single
splash
single
7
fscanf returns the number of elements scanned or EOF in case of error or end of file is reached before it could match anything. On the last loop fscanf returns 0 - it did not scan the element but it scanned \n, so it returns 0. Do:
while(fscanf(readmissiles, "%s", missilename) == 1)
Loop until there is one element scanned.
Related
I'm trying to implement a cycle which will read lines off a file until it finds a line with a specific format. Namely, until it finds a line with:
number number character number
and nothing (but spaces or tabs) until the newline. Specifically, the stuff I have to go through until I find such a line will always be the contents of a 'lines.rows' matrix, but its data points are not necessarily neatly ordered in 'lines' lines of 'rows' elements. They can have any amount of spaces, tabs or newlines between each element.
However, after lines.rows elements there will always be a line in the format I'm scanning for, after an arbitrary number of spaces, tabs or newlines following the last element of the matrix.
I've been trying for several hours to use fgets and fscanf in different ways to achieve this, but the output is simply not correct. Right now I have this:
for (i=0;i<lines;i++)
{
for(j=0;j<rows;j++)
{
fscanf(in_fp, "%d", &temp);
}
}
, which is working but takes way too long in large matrices. Aside from this I've tried, for example,
for (i=0;i<lines;i++)
{
fscanf(in_fp, "%*[^\n]\n", NULL);
}
, which did not work. The idea was to skip to the end of each line and then also read the new line so as to start at the beginning of the following line. However, by the end of this cycle my file was not pointing to the correct line (which would be one with the format specific above - %d %d %c %d). Instead, it was pointing to a 'random' line in the middle of the matrix at hand.
I also tried the same code as above but with an fgets. When I ran an fscanf following a cycle of fgets for all the lines of the matrix, I still did not read the line that should be coming next (with the format specified above).
If you have any input on how to achieve this, or how to make this question more understandable, I would be very thankful.
I am trying to read from a file line by line by running it through a while loop which is instructed to exit once EOF has been reached. But for some reason once the last line has been read and the while condition is checked again the program just freezes.
This is my code:
char character1;
int number1;
while(fscanf(file,"%s %d",&character1, &number1) != EOF){
//printf("%s %d\n",character1,number1)
}
My files contents:
A 1
B 2
C 3
D 4
E 5
Output:
A 1
B 2
C 3
D 4
E 5
| <---Blinking terminal pointer currently there
Can anyone help me figure this out?
EDIT: I am not opening/closing the file in main(), I am doing it in another function, could this be causing a problem?
Improve the condition checking on the while loop. The fscanf() can produce more results that EOF or a positive number. It can also return a positive number when an end-of-file occurs after conversion has begun. Meaning that you have something going wrong with a conversion and so the data is still there the next time you loop around to get more data from the stream. So you are stuck infinitely failing to convert that same failed conversion.
You are looking for 2 input items so check that the fscanf() has found 2 input items in order to continue looping.
The problem is that your fscanf reads the \r or \n in the end of every line. Just creates a buffer to ignore it and will be fine. I did the following and it worked smoothly.
char character1;
int number1;
char buffer[2]; // will read the end of line
while(fscanf(file,"%c %d",&character1, &number1) != EOF){
fgets(buffer, 2, file);// does the job
printf("[%c] [%d]\n",character1,number1);
}
I am trying to read a file given in following format
hello
{
1--2
2--3
3--4
}
I only want to use integers given in the file and to do that i am using the following code
while(fscanf(fp, "%d--%d", &a, &b) != EOF)
{ // do something here}
The problem is that this is not working because it goes in to an infinite loop after reading first line and if i remove first line it goes to an infinite loop at the last line where it read } . So,how can do this in a proper way?
If all else fails, RTFM:
fscanf: "This function return the number of input items successfully matched and assigned, which can be fewer than provided for, or even zero in the event of an early matching failure."
Meanwhile, EOF=-1 although in some stdio.h header files on some platforms it could be equal to zero.
You could either check that fscanf returns 0 or use feof(fp) to check that the end of file has been reached.
This question already has answers here:
Reading a C file, read an extra line, why?
(6 answers)
Closed 8 years ago.
I am trying to write a C program that is able to read data (strings) from a text file and swap each content in terms of bytes. I have already implemented the code and everything works just fine, and and I am able to read all the contents of the specified text file, but the problem is that the program is printing the last word from the text file twice and I do not know why? Any help will be helpful ! This is the code I have:
while( !feof(ptr_file))
{
//to read in group of words (sentences) if
//needed !.
fscanf(ptr_file, "%s", userName);
//time to swap letters of the word coming from the text file.
swap_a_word(userName, 0, 4);
swap_a_word(userName, 1, 2);
//new space.
printf("\n");
//display the word after swapping to the screen for the user.
printf("%s", userName);
}
The program must not print extra data. I do not know, but when the program reaches the end of the file, it prints the last data of the text file twice. Please any hints will be helpful !.
Thanks !
The problem is the while loop condition
while(!feof(ptr_file))
Note that EOF is preceded by a newline '\n'. fscanf returns the value EOF when the end of file is reached. However, this does not set the end-of-file indicator on the stream and as a result the loop is entered one extra time. You should check the return value of fscanf instead to find number of items successfully matched and assigned.
Hello i have simply function to read from file
while(fscanf(fp," %255[a-zA-Z]",test) == 1)
{
puste = 1;
push(&drzewo,test);
}
It should read only words which contains only alphabetic characters and that works great. When I have for example a single number in my file my while loop quits; how should I change it?
Of course it stops, since the fscanf() call will fail to do the conversion you're requiring, and thus return 0. What would you expect it to do?
It's often better to read whole lines using fgets(), and then parse them "manually", that way it's easy to just do nothing and read another line if the desired data is not found.