linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* programming using system calls
@ 2007-07-31 11:27 nisa
  2007-07-31 11:51 ` spiro harvey
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: nisa @ 2007-07-31 11:27 UTC (permalink / raw)
  To: linux-c-programming


hi,
i am quite new to programming using system calls and would like a basic idea
regarding the usage of system calls.
i would like assistance in the following area of c programming in linux:
1.how to open a text file ,read data and print the data on console using
system calls
2.create a text file and write some data
3.read data from a file and append that data to another file using lseek()
4.creation of a parent and child process using fork()

-- 
View this message in context: http://www.nabble.com/programming-using-system-calls-tf4192448.html#a11922382
Sent from the linux-c-programming mailing list archive at Nabble.com.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: programming using system calls
  2007-07-31 11:27 programming using system calls nisa
@ 2007-07-31 11:51 ` spiro harvey
       [not found] ` <192840a00707310451s6a1b369eg632be924c8de1003@mail.gmail.com>
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: spiro harvey @ 2007-07-31 11:51 UTC (permalink / raw)
  To: linux-c-programming

this sounds awfully like homework.

On 31/07/07, nisa <snisa.balakrishnan@gmail.com> wrote:
>
> hi,
> i am quite new to programming using system calls and would like a basic idea
> regarding the usage of system calls.
> i would like assistance in the following area of c programming in linux:
> 1.how to open a text file ,read data and print the data on console using
> system calls
> 2.create a text file and write some data
> 3.read data from a file and append that data to another file using lseek()
> 4.creation of a parent and child process using fork()
>
> --
> View this message in context: http://www.nabble.com/programming-using-system-calls-tf4192448.html#a11922382
> Sent from the linux-c-programming mailing list archive at Nabble.com.
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

^ permalink raw reply	[flat|nested] 7+ messages in thread

* programming using system calls
       [not found] ` <192840a00707310451s6a1b369eg632be924c8de1003@mail.gmail.com>
@ 2007-07-31 11:53   ` Андрій Мішковський
  0 siblings, 0 replies; 7+ messages in thread
From: Андрій Мішковський @ 2007-07-31 11:53 UTC (permalink / raw)
  To: linux-c-programming, snisa.balakrishnan

2007/7/31, nisa <snisa.balakrishnan@gmail.com>:

>
> hi,
> i am quite new to programming using system calls and would like a basic idea
> regarding the usage of system calls.
> i would like assistance in the following area of c programming in linux:
> 1.how to open a text file ,read data and print the data on console using
> system calls
> 2.create a text file and write some data
> 3.read data from a file and append that data to another file using lseek()
> 4.creation of a parent and child process using fork()
>
> --
> View this message in context:  http://www.nabble.com/programming-using-system-calls-tf4192448.html#a11922382
> Sent from the linux-c-programming mailing list archive at  Nabble.com.
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
>  the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>


Well, as far as i know, man pages are a good source of knowledge.
Wait, i'll check that. Hm... Yeah, they are. :)
Everything that you are looking for is situated in the second section
of man pages.

-- 
Wbr, Andriy Mishkovskyy.

He's got a heart of a little child, and he keeps it in a jar on his desk.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: programming using system calls
  2007-07-31 11:27 programming using system calls nisa
  2007-07-31 11:51 ` spiro harvey
       [not found] ` <192840a00707310451s6a1b369eg632be924c8de1003@mail.gmail.com>
@ 2007-07-31 12:00 ` Mateus Interciso
  2007-07-31 14:00   ` Андрій Мішковський
  2007-08-10 14:53 ` Li YanBo
  3 siblings, 1 reply; 7+ messages in thread
From: Mateus Interciso @ 2007-07-31 12:00 UTC (permalink / raw)
  To: linux-c-programming

On Tue, 31 Jul 2007 04:27:13 -0700, nisa wrote:

