Print Out for system programming

Embed Size (px)

Citation preview

  • 7/30/2019 Print Out for system programming

    1/22

    System Programming

    100260107011 Page 1

    Practical 2:-

    AIM:-Practical-: Introduction of File in C with sample Program

    File:

    FILE: File is a place in the disk where a group of related data is stored.

    File Operations:

    Naming a file

    Opening a file

    Reading data from file

    Writing data to a file

    Closing a file

    File handling functions (high level I/O functions).

    fopen() creates a new file for use / open a n existing file for use

    fclose() close a file which has been opened for use

    getc() reads a character from a fileputc() writes a character to a file

    getw() reads an integer from a file

    putw() writes an integer to a file

    fprintf() writes a set of data values to a file

    fscanf() reads a set of data values from a file

    fseek() sets the position anywhere in the file

    ftell() gives the current position in the file

    rewind() sets the position to the beginning of the file

    Opening a file:

    To store data in a file in the memory, we must specify :

    1. Filename: Filename is a string of characters that make up a valid filename for the

    operating systems.

  • 7/30/2019 Print Out for system programming

    2/22

    System Programming

    100260107011 Page 2

    It may contains two parts, a primary name and an optional period with the extension.

    Example: hello.c, input.data, test.out

    2. Data structure: Data structure of a file is defined as FILE in the library of standard I/O

    function definitions.FILE is defined data type.

    3. Purpose: Purpose specifies the mode of opening the file

    General format for declaring and opening a file :

    FILE *fp; fp = fopen(filename,mode);

    The first statement declares the variable fp as a pointer to the data type FILE which

    contains information about the file including its name, current position of the file.

    The second statement opens the file with named filename and assigns an identifier to

    the fp.In the second statements modes can be one of the followings :

    r ----- Open a file for reading only

    w ----- Create a file for writing only

    a ----- append (add) to file

    r+ ---- open a file for read / write

    w+ --- create a file for read / write

    a+ ---- append a file for read / write

    For example:

    FILE *p1, *p2; p1 = fopen(data, r); p2 = fopen(results, w);

    Here the file data is opened for reading and results is opened for writing.

    In case, the data file does not exit , an error will occur.

    In case, the results file already exits, its contents are deleted and the file is opened as

    a new file.

    Closing a file:

    After completing the operations on the files, the file must be closed using the fclose(

    ) function.

    Another case where we have to close a file is when we want to reopen the same file

    in a different mode.

    It takes the following form:

    fclose(file_pointer);

  • 7/30/2019 Print Out for system programming

    3/22

    System Programming

    100260107011 Page 3

    Example:

    fclose(FILE *fp);

    Here the argument fp is the FILE pointer.

    All files are closed automatically whenever a program terminates.

    INPUT/OUTPUT operations on files.

    The getc and putcfunctions :

    The getc is an input function and putc is an output function.

    These functions can handle only one character at a time.

    The getc function is used to read a character from a file which is open in read mode.

    The putc function is used to write a character to a file which is open in write mode.

    The general forms of getc and putcare :

    putc(character, fp); getc(fp);

    Example for putc :

    FILE *fp1; fp1 = fopen(DATA, w); putc(d, fp1);

    Here this statement writes a character contained in the character variable d to the file

    DATA which is associated with FILE pointer fp1.

    Example for getc :

    FILE *fp2; fp2 = fopen(DATA, r); d = getc(fp2);

    Here this statement reads a character from the file whose file pointer is fp2.

    The file pointer moves by one character position for every operation of getc or putc.

    The getc will return an end of file marker EOF when end of file has been reached.

    Therefore the reading is terminated when EOF is encountered.

    The getw and putwfunctions :

    The getw is an input function and putw is an output function.

    These functions are integer oriented functions.

    These functions can handle only one integer at a time.

    The getw function is used to read an integer value from a file which is open in read

    mode.

    The putw function is used to write an integer to a file which is open in write mode.

  • 7/30/2019 Print Out for system programming

    4/22

    System Programming

    100260107011 Page 4

    The general forms of getw and putware :

    putw(integer, fp); getw(fp);

    Example for putw :

    FILE *fp1; fp1 = fopen(DATA, w); putw(d, fp1);

    Here this statement writes an integer value contained in the integer variable d to the

    file DATA which is associated with FILE pointer fp1.

    Example for getw :

    FILE *fp2; fp2 = fopen(DATA, r); d = getw(fp2);

    Here this statement reads an integer value from the file whose file pointer is fp2.

    The file pointer moves by one integer value position for every operation of getw or

    putw.The getw will return an end of file marker EOF when end of file has been reached.

    Therefore the reading is terminated when EOF is encountered.

    ftell and fseek functions on files.

    ftell( ) :

    ftell( ) function gives the current position in the file .

    It takes the following form:

    n = ftell (fp);

    Here fp is a FILE pointer associated with the file.

    The function ftell( ) returns a type long that gives the current file position in bytes

    from the start of the file (the first byte is at position 0).

    In case of an error, ftell( ) returns -1L (a type long -1).

    So n will give the relative offset (in bytes) of the current position.

    fseek( ) :

    fseek( ) function is used to set the position anywhere in the file .

    It takes the following form:

    fseek(file_pointer, offset, position);

    Here fp is a FILE pointer associated with the file.

  • 7/30/2019 Print Out for system programming

    5/22

    System Programming

    100260107011 Page 5

    The offset specifies the number of positions to be moved from the location specified

    by position.

    The offset may be positive, meaning moves forwards, or negative, meaning move

    backwards.

    The position can take one of the following values :

    VALUE MEANING 0 Starting of file 1 Current position 2 End of file

    The following table shows the operations of the fseek( ) function :

    STATEMENT MEANING fseek(fp,0L,0); go to the starting fseek(fp,0L,1); stay at the

    current position fseek(fp,0L,2); go to the end of file fseek(fp,m,0); move to (m+1)th byte

    in the file fseek(fp,m,1); go forwards by m bytes fseek(fp,-m,1); go backward by m bytes

    from the current position fseek(fp,-m,2); go backward by m bytes from the end

    fseek( ) function returns 0 (zero) when the operation is successful. And fseek( )

    function returns -1 when an error is occurs.The offset specifies the number of positions to be moved from the location specified

    by position.

    Sample Program:

    #include #include void main() { FILE *fp; char c; int n; clrscr();

    fp=fopen("ankit.txt","w"); printf("enter file content(press enter for exit)\n\n");

    while((c=getchar())!='\n') putc(c,fp); fclose(fp); fp=fopen("ankit.txt","r"); printf("\n

    ***** file content***\n\n"); while((c=getc(fp))!=EOF) { n=ftell(fp);

    printf("\n%c\t%d",c,n); } fclose(fp); getch(); }

    *************************************************************

  • 7/30/2019 Print Out for system programming

    6/22

    System Programming

    100260107011 Page 6

    Practical 3:

    AIM:Write a proramme to copy text from one file to another file.

    #include

    #include

    #include

    void main()

    {

    FILE *fp1,*fp2;

    char c;

    fp1=fopen("D:/a.txt","r");

    fp2=fopen("D:/b.txt","w");

    while((c=getc(fp1))!=EOF)

    {

    putc(c,fp2);

    }

    fclose(fp1);

    fclose(fp2);

    }

  • 7/30/2019 Print Out for system programming

    7/22

    System Programming

    100260107011 Page 7

    OUTPUT:

  • 7/30/2019 Print Out for system programming

    8/22

    System Programming

    100260107011 Page 8

  • 7/30/2019 Print Out for system programming

    9/22

    System Programming

    100260107011 Page 9

    Practical 4:

    AIM: Write a program to read text from user and write it into the file and also read the text

    from file and display on the output terminal.

    #include

    #include

    #include

    #include

    void main()

    {

    FILE *fp1,*fp2;char c,d,e[10],f[10];

    clrscr();

    int i;

    fp1=fopen("D:/a.txt","w");

    printf("\n enter the string");

    scanf("%s",e);

    for(i=0;e[i]!='\0';i++)

    { putc(e[i],fp1);

    }

    fclose(fp1);

    fp1=fopen("d:/a.txt","r");

    while((d=getc(fp1))!=EOF)

    {

    printf("%c",d);

    }

    fclose(fp1);

    getch();

    }

  • 7/30/2019 Print Out for system programming

    10/22

    System Programming

    100260107011 Page 10

    OUTPUT:

  • 7/30/2019 Print Out for system programming

    11/22

    System Programming

    100260107011 Page 11

    Practical 5:

    AIM: write a program to copy from command line.

    #include

    #include

    #include

    void main(int argc,char *argv[])

    {

    clrscr();

    FILE *fp1;

    FILE *fp2;

    char c;

    fp1=fopen(argv[1],"r");

    fp2=fopen(argv[2],"w");

    getc(fp1);

    while((c=getc(fp1))!=EOF)

    {

    putc(c,fp2);

    }

    fclose(fp1);

    fclose(fp2);

    getch();

    }

  • 7/30/2019 Print Out for system programming

    12/22

    System Programming

    100260107011 Page 12

  • 7/30/2019 Print Out for system programming

    13/22

    System Programming

    100260107011 Page 13

  • 7/30/2019 Print Out for system programming

    14/22

    System Programming

    100260107011 Page 14

    Practical 6:

    AIM: WAP to remove comment line from the source file

    #include

    #include

    #include

    void main()

    { clrscr();

    FILE *fp1;

    FILE * fp2;

    charc,ch;

    inti;

    fp1=fopen("d:/a.txt","r");

    fp2=fopen("d:/b.txt","w");

    while((c=getc(fp1))!=EOF)

    {

    if(c=='/')

    { ch=c;

    c=getc(fp1);

    if(c=='*')

    {

    while(1)

    {

    if((c=getc(fp1))=='*' &&(c=getc(fp1))=='/')

    {

    break;

    }

    }

    }

    else

    {

    putc(ch,fp2);

    putc(c,fp2);

    }

    }

    else

    {

    putc(c,fp2);}

    }

    fclose(fp1);

    fclose(fp2);

    getch(); }

  • 7/30/2019 Print Out for system programming

    15/22

    System Programming

    100260107011 Page 15

    OUTPUT:-

  • 7/30/2019 Print Out for system programming

    16/22

    System Programming

    100260107011 Page 16

    Practical 7:

    Aim: Write a c program to find operator& key word.

    #include

    #include

    #include

    #include

    struct operator

    {

    char line[50];

    }s[50];

    void jump(char str[]);

    void main()

    {

    inti=0,j,b,c=0,k;

    int c1,c2;

    charstr[50];

  • 7/30/2019 Print Out for system programming

    17/22

    System Programming

    100260107011 Page 17

    clrscr();

    printf("Enter the code:");

    do

    {

    gets(s[i].line);

    }while(strcmpi (s[i++].line,"end"));

    i=0;

    while(strcmpi (s[i].line,"end"))

    {

    for(j=0,k=0;s[i].line[j]!='\0';j++,k++)

    {

    if(s[i].line[j]=='+'||s[i].line[j]=='-'||s[i].line[j]=='/'||s[i].line[j]=='*')

    {

    b=j;

    c1=b+1;

    c2=b-1;

    if(isalpha(s[i].line[c1]) &&isalpha(s[i].line[c2]))

    {

    printf("\n%c",s[i].line[j]);

    c++;

    }

    }

    else if(s[i].line[j]=='(' || s[i].line[j]==' ')

    {

  • 7/30/2019 Print Out for system programming

    18/22

    System Programming

    100260107011 Page 18

    str[k]='\0';

    jump(str);

    k=-1;

    }

    else

    {

    str[k]=s[i].line[j];

    }

    }

    i++;

    }

    printf("\n operator=%d",c);

    getch();

    }

    void jump(char str[])

    {

    int k;

    char a[10][10]={"include","main","int","float","char","if","else","for","while"};

    for(k=0;k

  • 7/30/2019 Print Out for system programming

    19/22

    System Programming

    100260107011 Page 19

    if(strcmp(str,a[k])==0)

    {

    printf("%s",str);

    }

    }

    }

    Output:

  • 7/30/2019 Print Out for system programming

    20/22

    System Programming

    100260107011 Page 20

    Practical 8:

    AIM :- Implementation of assembler pass-1.

    # include

    # include

    # include

    void main()

    {

    char opcode[10],mnemonic[3],operand[10],label[10],code[10];

    intlocctr,start,length;

    FILE *fp1,*fp2,*fp3,*fp4;

    clrscr();fp1=fopen("input.dat","r");

    fp2=fopen("symtab.dat","w");

    fp3=fopen("out.dat","w");

    fp4=fopen("optab.dat","r");

    fscanf(fp1,"%s%s%s",label,opcode,operand);

    if(strcmp(opcode,"START")==0)

    {

    start=atoi(operand);

    locctr=start;

    fprintf(fp3,"\t%s\t%s\t%s\n",label,opcode,operand);

    fscanf(fp1,"%s%s%s",label,opcode,operand);

    }

    else

    locctr=0;

    while(strcmp(opcode,"END")!=0)

    {

    fprintf(fp3,"%d\t",locctr);

    if(strcmp(label,"**")!=0)

    fprintf(fp2,"%s\t%d\n",label,locctr);

    rewind(fp4);

    fscanf(fp4,"%s",code);

    while(strcmp(code,"END")!=0)

    {

    if(strcmp(opcode,code)==0)

    {

    locctr+=3;

  • 7/30/2019 Print Out for system programming

    21/22

    System Programming

    100260107011 Page 21

    break;

    }

    fscanf(fp4,"%s",code);

    }

    if(strcmp(opcode,"WORD")==0)

    locctr+=3;

    else if(strcmp(opcode,"RESW")==0)

    locctr+=(3*(atoi(operand)));

    else if(strcmp(opcode,"RESB")==0)

    locctr+=(atoi(operand));

    else if(strcmp(opcode,"BYTE")==0)

    ++locctr;

    fprintf(fp3,"%s\t%s\t%s\n",label,opcode,operand);

    fscanf(fp1,"%s%s%s",label,opcode,operand);

    }

    fprintf(fp3,"%d\t%s\t%s\t\%s\n",locctr,label,opcode,operand);

    length=locctr-start;

    printf("The length of the program is %d",length);

    fclose(fp1);

    fclose(fp2);

    fclose(fp3);

    fclose(fp4);

    getch();

    }

    INPUT FILES

    INPUT.DAT

    ** START 2000

    ** LDA FIVE

    ** STA ALPHA

    ** LDCH CHARZ

    ** STCH C1

    ALPHA RESW 1

  • 7/30/2019 Print Out for system programming

    22/22

    System Programming

    100260107011 Page 22

    FIVE WORD 5

    CHARZ BYTE CZ

    C1 RESB 1

    ** END **

    OPTAB.DAT

    START

    LDA

    STA

    LDCH

    STCH

    END

    OUTPUT FILES

    OUT.DAT

    ** START 2000

    2000 ** LDA FIVE

    2003 ** STA ALPHA

    2006 ** LDCH CHARZ

    2009 ** STCH C1

    2012 ALPHA RESW 1

    2015 FIVE WORD 5

    2018 CHARZ BYTE CZ

    2019 C1 RESB 1

    2020 ** END **

    SYMTAB.DAT

    ALPHA 2012

    FIVE 2015

    CHARZ 2018

    C1 2018