Formatted Text Mode

File can be in binary mode or text mode. Text mode deals with simple text and strings. Text mode can be in the form of unformatted text or in the form of formatted text/strings. We use the word formatted because records are stored in human readable format. These files can be viewed by a simple text editor and can be altered in the same way. C library has two API routines fscanf and fprintf. fscanf is to read formatted strings from the file and fprintf is to write formatted strings to a file. We will re-write our previous student database program with formatted string APIs.

Formatted Mode fprintf

fprintf takes two fixed arguments and can take variable additional arguments. First argument is the file pointer. Second argument is the format string. Additional arguments depends of formatting string. User has to provide the elements in the same sequence as the format string has been given. fprintf scans the formatting string and replaces all the %<element> with the text equivalent of the arguments given. This buffer then goes for writing to the device. It returns the number of bytes written to the files.

int fprintf (FILE * fp, const char * format, ...);

Formatted Mode fprintf C code

/* Student Database Add records */
#include <stdio.h>
int main1(int argc, char *argv[])
{
    FILE *fp;
    char name[20];
    int roll;
    int std;
    memset(name, 0, sizeof(name));
    fp = fopen("records.txt", "w");
    if (fp) {
        printf("== Student Database Add records ==\n");
        printf("Name : ");
        scanf("%[^\n]", name);
        fflush(stdin);
        printf("Roll : ");
        scanf("%d", &roll);
        printf("Std : ");
        scanf("%d", &std);
        if (fprintf(fp, "%s\r\n%d\r\n%d\r\n", name, roll, std)) {
            printf("Record added.");
        }
        fclose(fp);

    }
    return 0;
}

Output

== Student Database Add records ==
Name : Student 1
Roll : 1
Std : 1
Record added.

Formatted Mode fprintf

fscanf takes similar arguments as fprintf. It also take two fixed arguments and can take variable additional arguments. First argument is the file pointer. Second argument is the format string. Additional arguments depends of formatting string. User has to provide the elements in the same sequence as the format string has been given. fscanf reads the buffer from file and scans input buffer as per formatting string and whenever it finds %<element> it converts the element and stores in the address given in the additional arguments. User has to provide the reference of the variables in the arguments in order to receive type objects from file. It returns the number of successful arguments converted successfully.

int fscanf (FILE * fp, const char * format, ...);

Formatted Mode fscanf C code

/* Student Database Display records */
#include <stdio.h>
int main(int argc, char *argv[])
{
    FILE *fp;
    char name[20];
    int roll;
    int std;
    memset(name, 0, sizeof(name));
    fp = fopen("records.txt", "r");
    if (fp) {
        printf("== Student Database Display records ==\n");
        if(fscanf(fp, "%[^\r]\n%d\r\n%d\r\n", name, &roll, &std)){
            printf("Name : %s, Roll : %d, Std : %d\n", name, roll, std);
        }
        fclose(fp);

    }
    return 0;
}

Output

== Student Database Display records ==
Name : Student 1, Roll : 1, Std : 1

About our authors: Team EQA

You have viewed 1 page out of 252. Your C learning is 0.00% complete. Login to check your learning progress.

#