linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Mateus Interciso <p.zarnick@gmail.com>
To: linux-c-programming@vger.kernel.org
Subject: Re: programming using system calls
Date: Tue, 31 Jul 2007 12:00:14 +0000 (UTC)	[thread overview]
Message-ID: <f8n88c$hsu$1@sea.gmane.org> (raw)
In-Reply-To: 11922382.post@talk.nabble.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.


  parent reply	other threads:[~2007-07-31 12:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2007-07-31 14:00   ` Андрій Мішковський
2007-07-31 14:13     ` Mateus Interciso
2007-08-10 14:53 ` Li YanBo

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='f8n88c$hsu$1@sea.gmane.org' \
    --to=p.zarnick@gmail.com \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).