Can I read and write after EOF in a File

M

Matrixinline

Hi All,

Can I read or write data after I encounter EOF in a file. I tried the
below code to write to a file it did not throw any error.

FILE *fFile = fopen(sFilePath, "a+");
if(fFile != NULL)
{
int iSize = fseek(fFile, 0, SEEK_END);
fwrite(sData,1, strlen(sData), fFile);
fwrite(",",1, strlen(","), fFile);
fwrite(sdata2, 1, strlen(sData2), fFile);
fwrite("\0",1, 1, fFile);
fclose(fFile);
}.


and for reading I used like this

FILE *fFile = fopen(sFilePath, "r");
if(fFile != NULL)
{
int iSize = fseek(fFile, 0, SEEK_END);
while (!feof(fFile))
{
char sData[128];
fread(sData, 1, 10, fFile);
printf("%s", sData);
}
fclose(fFile);
}

but it did not read the data I have written to the file.

Can you please let me know what I am doing wrong ?

Thanks
Anup
 
M

Matrixinline

Matrixinline said:



When a function returns EOF, it means it couldn't read any more data
- so that's going to stop you reading data, right? But there's
nothing to stop you writing more data to the file, and nothing to
stop you from reading the data you've written.

 I tried



In append mode, you don't need to seek to the end to find the place
to write. You can just write, and it'll magically appear at the
end.

Yes In append mode it gets to the end of file and then write it there
but I want to write after EOF bit.
fwrite(sData,1, strlen(sData), fFile);
fwrite(",",1, strlen(","), fFile);
fwrite(sdata2, 1, strlen(sData2), fFile);
fwrite("\0",1, 1, fFile);
fclose(fFile);
}.
and for reading I used like this
FILE *fFile = fopen(sFilePath, "r");
if(fFile != NULL)
{
int iSize = fseek(fFile, 0, SEEK_END);

This call moves PAST all the data you've written, to the very end of
the file. Are you really surprised that there's no data past that
point?

while (!feof(fFile))

This is reactive, not predictive.
{
char sData[128];
fread(sData, 1, 10, fFile);

This may fail because EOF is encountered...
printf("%s", sData);

...but you use the data anyway. Better to do it like this:

while((bytes = fread(sData, 1, 10, fFile)) > 0)
{
  printf("%.*s\n", bytes, sData);

}

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999
 
M

mohangupta13

from:www.dinkumware.com/manuals/?manual=compleat&page=lib_prin.html

"If you write an * instead of a decimal number for a precision, a
print function takes the value of the next argument (which must be
of type int) as the precision."

--
"When you are having a bad day and it seems like everybody is trying
to piss you off, remember that it takes 42 muscles to produce a
frown, but only 4 muscles to work the trigger of a good sniper
rifle."

and the term precision for strings means the maximum number of
characters to be printed.
so here it means print at most 'bytes' number of characters from the
string 'sData'.
Mohan
 
D

david

from:www.dinkumware.com/manuals/?manual=compleat&page=lib_prin.html

"If you write an * instead of a decimal number for a precision, a
print function takes the value of the next argument (which must be
of type int) as the precision."

--
  "When you are having a bad day and it seems like everybody is trying
   to piss you off, remember that it takes 42 muscles to produce a
   frown, but only 4 muscles to work the trigger of a good sniper
rifle."

Thanks
 
M

Matrixinline

Matrixinline said:





If by "EOF bit" you mean a special bit (binary digit) embedded in
the file that represents its end, there is no such bit. If you
think EOF is a byte, well, there's no such byte. (The waters are
muddied by old CP/M text files, but the so-called EOF byte they
write is just a byte, which a C program can read and process just
like any other, and the only significance of which is the
significance that an implementation, or a program, chooses to place
upon it.)

EOF is a *condition*, a *state*, a report by the C library that an
attempt to read data failed because there were no more data to read
- in other words, that the end of the file has been reached. If you
write past that point (which is fine), clearly the amount of data
in the file increases, so there will be more data to read on a
subsequent read-pass.

It would be helpful, I think, to know the question behind the
question. What high-level operation are you trying to achieve that
you think requires writing "after" EOF and which you distinguish
from writing at the end of the file?

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999

Thanks Richard Heathfield, your post helps a LOT :)
 
J

James Kuyper

david said:
Hi Richard, what's the meaning of "%.*s" ?

The * causes the value of the corresponding variable argument to
printf() to be used as if it were a number in the format specifier. In
this case, if bytes is 5, then

printf("%.*s\n", bytes, sData);

would behave the same as

printf("%.5s\n", sData);

The '5' in the "%.5s" specifier limits it to printing no more than 5
bytes. It may print fewer if it runs into a '\0' character first, but it
will never print more.
 
B

Ben Bacarisse

Richard Heathfield said:
Matrixinline said:

When a function returns EOF, it means it couldn't read any more data
- so that's going to stop you reading data, right?

Nit: I think you can read data after you encounter EOF. If it were
not possible, why would clearerr clear the end-of-file indicator? I
don't think that the C standard ensures that it is always possible,
but I don't think an implementation is wrong to permit it. You
certainly can do so on my system.
 
A

Antoninus Twink

I was actually aware of this (I claim no special knowledge - there was
a thread on it a few aeons ago, which I found enlightening), but on
this occasion I decided, in the interests of simplicity, to keep
things simple. :)

Here we see the famous Heathfield humility - why not just accept that
you were wrong, plain old wrong, completely and utterly wrong, instead
of trying to pretend that you "knew really but didn't want to overwhelm
the OP with the magnitude of your greatness"? It's pathetic.

Let me enlighten you, Heathfield. Try this out.

$ cat a.c
#include <stdio.h>
#include <unistd.h>

int main(void)
{
int c, i = 0, j;
FILE *fp = fopen("foo", "r");
while( (c = fgetc(fp)) != EOF) {
i++;
}
j = i;
sleep(5);
while( (c = fgetc(fp)) != EOF) {
j++;
}
printf("\rtime A: %d characters\ntime B: %d characters\n", i, j);
return 0;
}
$ echo hello > foo ; ./a & sleep 2 ; echo world >> foo
[1] 12426
time A: 6 characters
time B: 12 characters
 
R

Richard Tobin

Ben Bacarisse said:
Nit: I think you can read data after you encounter EOF. If it were
not possible, why would clearerr clear the end-of-file indicator?

And it was quite a common thing to do with terminals - the program
prompts for some input, the user types it and then presses ^Z or
whatever, the program gets EOF and does clearerr(), and later it
prompts for some more input.

Now that almost every program has a graphical interface whether it
needs it or not, it's less often used.

-- Richard
 
L

lawrence.jones

Antoninus Twink said:
Let me enlighten you, Heathfield. Try this out.

$ cat a.c
#include <stdio.h>
#include <unistd.h>

int main(void)
{
int c, i = 0, j;
FILE *fp = fopen("foo", "r");
while( (c = fgetc(fp)) != EOF) {
i++;
}
j = i;
sleep(5);
while( (c = fgetc(fp)) != EOF) {
j++;
}
printf("\rtime A: %d characters\ntime B: %d characters\n", i, j);
return 0;
}
$ echo hello > foo ; ./a & sleep 2 ; echo world >> foo
[1] 12426
time A: 6 characters
time B: 12 characters

You have a defective implementation. A correct C implementation gives:
[1] 17610
time A: 6 characters
time B: 6 characters

You need a call to clearerr(), fseek(), fsetpos(), or ungetc() between
the two loops to clear the end-of-file indicator for the stream before
trying to read more data.

(And novices should note that <unistd.h> is not a standard C header and
sleep() is not a standard C function.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top