Home > Archive > Unix Programming > March 2008 > Unexpected Output when reading a files using read()
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Unexpected Output when reading a files using read()
|
|
| Sanchit 2008-03-21, 8:12 am |
| My program is
#include <fcntl.h>
#include <unistd.h>
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=1
9=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
| |
| Chris McDonald 2008-03-21, 8:12 am |
| Sanchit <sanchitgupta.1@gmail.com> 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=1
9=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.
| |
| William Pursell 2008-03-21, 7:19 pm |
| On Mar 21, 9:50 am, Sanchit <sanchitgupt...@gmail.com> wrote:
> My program is
>
> #include <fcntl.h>
> #include <unistd.h>
> 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
> =C8=AC=BF^=E4=EC=B79 =F7=B7=9E=96 =9E=B1=AC=BFX=83 =F4=CF=FA=B7=9E=96 =
=D8=B1=AC=BF)=85
>
> 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 ] =3D '\0' to terminate the
string in buf. (The 'special symbols' are the data after
buf until there happens to be a '\0'.)
| |
| John Gordon 2008-03-21, 7:19 pm |
| In <8bec21ee-6b2e-4092-b144-c5fb86ec95f3@s12g2000prg.googlegroups.com> Sanchit <sanchitgupta.1@gmail.com> writes:
> My program is
> #include <fcntl.h>
> #include <unistd.h>
> 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=1
9=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"
|
|
|
|
|