> hi,
> i am quite new to programming using system calls and would like a basic
> idea regarding the usage of system calls.
> i would like assistance in the following area of c programming in linux:
> 1.how to open a text file ,read data and print the data on console using
> system calls
> 2.create a text file and write some data 3.read data from a file and
> append that data to another file using lseek() 4.creation of a parent
> and child process using fork()

Well, just to not let you in blank, here is a VERY simple file that reads 
a text file, and output it to the screen

#include <stdio.h>  //standard IO
#include <stdlib.h> //for reading files
#include <string.h> //for memset
#include <errno.h>  //for errno
int main(int argc, char *argv[]){
  FILE *fp = NULL;
  char ch[1];

  if(argc!=2){
    fprintf(stderr,"Usage:%s <file>\n",argv[0]);
    return 1;
  }
  memset(ch,'\0',sizeof(char)*1);
  if((fp=fopen(argv[1],"r"))==NULL){
    perror("fopen");
    return errno;
  }
  while(feof(fp)==0){
    if( (fread(ch,sizeof(char),1,fp)==0) && (feof(fp)==0) ){
      perror("fread");
      fclose(fp);
      memset(ch,'\0',sizeof(char)*1);
      return errno;
    }
    fprintf(stdout,"%c",ch[0]);
  }
  fclose(fp);
  memset(ch,'\0',sizeof(char)*1);
  return 0;
}

Also, as it was stated before, use the man pages. If you don't have a 
Linux box, then google will be your friend for this.
In this example, you would need, the man pages for fopen(),fread() and 
feof().

Good luck.


^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: programming using system calls
  2007-07-31 12:00 ` Mateus Interciso
@ 2007-07-31 14:00   ` Андрій Мішковський
  2007-07-31 14:13     ` Mateus Interciso
  0 siblings, 1 reply; 7+ messages in thread
From: Андрій Мішковський @ 2007-07-31 14:00 UTC (permalink / raw)
  To: Mateus Interciso; +Cc: linux-c-programming

Hi, Mateus.
Maybe it looks rude, but i want to correct you:
fread, feof, fopen are _not_ system calls, as i remember.
Your example is correct, but it makes no use of system calls. Your
code is pure ISO C. :)
Considering this, the program should look like this:

#include <unistd.h> /*read(), close()*/
#include <fcntl.h> /*open()*/
#include <stdio.h> /*fprintf()*/
#include <sys/types.h> /**/
#include <errno.h> /* errno*/
#include <string.h> /* strerror(), memset()*/

int
main(int argc, char **argv)
{
	int fd; /*file descriptor*/
	char buf[BUFSIZ]; /*buffer for reading data*/
	int bytes_read = -1; /*bytes, already read from file*/
	
	if (argc != 2)
	{
		fprintf(stderr, "Usage: %s <file>\n", argv[0]);
		return 1;
	}
	
	fd = open (argv[1], O_RDONLY);
	if (fd == -1)
	{
		fprintf(stderr, "%s: open() failed. Reason: %s", argv[0], strerror(errno));
		return 1;
	}
	
	while (bytes_read != 0)
	{
		bytes_read = read(fd, buf, BUFSIZ);
		if (bytes_read == -1)
		{
			fprintf(stderr, "%s: read() failed. Reason: %s", argv[0], strerror(errno));
			close(fd);
			return 1;
		}
		fprintf(stdout, "%s", buf);
		memset(buf, 0, BUFSIZ);
	}
	
	close(fd);
	
	return 0;
}

2007/7/31, Mateus Interciso <p.zarnick@gmail.com>:
> On Tue, 31 Jul 2007 04:27:13 -0700, nisa wrote:
>
> > hi,
> > i am quite new to programming using system calls and would like a basic
> > idea regarding the usage of system calls.
> > i would like assistance in the following area of c programming in linux:
> > 1.how to open a text file ,read data and print the data on console using
> > system calls
> > 2.create a text file and write some data 3.read data from a file and
> > append that data to another file using lseek() 4.creation of a parent
> > and child process using fork()
>
> Well, just to not let you in blank, here is a VERY simple file that reads
> a text file, and output it to the screen
>
> #include <stdio.h>  //standard IO
> #include <stdlib.h> //for reading files
> #include <string.h> //for memset
> #include <errno.h>  //for errno
> int main(int argc, char *argv[]){
>   FILE *fp = NULL;
>   char ch[1];
>
>   if(argc!=2){
>     fprintf(stderr,"Usage:%s <file>\n",argv[0]);
>     return 1;
>   }
>   memset(ch,'\0',sizeof(char)*1);
>   if((fp=fopen(argv[1],"r"))==NULL){
>     perror("fopen");
>     return errno;
>   }
>   while(feof(fp)==0){
>     if( (fread(ch,sizeof(char),1,fp)==0) && (feof(fp)==0) ){
>       perror("fread");
>       fclose(fp);
>       memset(ch,'\0',sizeof(char)*1);
>       return errno;
>     }
>     fprintf(stdout,"%c",ch[0]);
>   }
>   fclose(fp);
>   memset(ch,'\0',sizeof(char)*1);
>   return 0;
> }
>
> Also, as it was stated before, use the man pages. If you don't have a
> Linux box, then google will be your friend for this.
> In this example, you would need, the man pages for fopen(),fread() and
> feof().
>
> Good luck.
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>


-- 
Wbr, Andriy Mishkovskyy.

He's got a heart of a little child, and he keeps it in a jar on his desk.

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: programming using system calls
  2007-07-31 14:00   ` Андрій Мішковський
