LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu
User Name
Password
Ubuntu This forum is for the discussion of Ubuntu Linux.

Notices


Reply
  Search this Thread
Old 11-30-2017, 10:08 PM   #1
gursewaks056
LQ Newbie
 
Registered: Sep 2017
Location: Chandigarh
Distribution: Ubuntu 16.04 LTS
Posts: 13

Rep: Reputation: Disabled
read() doesn't read properly


When I use read() function for reading from file then it read the text but also print 2 or 3 unwanted characters.

for example :
file1.txt : My name is John. This is file contents.

coding : char buff[10]; int f; f=open("file1.txt",O_RDONLY);
fread(f,buff,5); printf("%s\n",buff);

output : My na$%#
 
Old 11-30-2017, 10:20 PM   #2
giis
Member
 
Registered: Nov 2013
Location: Third Rock from Moon
Distribution: RPM/DEB based and LFS
Posts: 73

Rep: Reputation: Disabled
That's because your buf size 10 and you read only 5 chars.

And also you are not reading entire file content. Just reading first 5 bytes of it. I don't think thats your requirement.
 
Old 11-30-2017, 11:09 PM   #3
gursewaks056
LQ Newbie
 
Registered: Sep 2017
Location: Chandigarh
Distribution: Ubuntu 16.04 LTS
Posts: 13

Original Poster
Rep: Reputation: Disabled
Not work

If I used my total buffer size to read then also it does same thing. Thanks for reply.
 
Old 11-30-2017, 11:32 PM   #4
giis
Member
 
Registered: Nov 2013
Location: Third Rock from Moon
Distribution: RPM/DEB based and LFS
Posts: 73

Rep: Reputation: Disabled
Quote:
Originally Posted by gursewaks056 View Post
If I used my total buffer size to read then also it does same thing.
Yes, Just cleaning your buffer won't solve the problem.

Can you explain whats going on here:

Quote:
fread(f,buff,5);
in plain english?
 
Old 12-01-2017, 06:49 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 25,679

Rep: Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892Reputation: 5892
printf("%s\n",buff) will output the contents buff up to but not including a NULL character. Any printable character that is in buff between the 5th element and a NULL character will be displayed.

You can add a NULL (0) character to buff or only print 5 characters. Here are a few examples.
Code:
   buff[5]=0;
   printf("%s\n", buff); // will print to the first NULL character
   printf("%.5s\n",buff);  // Will print first 5 characters
   length=5;
   printf("%.*s\n", length, buff);  // will print number of characters defined by length

Last edited by michaelk; 12-01-2017 at 06:51 AM.
 
Old 12-01-2017, 09:04 AM   #6
gursewaks056
LQ Newbie
 
Registered: Sep 2017
Location: Chandigarh
Distribution: Ubuntu 16.04 LTS
Posts: 13

Original Poster
Rep: Reputation: Disabled
Its meaning.

Quote:
Originally Posted by giis View Post
Yes, Just cleaning your buffer won't solve the problem.

Can you explain whats going on here:



in plain english?
Oh! Spelling mistake is here. I wanted to use read() function as that place of fread(). read(f,buff,5); statement reads first 5 characters from the file opened by f descriptor.
 
Old 12-01-2017, 10:23 PM   #7
rhubarbdog
Member
 
Registered: Apr 2015
Location: Yorkshire, England
Distribution: Linux Mint
Posts: 145

Rep: Reputation: Disabled
The %s format takes a nul terminated string you need to do the following
Code:
char buf[10];
  f=open("file.txt",O_RDONLY);
  ok=fead(f,buf,5);
  if (ok<0) buf[0]='\0';
  else buf[ok]='\0';
  printf("%s\n",buf);
Note you have to nul terminate all strings explicitly. And have to allow space for it, that is buf[10] gives 10 character positions one of which has to be the nul byte leaving ipto 9 for data

Last edited by rhubarbdog; 12-01-2017 at 10:30 PM.
 
1 members found this post helpful.
Old 12-04-2017, 04:48 AM   #8
giis
Member
 
Registered: Nov 2013
Location: Third Rock from Moon
Distribution: RPM/DEB based and LFS
Posts: 73

Rep: Reputation: Disabled
Here's simple char-char version with file operation using sys.call directly:

Quote:
#include <stdio.h>

int main(){
char c;
int fp,ret=0;

fp=open("/tmp/f1.txt",0);
if (fp < 0){
printf("file open error");
return -1;
}

ret=read(fp,&c,1);
while(ret > 0){
printf("%c",c);
ret = read(fp,&c,1);
}
close(fp);
}
HTH
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
ksh: while read doesn't get to read more than one variable eantoranz Linux - Software 1 07-24-2009 03:33 PM
why file is not read properly here? cranium2004 Programming 1 03-17-2005 08:26 AM
can't install fedora, my computer doesn't read the cd properly genie123456 Linux - Software 3 10-27-2004 07:30 PM
Example share in smb.conf doesn't work - read/write vs read only kleptophobiac Linux - Networking 0 09-01-2004 07:14 PM
cds don't read properly i_is_cat Linux - Hardware 6 08-09-2003 11:32 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Ubuntu

All times are GMT -5. The time now is 10:36 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration