C examples for File:Binary File
For example, the next fscanf() function expects to read two character strings called name and hobby.
fscanf(pRead, "%s%s", name, hobby);
%s type specifier will read a series of characters until a white space is found, including blank, new line, or tab.
The following table lists the type specifiers you can use with the fscanf() function.
Type | Description |
---|---|
c | Single character |
d | Decimal integer |
e, E, f, g, G | Floating point |
o | Octal integer |
s | String of characters |
u | Unsigned decimal integer |
x, X | Hexadecimal integer |
#include <stdio.h> int main() { //from w w w . j a va 2 s . c om FILE *pRead; char name[10]; char hobby[15]; pRead = fopen("hobbies.dat", "r"); if ( pRead == NULL ) printf("\nFile cannot be opened\n"); else printf("\nName\tHobby\n\n"); fscanf(pRead, "%s%s", name, hobby); while ( !feof(pRead) ) { printf("%s\t%s\n", name, hobby); fscanf(pRead, "%s%s", name, hobby); } //end loop }