@ 2007-07-31 14:13     ` Mateus Interciso
  0 siblings, 0 replies; 7+ messages in thread
From: Mateus Interciso @ 2007-07-31 14:13 UTC (permalink / raw)
  To: linux-c-programming

On Tue, 31 Jul 2007 18:00:03 +0400,
Андрій Мішковський wrote:

> Hi, Mateus.
> Maybe it looks rude, but i want to correct you: fread, feof, fopen are
> _not_ system calls, as i remember. Your example is correct, but it makes
> no use of system calls. Your code is pure ISO C. :)
> Considering this, the program should look like this:
> 
> #include <unistd.h> /*read(), close()*/ #include <fcntl.h> /*open()*/
> #include <stdio.h> /*fprintf()*/
> #include <sys/types.h> /**/
> #include <errno.h> /* errno*/
> #include <string.h> /* strerror(), memset()*/
> 
> int
> main(int argc, char **argv)
> {
> 	int fd; /*file descriptor*/
> 	char buf[BUFSIZ]; /*buffer for reading data*/ int bytes_read = -1;
> 	/*bytes, already read from file*/
> 	
> 	if (argc != 2)
> 	{
> 		fprintf(stderr, "Usage: %s <file>\n", argv[0]); return 1;
> 	}
> 	
> 	fd = open (argv[1], O_RDONLY);
> 	if (fd == -1)
> 	{
> 		fprintf(stderr, "%s: open() failed. Reason: %s", argv[0],
> 		strerror(errno)); return 1;
> 	}
> 	
> 	while (bytes_read != 0)
> 	{
> 		bytes_read = read(fd, buf, BUFSIZ);
> 		if (bytes_read == -1)
> 		{
> 			fprintf(stderr, "%s: read() failed. Reason: %s", 
argv[0],
> 			strerror(errno)); close(fd);
> 			return 1;
> 		}
> 		fprintf(stdout, "%s", buf);
> 		memset(buf, 0, BUFSIZ);
> 	}
> 	
> 	close(fd);
> 	
> 	return 0;
> }
> 
> 2007/7/31, Mateus Interciso <p.zarnick@gmail.com>:
>> On Tue, 31 Jul 2007 04:27:13 -0700, nisa wrote:
>>
>> > hi,
>> > i am quite new to programming using system calls and would like a
>> > basic idea regarding the usage of system calls. i would like
>> > assistance in the following area of c programming in linux: 1.how to
>> > open a text file ,read data and print the data on console using
>> > system calls
>> > 2.create a text file and write some data 3.read data from a file and
>> > append that data to another file using lseek() 4.creation of a parent
>> > and child process using fork()
>>
>> Well, just to not let you in blank, here is a VERY simple file that
>> reads a text file, and output it to the screen
>>
>> #include <stdio.h>  //standard IO
>> #include <stdlib.h> //for reading files #include <string.h> //for
>> memset
>> #include <errno.h>  //for errno
>> int main(int argc, char *argv[]){
>>   FILE *fp = NULL;
>>   char ch[1];
>>
>>   if(argc!=2){
>>     fprintf(stderr,"Usage:%s <file>\n",argv[0]); return 1;
>>   }
>>   memset(ch,'\0',sizeof(char)*1);
>>   if((fp=fopen(argv[1],"r"))==NULL){
>>     perror("fopen");
>>     return errno;
>>   }
>>   while(feof(fp)==0){
>>     if( (fread(ch,sizeof(char),1,fp)==0) && (feof(fp)==0) ){
>>       perror("fread");
>>       fclose(fp);
>>       memset(ch,'\0',sizeof(char)*1);
>>       return errno;
>>     }
>>     fprintf(stdout,"%c",ch[0]);
>>   }
>>   fclose(fp);
>>   memset(ch,'\0',sizeof(char)*1);
>>   return 0;
>> }
>>
>> Also, as it was stated before, use the man pages. If you don't have a
>> Linux box, then google will be your friend for this. In this example,
>> you would need, the man pages for fopen(),fread() and feof().
>>
>> Good luck.
>>
>> -
>> To unsubscribe from this list: send the line "unsubscribe
>> linux-c-programming" in the body of a message to
>> majordomo@vger.kernel.org More majordomo info at 
>> http://vger.kernel.org/majordomo-info.html
>>

You are absolutly right, I'm terrible sorry...It's just that it has been 
so long since I haven't used open(), read(), close(), that I must have 
gone crazy, I'm terribly sorry for this.

Mateus

-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 7+ messages in thread

* Re: programming using system calls
  2007-07-31 11:27 programming using system calls nisa
                   ` (2 preceding siblings ...)
  2007-07-31 12:00 ` Mateus Interciso
@ 2007-08-10 14:53 ` Li YanBo
  3 siblings, 0 replies; 7+ messages in thread
