473,388 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

File Read help reqd

Hello All,

Can any one help me in this file read problem.

#include <stdio.h>

int main()
{
FILE* fp = fopen("TITLE.mx2","rb");

char h[100];

while(fscanf(fp,"%[^\r\n]",h) != EOF)
{
fprintf(stdout,"%s",h);
if((fscanf(fp,"%[\r\n]",h) == EOF)) //dummy
break;
}
fclose(fp);
return 0;

}

it goes into an infinite loop.

Am i eating up the EOF?

here while(fscanf(fp,"%[^\r\n]",h) != EOF)?

how to avoid it?

Thanks in advance.

Regards,
Naren.

Nov 14 '05 #1
7 2211
Naren wrote:

#include <stdio.h>
int main()
{
FILE* fp = fopen("TITLE.mx2","rb");
char h[100];

while(fscanf(fp,"%[^\r\n]",h) != EOF)


fscanf can never return EOF. Try reading its definition.

--
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare
Nov 14 '05 #2
CBFalconer wrote:
Naren wrote:

#include <stdio.h>
int main()
{
FILE* fp = fopen("TITLE.mx2","rb");
char h[100];

while(fscanf(fp,"%[^\r\n]",h) != EOF)


fscanf can never return EOF. Try reading its definition.


According to my copy of the Standard:
7.19.6.2p16:

The fscanf function returns the value of the macro EOF if an input
failure occurs
before any conversion.

Am I missing something?

Robert Gamble

Nov 14 '05 #3
In article <11**********************@g47g2000cwa.googlegroups .com>
Naren <na******************@gmail.com> wrote:
[snippage, and minor vertical compression]
FILE* fp = fopen("TITLE.mx2","rb");
char h[100];

while(fscanf(fp,"%[^\r\n]",h) != EOF) {
fprintf(stdout,"%s",h);
if((fscanf(fp,"%[\r\n]",h) == EOF)) //dummy
break;
}
... goes into an infinite loop.


One possibility is that the fopen() has failed and fp==NULL and
the undefined behavior of calling fscanf() with this NULL is
that it does nothing other than return a value not equal to -1.
For instance, perhaps your fscanf() happens to return 42 when
called with NULL (and read no data, or at least do nothing
visibly harmful). This could easily result in an infinite
loop.

Another possibility is that one of the fscanf() calls overruns the
buffer h[], i.e., there are more than 99 non-\r non-\n characters
in sequence at some point, or there are more than 99 \r-or-\n
characters in sequence at some other point. This would store
more than 100 "char"s in the 100-char array, leading to undefined
behavior, which could also result in an infinite loop.

It also seems very odd to open with "rb" and then use fgets(),
since fgets() is a "text-file-oriented" operation (text files are
composed of "lines", while binary files contain arbitrary binary
data not necessarily composed of lines). But this in and of
itself should not result in an infinite loop.

Other than these issues, the code *should* not loop forever. The
first fscanf() directive -- a "%[" -- matches one or more non-\r
non-\n characters, or fails with either an input or a matching
failure. In most cases (non-blank first text line), it will succeed,
and read-and-convert the non-\r-or-\n characters, storing them in
h[].

If the file begins with a sequence of \r-or-\n characters, the
first fscanf() performed will fail with a matching failure and
return 0, leaving garbage in the array h[]. The fprintf() to stdout
would then technically have undefined behavior, which theoretically
could result in an infinite loop; but in practical terms, on the
computer you are using (the one I see in my crystal ball :-) ),
you would just get either garbage output, or no output at all, from
that fprintf().

Having either succeeded or failed with a matching failure, fscanf()
will then return either 1 or 0 (respectively). You ignore this
distinction in return values -- which is almost always a bug, and
is indeed a bug in this case -- and go on to print the contents of
the array (possibly garbage).

You then call fscanf() again, using another %[ directive, but this
one says "read and convert at least one, and as many as possible,
\r or \n characters". Since the previous conversion will have read
and converted everything that is not a \r or \n, the only possible
character in the stream at this point is \r or \n. Thus, if there
are any characters to read at all, the %[ directive will definitely
succeed. The only other possibility is that getc(fp) will return
EOF (due to input error or end-of-file), rather than returning a
'\r' or '\n' character; in that case, the fscanf() call itself will
return EOF, and the "break" will terminate the loop. Since the
loop does not terminate, we can assume that fscanf() does find at
least one \r or \n, and writes them into the array h[]. (Thus, if
there are 37 '\r' characters followed by 3 '\n's followed by 9
'\r's followed by 'x' or EOF, h[] will have 37+3+9+1 = 50 bytes
written into it.) The fscanf() call will then return 1 -- the
only possible return values are EOF and 1, in this case. Hence
the test fscanf(...)==EOF is sufficient, unlike the initial one in
the while loop.

If the fscanf() does return 1, we know that the fscanf() stopped
at either a non-\r non-\n character, or it encountered EOF. Thus,
subsequent trips through the while loop's test will have a return
value from fscanf() of either EOF or 1. Only the very first
fscanf() performed can return 0 (when the file begins with a
'\r' or '\n' character).

All of this may leave the original poster with the question of what
he *should* code, instead of the above; but no one can really answer
that, as he has not expressed a goal, merely a question about the
loop. I can make a guess here (that the goal is to remove all
'\n' and/or '\r' characters from a binary file), but it is just a
guess, and it seems a little unlikely.
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
Nov 14 '05 #4
Robert Gamble wrote:
CBFalconer wrote:
.... snip ...
fscanf can never return EOF. Try reading its definition.


According to my copy of the Standard:
7.19.6.2p16:

The fscanf function returns the value of the macro EOF if an input
failure occurs before any conversion.

Am I missing something?


No, but I am. Thanks for the correction.

--
"I conclude that there are two ways of constructing a software
design: One way is to make it so simple that there are obviously
no deficiencies and the other way is to make it so complicated
that there are no obvious deficiencies." -- C. A. R. Hoare

Nov 14 '05 #5
El Thu, 02 Jun 2005 10:19:36 GMT, CBFalconer escribió:
According to my copy of the Standard:
7.19.6.2p16:

The fscanf function returns the value of the macro EOF if an input
failure occurs before any conversion.

Am I missing something?


No, but I am. Thanks for the correction.


Not at all. It says *input failure* needed to return EOF. So, if a conversion
occurs ([^\r\n]) with no input failure, fscanf will not return EOF and
thus the while will be a forever loop. Please correct me if i'm wrong.

Greetings
--
Luis Alberto Giménez
JabberID: Si*******@bulmalug.net
GnuPG ID: 0x3BAABDE1
Nov 14 '05 #6
In article <m5**********@127.0.0.1>,
Alberto =?iso-8859-1?Q?Gim=E9nez?= <al****@teleline.es> wrote:
El Thu, 02 Jun 2005 10:19:36 GMT, CBFalconer escribió:
According to my copy of the Standard:
7.19.6.2p16: The fscanf function returns the value of the macro EOF if an input
failure occurs before any conversion.
Not at all. It says *input failure* needed to return EOF. So, if a conversion
occurs ([^\r\n]) with no input failure, fscanf will not return EOF and
thus the while will be a forever loop. Please correct me if i'm wrong.


No, if a conversion occurs then at least one character will be removed
from the input stream -- and with that particular format, all the
characters until EOF or the first \r or \n will be removed from the
input stream.

Thus after the execution of the fscanf() that is the condition of the
while() loop, if a conversion occured then input has been removed and
one is positioned at an EOF or \r or \n. Then one encounters the
second fscanf() which is the condition of the "if" statement.

If one was at a \r or \n then the fscanf() at the "if" will remove all
the \r and \n that await in sequence, either leaving one at EOF or
at a character which is not \r or \n and looping back.

Inductively, this will repeat until one or the other of the fscanf()
encounters EOF; at that point, the next fscanf() *will* return EOF
and either the 'break' will take effect or the while() condition will
become false and the loop will end.
There is the case where the input starts out positioned at \r or \n
but there are characters there. In that situation, the conversion count
will be 0 for the while()'s fscanf(), but 0 != EOF so the loop body
will be executed and the inner fscanf() will consume the \r \n sequence
leading back to the case solved above.
The next bit you need to know for this to work is that fscanf()
*defines* that when EOF is reached with no conversion having taken
place, that an "input failure" is returned. And then, as was quoted,
that "input failure" is signaled by the value EOF being returned.

For more certainty, examine the example at the end of the C89
description of fscanf(), and notice that it -specifically- has
"count" becoming EOF at the end of the input stream.

--
Ceci, ce n'est pas une idée.
Nov 14 '05 #7
Hello All,

I am extremely sorry for the delay,
Thanks for answers.

I have thing to add if i just added

while(!feof(fp) && fscanf(fp,"%[^\r\n]",h) != EOF)

It breaks the infinte loop and it works fine. Is there anything wrong
in my usage?

Thanks and Regards,
Naren.

Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Frak | last post by:
Hi. I have a text file which in which the lines are numbered this way: - - and so....
5
by: simon place | last post by:
is the code below meant to produce rubbish?, i had expected an exception. f=file('readme.txt','w') f.write(' ') f.read() ( PythonWin 2.3 (#46, Jul 29 2003, 18:54:32) on win32. ) I got...
0
by: Ganesh Kolappan via .NET 247 | last post by:
Hi I am trying to populate a <asp:dropdownlist> in a XSLT file withdatasource pointing to a C# codebehind file method which returnsa dataview. I am using XSLT extension object. But I am...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
3
by: Mark | last post by:
My experience with reading/writing files is to open one file, read it into memory close it and open another file and write to it. Now I would like to learn how to read and write to the same file....
9
by: Use*n*x | last post by:
Hello, I have a binary file (image file) and am reading 4-bytes at a time. The File size is 63,480,320 bytes. My assumption is that if I loop through this file reading 4 bytes at a time, I...
5
by: Diwa | last post by:
Does the "value" type (value as in key-value pair )of "std::map" require a default ctor even if it is not used ? If I comment out Line 1 in the code attached later, i.e remove the default ctor...
3
by: JDeats | last post by:
I have some .NET 1.1 code that utilizes this technique for encrypting and decrypting a file. http://support.microsoft.com/kb/307010 In .NET 2.0 this approach is not fully supported (a .NET 2.0...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.