Unexpected Output when reading a files using read()

S

Sanchit

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 = 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
 
M

maxim.pgv

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

Try to clear your buffer:

memset( buf, 0, sizeof(buf) );
 
M

Morris Dovey

Sanchit said:
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 = 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ÂfôÃ?ú·žÂ-Ã~±¬¿)Â?

I am unable to understand why these special symbols are coming

The %s specification in printf() caused printing of the data at
buf until a '\0' was found. What do you suppose would have
happened if no terminator at all had been found?
 
R

Richard Heathfield

Sanchit said:
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 = open("marks.dat", O_RDONLY);
/* read the data */
nread = read(fd, buf, 1024);

I'm assuming that your read() call is to the POSIX function of that name,
in which case you're trying to read up to 1024 bytes into a 100-byte
buffer. Okay, so your file is less than 100 characters long, so it's not
an immediate problem - but it's bound to bite you eventually. Note,
however, that your file doesn't contain any null characters, and you
haven't put any null characters into buf...
/*close the file */

printf("%s\n\n",buf);

....and yet you tell printf that buf is a string (i.e. a sequence of
characters terminated by the first null character). So it starts printing
characters from the address specified by &buf[0] (or buf, for short), and
keeps going until it finds a null character. It isn't going to stop just
because it runs off the end of buf.

Solution: change your definition to:

char buf[100] = {0};

Exercise: do some research into *why* this is a solution.

Alternative solution: after the read() call, test nread to see whether the
call worked. If so, and if nread is < 100, you can just do this:

buf[nread] = {0};
 
B

Ben Bacarisse

Richard Heathfield said:
Sanchit said:
Alternative solution: after the read() call, test nread to see whether the
call worked. If so, and if nread is < 100, you can just do this:

buf[nread] = {0};

Typo: you meant

buf[nread] = 0;

(or maybe = '\0').

To the OP: in addition to what has been said, you must include stdio.h
to make the use of printf well-defined.
 
J

jacob navia

Richard said:
Ben Bacarisse said:
Richard Heathfield said:
[...] if nread is < 100, you can just do this:

buf[nread] = {0};
Typo: you meant

buf[nread] = 0;

(or maybe = '\0').
Ta.

To the OP: in addition to what has been said, you must include stdio.h
to make the use of printf well-defined.

Ooooh, ta twicely.

Yeah. Always so fast to see a missing
'L'
when writing an integer constant, then blaming people for that.

Note: You should read your code before you post!
 
R

Richard Heathfield

Ben Bacarisse said:
Richard Heathfield said:
[...] if nread is < 100, you can just do this:

buf[nread] = {0};

Typo: you meant

buf[nread] = 0;

(or maybe = '\0').
Ta.

To the OP: in addition to what has been said, you must include stdio.h
to make the use of printf well-defined.

Ooooh, ta twicely.
 
R

Richard Heathfield

jacob navia said:
Richard said:
Ben Bacarisse said:
[...] if nread is < 100, you can just do this:

buf[nread] = {0};
Typo: you meant

buf[nread] = 0;

(or maybe = '\0').
Ta.

To the OP: in addition to what has been said, you must include stdio.h
to make the use of printf well-defined.

Ooooh, ta twicely.

Yeah. Always so fast to see a missing
'L'
when writing an integer constant, then blaming people for that.

I don't know what you're talking about. I haven't blamed anyone for missing
an L suffix.
Note: You should read your code before you post!

Well, I do - I read it while I'm writing it. But I don't claim to be
perfect, and I do make mistakes. When that happens, I fess up. It is a
strategy that I recommend highly.
 
K

Keith Thompson

Sanchit said:
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 = open("marks.dat", O_RDONLY);
/* read the data */
nread = read(fd, buf, 1024);
/*close the file */

printf("%s\n\n",buf);
close(fd);
}
[snip]

In addition to the other comments (attempting to read 1024 bytes into
a 100-byte buffer, printing with "%s" something that isn't a string,
and the missing "#include <stdio.h>"), you might consider using the
standard C functions (fopen, fread, fclose) rather than the
POSIX-specific ones you're using (open, read, close). There are times
when the POSIX functions have advantages over the corresponding
standard C functions, but I don't see any indication that this is one
of those times.
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top