Random File Access in C

WAP to print the first character of each word from the string contained in a file “details.txt”. The file “details.txt” has “Deep Raj Bhujel” string in it and your program should print “D R B” on the console screen. [Random File Access: fseek(), ftell(), rewind()]

#include<stdio.h>

#include<stdlib.h>

int main()

{

    FILE *fp;

    char ch;

    //long pos;


    fp = fopen("details.txt","r");


    if(fp == NULL)

    {

        puts("Error opening file");

        exit(1);

    }


    //printf("%ld ",ftell(fp));

    ch = fgetc(fp);

    printf("%c ",ch);

    //printf("%ld ",ftell(fp));


    fseek(fp,4,SEEK_CUR);

    ch = fgetc(fp);

    printf("%c ",ch);

    //printf("%ld ",ftell(fp));


    fseek(fp,3,SEEK_CUR);

    ch = fgetc(fp);

    printf("%c ",ch);

    //printf("%ld ",ftell(fp));


    //rewind(fp);

    //printf("%ld ",ftell(fp));


    return 0;

}


No comments:

Post a Comment