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));
10      fp = fopen("records.txt", "w");
11      if (fp) {
12          printf("== Student Database Add records ==\n");
13          printf("Name : ");
14          scanf("%[^\n]", name);
15          fflush(stdin);
16          printf("Roll : ");
17          scanf("%d", &roll);
18          printf("Std : ");
19          scanf("%d", &std);
20          if (fprintf(fp, "%s\r\n%d\r\n%d\r\n", name, roll, std)) {
21              printf("Record added.");
22          }
23          fclose(fp);
24 
25      }
26      return 0;
27  }

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));
10      fp = fopen("records.txt", "r");
11      if (fp) {
12          printf("== Student Database Display records ==\n");
13          if(fscanf(fp, "%[^\r]\n%d\r\n%d\r\n", name, &roll, &std)){
14              printf("Name : %s, Roll : %d, Std : %d\n", name, roll, std);
15          }
16          fclose(fp);
17 
18      }
19      return 0;
20  }

Output

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

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

 Vote 0

Similar topics related to this section

fdopen, write vs append mode, binary vs text mode, binary mode, fseek, ftell, rewind, fprintf, fscanf, fflush, poll, select, fcntl, fputs,fgets,fgetc,fputc,

# C Programming Language (Prentice Hall Software)
# Let Us C Paperback - 2006 by Yashavant Kanetkar
# Understanding and Using C Pointers Core techniques for memory management
# Data Structures Using C and C++ Paperback - 1998
# Data Structures In C Paperback - August 11, 2008 by Noel Kalicharan