From: Li YanBo @ 2007-08-10 14:53 UTC (permalink / raw)
  To: nisa; +Cc: linux-c-programming

On 7/31/07, nisa <snisa.balakrishnan@gmail.com> wrote:
>
> hi,
> i am quite new to programming using system calls and would like a basic idea
> regarding the usage of system calls.
> i would like assistance in the following area of c programming in linux:
> 1.how to open a text file ,read data and print the data on console using
> system calls
> 2.create a text file and write some data
> 3.read data from a file and append that data to another file using lseek()
> 4.creation of a parent and child process using fork()
>
> --
> View this message in context: http://www.nabble.com/programming-using-system-calls-tf4192448.html#a11922382
> Sent from the linux-c-programming mailing list archive at Nabble.com.
>
> -
> To unsubscribe from this list: send the line "unsubscribe linux-c-programming" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html
>

Recommend the Unix&&Linux system&&C program bible: Advanced
Programming in the UNIX Environment author:W. Richard Stevens

lyb

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2007-08-10 14:53 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-07-31 11:27 programming using system calls nisa
2007-07-31 11:51 ` spiro harvey
     [not found] ` <192840a00707310451s6a1b369eg632be924c8de1003@mail.gmail.com>
2007-07-31 11:53   ` Андрій Мішковський
2007-07-31 12:00 ` Mateus Interciso
2007-07-31 14:00   ` Андрій Мішковський
2007-07-31 14:13     ` Mateus Interciso
2007-08-10 14:53 ` Li YanBo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).