fileinc

1

Click here to load reader

Upload: karthic-rao

Post on 09-Jul-2015

88 views

Category:

Education


0 download

DESCRIPTION

First create a file in your Home directory by name seqno.txt,just write one character the digit one( 1 ) into the file and save it.This program reads the number from the file , increments it then writes back the incremented value back into the file. This sequence o program is used to control print jobs in UNIX

TRANSCRIPT

Page 1: Fileinc

/* First create a file in your Home directory by name seqno.txt,just write one character the digit one( 1 ) into the file and save it.This program reads the number from the file , increments it then writes back the incremented value back into the file. This sequence o program is used to control print jobs in UNIX */

#define PATH "/home/hackintoshrao/seqno.txt" //Enter the path of your home directory here#include<stdio.h>#include<stdlib.h>#include<fcntl.h>int main(){

int i,fd,n; char buf[100]; if((fd=open(PATH,O_RDWR))==-1) //open the file to read and write fprintf(stderr,"Error opening file",50); read(fd,buf,100) ;//read the number from the file n=sizeof(buf); buf[n]='\0'; //terminate with null characters to use sscanf sscanf(buf,"%d\n",&i); //read the number from the string into integer i i++;//increment the value sprintf(buf,"%d",i); //write the incremented value into the string lseek(fd,0,SEEK_SET); /*set the write pointer to the beginning of the file(the file position was altered by the read function after reading the first character*/ write(fd,buf,sizeof(buf));//write the incremented value back to the file close(fd); return 0;}