473,396 Members | 2,017 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Unexpected Output when reading a files using read()

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
Mar 21 '08 #1
7 1520
Sanchit 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 = 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÷·ž-ž±¬¿Xfô?ú·ž-~±¬¿)?

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?

--
Morris Dovey
DeSoto Solar
DeSoto, Iowa USA
http://www.iedu.com/DeSoto/
Mar 21 '08 #2
On 21 мар, 12:49, Sanchit <sanchitgupt...@gmail.comwrote:
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) );
Mar 21 '08 #3
Richard Heathfield <rj*@see.sig.invalidwrites:
Sanchit said:
>My program is

#include <fcntl.h>
#include <unistd.h>
<snip>
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.

--
Ben.
Mar 21 '08 #4
Ben Bacarisse said:
Richard Heathfield <rj*@see.sig.invalidwrites:
>[...] 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.

--
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
Mar 21 '08 #5
Richard Heathfield wrote:
Ben Bacarisse said:
>Richard Heathfield <rj*@see.sig.invalidwrites:
>>[...] 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!
--
jacob navia
jacob at jacob point remcomp point fr
logiciels/informatique
http://www.cs.virginia.edu/~lcc-win32
Mar 21 '08 #6
jacob navia said:
Richard Heathfield wrote:
>Ben Bacarisse said:
>>Richard Heathfield <rj*@see.sig.invalidwrites:

[...] 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.

--
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
Mar 21 '08 #7
Sanchit <sa************@gmail.comwrites:
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.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 22 '08 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of...
13
by: Nigel J. Andrews | last post by:
This will be a little vague, it was last night and I can't now do the test in that db (see below) so can't give the exact wording. I seem to remember a report a little while ago about tsearch v2...
5
by: Tom Lam lemontea | last post by:
Hi all, This is my very first post here, I've seriously tried some programming on C, and shown below is my very first program(So you can expect it to be very messy) that I wrote after I've learned...
3
by: Brad | last post by:
I'm working on a web app which will display LARGE tiff image files (e.g files 10-20+ mb). Files are hidden from users direct access. For other, smaller image files I have used FileStream to read...
8
by: D & J G | last post by:
A VB6 program that I created has been installed on a wide range of computers and networks without any problems. Win 95, Win 98 and XP so far.. Occasionally, however, the function refuses to start...
3
by: Alan | last post by:
Dear All, I'm trying to write a C++ that reads in hexadecimal characters from a text file. For this I use the >> to read in a character at a time inside a while loop that waits for eof to be...
3
by: Anup Daware | last post by:
Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read half of the xml and suddenly an exception:...
1
by: dwaterpolo | last post by:
Hi Everyone, I am trying to read two text files swY40p10t3ctw45.col.txt and solution.txt and compare them, the first text file has a bunch of values listed like: y y y y y y y
3
by: nvr | last post by:
Hi all I am doing the socket programming for the client side. but the code is not compiling and i am getting the below error ./Clientsend.c: line 11: syntax error near unexpected token `('...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.