All about programming in GNU/LINUX

UNIX print job sequence no program

//download link for the file: http://www.slideshare.net/hackintoshrao/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]=”;   //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;
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s