Unexpected Output when reading a files using read() - Unix
This is a discussion on Unexpected Output when reading a files using read() - Unix ; My program is
#include
#include
int main(void)
{
int fd;
ssize_t nread;
char buf[100];
/*open file for reading */
fd = open("marks.dat", O_RDONLY);
/* read the data */
nread = read(fd, buf, 1024);
/*close the file */
printf("%s\n\n",buf);
close(fd);
}
...
-
Unexpected Output when reading a files using read()
My program is
#include
#include
int main(void)
{
int fd;
ssize_t nread;
char buf[100];
/*open file for reading */
fd = open("marks.dat", O_RDONLY);
/* read the data */
nread = read(fd, buf, 1024);
/*close the file */
printf("%s\n\n",buf);
close(fd);
}
And my marks.dat is
123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d(HERE FILE
ENDS)
Output is
123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d
Ȭ¿^äì·9÷·žž±¬¿XôÏú·ž ر¬¿)
I am unable to understand why these special symbols are coming
-
Re: Unexpected Output when reading a files using read()
Sanchit writes:
>And my marks.dat is
>123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d(HERE FILE
>ENDS)
>Output is
>123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d
>=C3=88=C2=AC=C2=BF^=C3=A4=C3=AC=C2=B79=19=C3=B7=C2 =B7=C5=BE=C2=96=04=08=C5=
>=BE=C2=B1=C2=AC=C2=BFX=C2=83=04=08=C3=B4=C3=8F=C3= BA=C2=B7=C5=BE=C2=96=04=08=
>=C3=98=C2=B1=C2=AC=C2=BF)=C2=85=04=08
>I am unable to understand why these special symbols are coming
Does the file end with a NULL byte, or that final 'd' ?
When printing 'buf', you're assuming it to contain a NULL terminated string.
--
Chris.
-
Re: Unexpected Output when reading a files using read()
On Mar 21, 9:50 am, Sanchit wrote:
> My program is
>
> #include
> #include
> int main(void)
> {
> int fd;
> ssize_t nread;
> char buf[100];
> /*open file for reading */
> fd = open("marks.dat", O_RDONLY);
> /* read the data */
> nread = read(fd, buf, 1024);
> /*close the file */
>
> printf("%s\n\n",buf);
> close(fd);
>
> }
>
> And my marks.dat is
> 123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d(HERE FILE
> ENDS)
>
> Output is
> 123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d
> Ȭ^9 X ر)
>
> I am unable to understand why these special symbols are coming
Make the 3rd argument to read() 99 to prevent an error when the
file is larger, and set buf[ nread ] = '\0' to terminate the
string in buf. (The 'special symbols' are the data after
buf until there happens to be a '\0'.)
-
Re: Unexpected Output when reading a files using read()
In <8bec21ee-6b2e-4092-b144-c5fb86ec95f3@s12g2000prg.googlegroups.com> Sanchit writes:
> My program is
> #include
> #include
> int main(void)
> {
> int fd;
> ssize_t nread;
> char buf[100];
> /*open file for reading */
> fd =3D open("marks.dat", O_RDONLY);
> /* read the data */
> nread =3D read(fd, buf, 1024);
> /*close the file */
> printf("%s\n\n",buf);
> close(fd);
> }
> And my marks.dat is
> 123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d(HERE FILE
> ENDS)
> Output is
> 123 123 123 4 56 sas as fd faa a s d sf d!!f d fd f df df d
> =C3=88=C2=AC=C2=BF^=C3=A4=C3=AC=C2=B79=19=C3=B7=C2 =B7=C5=BE=C2=96=04=08=C5=
> =BE=C2=B1=C2=AC=C2=BFX=C2=83=04=08=C3=B4=C3=8F=C3= BA=C2=B7=C5=BE=C2=96=04=08=
> =C3=98=C2=B1=C2=AC=C2=BF)=C2=85=04=08
> I am unable to understand why these special symbols are coming
When you call fread(), it inserts the characters from the file into buf.
That's ALL it does. It does NOT insert a final terminating null character.
When you call printf(), it expects buf to be a null-terminated string.
It keeps printing characters until it happens to encounter a null,
which in your case is quite a ways past where you expected it to be.
To fix your problem, either insert a null character into buf in the
appropriate spot, or use fwrite() instead of printf().
--
John Gordon A is for Amy, who fell down the stairs
gordon@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"