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.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
any process has three standard descriptor:0,1 and 2. for example the printf() library function always sends its output using file descriptor 1 which display screen
in my code I'm trying to read a file and display it without using the C library function. I use the write system call to write the file to the screen
Code:
int fd1 = open(argv[c], O_RDONLY);
int n,i;
char buffer[SIZE];
while ((n=read(fd1, buffer, SIZE)) != 0) {
for (i=0; i<n ; i++)
write(stdout , buffer[i], sizeof(buffer));
}
I want to take it line by line and display it. So, What am I doing? why there is no output ?
---------------------------------------------------------------------------------------------------------------------------
int fd1 = open(argv[c], O_RDONLY);
int n,i;
char buffer[SIZE];
while ((n=read(fd1, buffer, SIZE)) != 0) {
for (i=0; i<n ; i++)
write(stdout , buffer[i], sizeof(buffer));
}
n Apr 8 19:37:14 EDT 2001i586ccse)}@ô÷ÿ¿¸U@0ùÿ¿lZ@÷ÿ¿["úÿ¿¬"@÷ÿ¿/"
----------------------------------------------------------------------------------------------------------------------------
and in some file it display correctly and it display part of again, I don't why
is it the size?
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
I already solve it
Not sure you solved it, as you didn't change your code.
You seem to have redefined stdout as an int equal to 1 which is a very bad initiative ...
here is something that should work:
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
what do you mean by "bad initiative"?
Bad initiative because, as perfect_circle already pointed out, stdout is already defined as a pointer (i.e. the address of something) while the write call is expecting a file descriptor which is an integer.
If you redeclare well known variables to mean something different, your code is going to be unreadable.
perfect_circle already replied about how to output one character after another, which is also, sorry to tell, a very bad initiative, unless you are competing for the slowest program contest ...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.