LinuxQuestions.org
Help answer threads with 0 replies.
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 11-06-2002, 10:28 AM   #1
Y_Haarman
LQ Newbie
 
Registered: Nov 2002
Posts: 2

Rep: Reputation: 0
[C & Linux] Read line from text-file


The following code is supposed to read a text file and prints it in the console:

==============================

char *myfile = "test.txt";
int fd;
char line[300];
char ch1;

if(fd = open(myfile, O_RDONLY )== -1 )
{
printf("File open error.\n");
exit(-1);
}

do
{
ch1 = read(fd, &line, 300);
printf("%c\n", line);

} while (ch1 > 0);

close(fd);

==============================

When I execute it, it looks like the "Read" function reads a line from the console. It asks for input but doesn't show anything. Is their a problem in the code maybe?

[BTW: I don't want to (may not) use stdio.h for file operations]
 
Old 11-06-2002, 11:16 AM   #2
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Firstly, read(...) doesn't read a line, it reads however many characters you ask it to (in your case 300). Presumably your file contains less than 300 characters, and so read never returns.

Secondly, the variable line in your program is a character string, so you need to use %s to print it, not %c. Also, you haven't terminated line with a 0 ('\0'), so printf will segfault. read does not put a 0 on the end for you

Hope that all helps
Alex
 
Old 11-06-2002, 11:25 AM   #3
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: ubuntu
Posts: 2,530

Rep: Reputation: 108Reputation: 108
... and the line:

ch1 = read(fd, &line, 300);

should read:

ch1 = read(fd, line, 300);

without the '&' for the reason Alex mentioned.
 
Old 11-06-2002, 11:58 AM   #4
Y_Haarman
LQ Newbie
 
Registered: Nov 2002
Posts: 2

Original Poster
Rep: Reputation: 0
Thanks for the replys.

So read doesn't read a line. Is it still smart to use it? Is their a systemcall similar to fgets() in stdio.h?
 
Old 11-06-2002, 01:33 PM   #5
llama_meme
Member
 
Registered: Nov 2001
Location: London, England
Distribution: Gentoo, FreeBSD
Posts: 590

Rep: Reputation: 30
Quote:
So read doesn't read a line. Is it still smart to use it? Is their a systemcall similar to fgets() in stdio.h?
I'm not sure, but I doubt it. Remember, these functions are supposed to be low level functions for reading and writing streams of bytes (be they from a binary or ASCII file, TCP/IP packets, whatever...) Reading a line from an ASCII file is actually quite a specialised operation in this context, and I doubt anybody would want to bloat the kernel by writing a system call to do it.

Why can't you use stdio.h? Unless you're writing a kernel module or something I don't see why you shouldn't...

Alex
 
Old 11-08-2002, 05:44 PM   #6
M3xican
LQ Newbie
 
Registered: Nov 2002
Location: Naples, Italy
Distribution: Slackware
Posts: 14

Rep: Reputation: 0
Lightbulb I/O & files

Quote:
Originally posted by llama_meme
Firstly, read(...) doesn't read a line, it reads however many characters you ask it to (in your case 300). Presumably your file contains less than 300 characters, and so read never returns.
Read doesn't return???????????

from the man page of read()
Quote:
On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number.
Now u know that read returns

-----------------------------------------------------------

The best way to use I/O files is using buffers, there are a lot of technical reasons, but
I think that now u have to know only that this is the fast way.
A typical code that could be used to manage files looks like this:
Code:
#include .....
#include .....
......

#define DIM_BUF 4096

int main()
{
        ......
        char buf[DIM_BUF];
        int lung;

        fd=open(file,O_RDONLY);

        while(lung=read(fd,buf,DIM_BUF))
        {
                ....
                code that manages the data
                (in example: write(STDOU_FILENO,buf,lung); )
                ....
        }

        ......

        close(fd);

        return 0;
}
I have omitted an error management because this is only an example.
 
  


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
bash read a given line number from text bendeco13 Programming 7 08-31-2012 04:49 PM
C++ text file line by line/each line to string/array Dimitris Programming 15 03-11-2008 09:22 AM
linux scripting help needed read from file line by line exc commands each line read atokad Programming 4 12-26-2003 11:24 PM
Display/Read line 'N' in a text file using script ganninu Linux - Newbie 2 10-13-2003 06:28 AM
trying to search and replace text file for single & multiple line breaks separately brokenfeet Programming 7 08-29-2003 02:56 PM


All times are GMT -5. The time now is 08:52 AM.

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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration