ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
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]
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
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...
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.