LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-16-2006, 06:45 PM   #1
sahel
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Rep: Reputation: 15
Question reading from file


hi
i want to read the contents of a file and show it on screen
i have this code but it doesn't work.can you help me.

char buffertest[1000]="";
char msg[1000]="";
int out;
if(out=open(path4,O_RDONLY)==-1)
{
perror("Error opening file");
}

else {
while(numread=read(out,buffertest,1)>0)
{
strcat(msg,buffertest);
}
puts(msg);

}


The file in path4 will be opened but nothing will be read
 
Old 01-16-2006, 08:18 PM   #2
FLLinux
Member
 
Registered: Jul 2004
Location: USA
Distribution: Fedora 9, LFS 6.3, Unbuntu 8.04, Slax 6.0.7
Posts: 145

Rep: Reputation: 15
Just looking at the code i would think nothing is printed out since the printing is inside the else of the if statement. do something like thischar

buffertest[1000]="";
char msg[1000]="";
int out;
bool fileOpen = true;
if(out=open(path4,O_RDONLY)==-1)
{
perror("Error opening file");
fileOpen = false;
}

if(fileOpen)
{
while(numread=read(out,buffertest,1)>0)
{
strcat(msg,buffertest);
}
puts(msg);
}

That should allow the while loop to run if the file is opened.
 
Old 01-16-2006, 08:49 PM   #3
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
I went ahead and copied your code, and tried it myself. The results were quite surprising; i got the same results as you, though i could see nothing wrong with the code. I noticed that the file handle 'out' was being set to zero, which was causing input to be taken from stdin! So i tried changing the open statement:

Code:
if(out=open(path4,O_RDONLY)==-1)
which i changed to two lines:

Code:
 
out=open(path4,O_RDONLY); 
if(out==-1)
this gave me a valid file handle, and the logic worked fine!

Maybe a gcc bug?

Let me know if this fixes things on your system.
 
Old 01-17-2006, 08:17 AM   #4
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
here is the bug in assinging and checking

Quote:
Originally Posted by sahel
if(out=open(path4,O_RDONLY)==-1)
{
perror("Error opening file");
}
try with,
Code:
if( (out=open(path4,O_RDONLY)) ==-1)
{
perror("Error opening file");
}
value returned by the open function after assigning to out variable and then only should be checked for error (-1)
 
Old 01-17-2006, 08:19 AM   #5
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by dogpatch
Code:
if(out=open(path4,O_RDONLY)==-1)
which i changed to two lines:
Code:
 
out=open(path4,O_RDONLY); 
if(out==-1)
you neednot change the above lines to two (there is nothing wrong in that actually)

this would suffice !!!

Code:
if( (out=open("dum",O_RDONLY)) ==-1)
 
Old 01-17-2006, 11:42 AM   #6
sahel
LQ Newbie
 
Registered: Dec 2005
Posts: 21

Original Poster
Rep: Reputation: 15
actually the problem is not with opening the file but with reading from file.The file will be opend and the value of out will be 0 it goes into else part but it doeant show the contents of the file.Is my read command right or not?
 
Old 01-17-2006, 11:52 AM   #7
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
Quote:
Originally Posted by sahel
actually the problem is not with opening the file but with reading from file.The file will be opend and the value of out will be 0 it goes into else part but it doeant show the contents of the file.Is my read command right or not?
Not quite true, since what is returned from the open function is the file descriptor. This is then passed into the read function. However if (as others have pointed out) this is not being set up correctly then the read will fail because it is trying to read from an invalid file descriptor.

I suggest that you change the code of the open as suggested and you should then have a valid fd and will then be able to read the file.

graeme.
 
Old 01-17-2006, 04:11 PM   #8
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
Breaking the if statement into two lines, or enclosing the file handle assignment within quotes in one line ends up achieving the same end. The problem with the original if clause was not a gcc bug as i first thought, but it's just how most C compilers evaluate any expression: from right to left. So the right-most part of the original if clause
Code:
open(path4,O_RDONLY))==-1
evaluates to false or zero, since any valid file handle returned by open() will be a positive integer. The result of this evaluation, which is zero, is then assigned to the variable 'out' in the left part. You don't want the file handle to be zero; that's the file handle for stdin (the keyboard)!

Enclosing the left-hand part in parenthesis forces that part to be evaluated first, which assigns a valid file handle to 'out'.
Code:
if((out=open(path4,O_RDONLY))==-1)
Breaking into two lines does the same thing
Code:
 

out=open(path4,O_RDONLY); 
if(out==-1)

Last edited by dogpatch; 01-17-2006 at 04:20 PM.
 
Old 01-17-2006, 04:30 PM   #9
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
what programming language is this ?
 
Old 01-17-2006, 04:43 PM   #10
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
It looks like C to me but I guess it could be something else...
 
Old 01-17-2006, 05:31 PM   #11
exvor
Senior Member
 
Registered: Jul 2004
Location: Phoenix, Arizona
Distribution: Gentoo, LFS, Debian,Ubuntu
Posts: 1,537

Rep: Reputation: 87
what i was thinking but lack of a stream anywhere confused me. I see now there using the open command rather then the fopen command.
open is a low level c command for file manipulation it returns a file descriptor. as far as me im not famillar with using it really i always use fopen command cause its eazier to work with.
 
Old 01-17-2006, 11:10 PM   #12
kshkid
Member
 
Registered: Dec 2005
Distribution: RHEL3, FC3
Posts: 383

Rep: Reputation: 30
Quote:
Originally Posted by exvor
what i was thinking but lack of a stream anywhere confused me. I see now there using the open command rather then the fopen command.
open is a low level c command for file manipulation it returns a file descriptor. as far as me im not famillar with using it really i always use fopen command cause its eazier to work with.
fopen is std I/O routine which internally makes use of open system call when std I/O buffers get filled.

Rather a write system call associated with open call flushes the buffer each time a write is made.

Hence for frequent writes with less no of characters - fopen and fwrite would suit to.
For less no of writes and bulk characters open and write would fit in.

But still usage depends upon the write frequency, application criticality, integrity needed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
QT file reading example fuzzhead85 Programming 7 06-24-2012 02:24 PM
awk: fatal:cannot open file for reading (no such file or Directory) in Linux sangati vishwanath Linux - Software 4 07-06-2005 12:59 AM
Log file reading Garak Linux - General 5 07-08-2003 06:55 PM
file reading bartgymnast Linux - Software 1 02-26-2003 03:34 AM
Script, Reading a file, When end of file? elibm Programming 2 07-16-2001 11:01 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:11 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