/* First create a file in your Home directory by name seqno.txt,just write onecharacter the   digit one( 1 ) into the file...
Upcoming SlideShare
Loading in …5
×

Fileinc

196 views

Published on

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

Published in: Education
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
196
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
1
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Fileinc

  1. 1. /* First create a file in your Home directory by name seqno.txt,just write onecharacter the digit one( 1 ) into the file and save it.This program reads the number from thefile , 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 homedirectory 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,"%dn",&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 wasaltered 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;}

×