File Handling: Take formated input from textfile using fscanf() function

code:

#include<stdio.h>
#define SIZE 80
int main()
{
    FILE *fp;
    fp=fopen("E:/nosemsters/codes/crefresh/crefresh/c_for_technical_interview_udemy_course/file pointer/frmt.txt","r");
    if(fp==NULL)
    {
        printf("unable to open file\n");
        return 0;
    }
    //prime read

    char names[SIZE];
    int salary;
    while(1)
    {
        fscanf(fp, "%[^,],%d", names, &salary);
        printf("%s gets $%d per hour\n",names,salary);

        if(feof(fp))
            break;
    }
    fclose(fp);


    return 0;
}

output:

Hello gets $100 per hour

Bolo gets $300 per hour

Nai gets $500 per hour

Helo gets $69 per hour

frmt.txt

Hello,100
Bolo,300
Nai,500
Helo,69

 

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published.