Computer Scientists are Pretty Pessimistic

Saturday 13 February 2016

How to read a FILE in C ?

Solution : :

#include <stdio.h>
int main ()
{
    FILE *fp;
    char strF[50];
    int idF;
    if((fp=fopen("myfile.txt","r"))!=NULL)
        printf("Successfully file open");
    else
    {
        printf("\nError in file opening");
        return 0;
    }
    if(fgets(strF,50,fp) != NULL)
        printf("\n\n Your name: %s",strF);
    else
        printf("\n\n Problem in reading from file.");
    if(fgets(strF,50,fp) != NULL)
        printf("\n\n Your country is: %s",strF);
    else
        printf(" Problem in reading from file.");
    if(fgets(strF,50,fp) != NULL)
        printf("\n\n Your mob number is: %s",strF);
    else
        printf(" Problem in reading from file.");
    if((fscanf(fp,"%d",&idF))<0)
    {
        printf("\n Error occured reading the numbers.");
        return 0;
    }
    else
        printf("\n\n Your id is %d",idF);
    if(fclose(fp)==EOF)
    { printf("Error in closing file\n"); return 0; }
    else
        printf("\n\nSuccessfully close\n");
}

No comments:

Post a Comment