C fscanf() function reads formatted input from a file. This function is implemented in file-related programs for reading formatted data from any file specified in the program. This tutorial guides you on how to use the fscanf() function in the C program.



Syntax:

int fscanf(FILE *stream, const char *format, ...)

Its returns the number of variables that are assigned values or EOF if no assignments could be made.

Example:

int main()
{
    char str1[10], str2[10];
    int yr;
    FILE* fileName;
    fileName = fopen("anything.txt", "w+");
    fputs("Welcome to", fileName);
    rewind(fileName);
    fscanf(fileName, "%s %s %d", str1, str2, &yr);
    printf("----------------------------------------------- \n");
    printf("1st word %s \t", str1);
    printf("2nd word  %s \t", str2);
    printf("Year-Name  %d \t", yr);
    fclose(fileName);
    return (0);
}


Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram