[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Using fgets on memory stream
From: |
Eray Ozkural (exa) |
Subject: |
Using fgets on memory stream |
Date: |
Tue, 31 Oct 2000 01:13:02 +0200 |
If I use a file stream instead of a memory stream, the
behavior is completely different. The code is quite
important for a little hack I'm working on so help
would be appreciated.
I read the libc info documentation on I/O several times to
see if I was wrong but AFAICT the problem is not with my
code because according to the documentation this should work.
If it worked to my expectations, this wouldn't be the case
when I ran the two test programs:
[I expect it to behave the same for file and mem streams]
orion:libc$ ./filestream
Read: testorion:libc$
orion:libc$ ./memstream
read error!
orion:libc$ cat testfile
testorion:libc$
As you can see, it isn't able to print anything at all
when I use fmemopen :'(
I'm attaching the source code for both .c files. The
filestream.c is derived from memstream.c, using the
file "testfile" instead of the memory stream. "testfile"
contains the characters "test", no newline at the end.
I suspect that the EOF and error indicators are not
updated correctly in the case of stream objects created
by fmemopen.
I'm using glibc 2.1.95 in Debian GNU/Linux i386 unstable (woody)
distribution. For your reference, here is the thread I've first
reported this in:
http://lists.debian.org/debian-devel-0010/msg02039.html
Please test this with the files attached.
Thanks,
--
Eray (exa) Ozkural
Comp. Sci. Dept., Bilkent University, Ankara
e-mail: address@hidden
www: http://www.cs.bilkent.edu.tr/~erayo
#include <stdio.h>
void main()
{
char *str = "test";
char line[80];
FILE *stream;
stream = fmemopen(str, strlen(str), "r");
while (!feof(stream)) {
if (!fgets(line, 80, stream)) {
printf("read error!\n");
exit(-1);
}
printf("Read: %s", line);
}
fclose(stream);
}
#include <stdio.h>
void main()
{
char line[80];
FILE *stream;
stream = fopen("testfile", "r");
while (!feof(stream)) {
if (!fgets(line, 80, stream)) {
printf("read error!\n");
exit(-1);
}
printf("Read: %s", line);
}
fclose(stream);
}
test
- Using fgets on memory stream,
Eray Ozkural (exa) <=