LinuxQuestions.org
Share your knowledge at the LQ Wiki.
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-30-2004, 09:47 AM   #1
ej25
Member
 
Registered: Nov 2004
Posts: 39

Rep: Reputation: 15
using system call


hi

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 ?
 
Old 11-30-2004, 10:04 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,774

Rep: Reputation: 469Reputation: 469Reputation: 469Reputation: 469Reputation: 469
write(fileno(stdout), ...)
or
write(1,...)
 
Old 11-30-2004, 10:32 AM   #3
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
thanks jlliagre

I already solve it

but it give me this

---------------------------------------------------------------------------------------------------------------------------
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@0lZ@[""@/"
----------------------------------------------------------------------------------------------------------------------------
and in some file it display correctly and it display part of again, I don't why
is it the size?
 
Old 11-30-2004, 10:48 AM   #4
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
stdout, stdin etc are stream descriptors (FILE *)



Quote:
I'm trying to read a file and display it without using the C library function
it's probably non of my bussiness but why are you doing this?
 
Old 11-30-2004, 10:58 AM   #5
perfect_circle
Senior Member
 
Registered: Oct 2004
Location: Athens, Greece
Distribution: Slackware, arch
Posts: 1,783

Rep: Reputation: 53
Code:
 write(stdout , buffer[i], sizeof(buffer));
first of all the file descripor for stdout is 1 (i think)

2. use sizeof(char)
sizeof(buffer) is SIZE*sizeof(char), and on every write() u write only buffer[i] which is a character not a string

instead of
Code:
 for (i=0; i<n ; i++)
 write(1 , buffer[i], sizeof(char));
you may also try
Code:
write(1, buffer, n)
I think this should work, but i'm not sure.
you may try it

Last edited by perfect_circle; 11-30-2004 at 11:16 AM.
 
Old 11-30-2004, 10:58 AM   #6
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
Quote:
it's probably non of my bussiness but why are you doing this?

I solve it in regular way. But I want to do it in the hard way to learn better and to minimize my code

Is there a way to take it character by character and display each character alone like this:

Code:
while ((n=read(fd1, buffer, SIZE)) != 0) {
for (i=0; i<n ; i++)
		 write(1, buffer[i], sizeof(buffer));
	}
 
Old 11-30-2004, 10:59 AM   #7
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,774

Rep: Reputation: 469Reputation: 469Reputation: 469Reputation: 469Reputation: 469
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:
Code:
while ((n=read(fd1, buffer, SIZE)) != 0)
{
  write(1 , buffer, n);
}

Last edited by jlliagre; 11-30-2004 at 11:24 AM.
 
Old 11-30-2004, 11:26 AM   #8
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
Quote:
Your seems to have redefined stdout as an int equal to 1 which is a very bad initiative ...
what do you mean by "bad initiative"?


Code:
while ((n=read(fd1, buffer, SIZE)) != 0){
write(1 , buffer, n);
}
I try by using this code to take it character by character it dosen't work, why?
 
Old 11-30-2004, 11:31 AM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Tribblix, Ubuntu/WSL
Posts: 9,774

Rep: Reputation: 469Reputation: 469Reputation: 469Reputation: 469Reputation: 469
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 ...
 
Old 11-30-2004, 11:45 AM   #10
ej25
Member
 
Registered: Nov 2004
Posts: 39

Original Poster
Rep: Reputation: 15
Thanks all

I really Benefit for what you said
 
  


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
new system call soul2 Linux - General 1 11-03-2004 02:41 PM
Is it possible to use system() and get the return value from the system call newguy21 Programming 1 08-11-2004 01:37 PM
System call dami Linux - General 0 11-18-2003 11:18 AM
about system call alchen1999 Programming 10 09-01-2003 03:43 AM
call for system date in c mr.moto Programming 2 09-30-2002 03:23 AM

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

All times are GMT -5. The time now is 12:51 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration