- Asked 2/16
- 2074 Asoj · 4 marks
- 2068 Shrawan · 4 marks
I/O functions used with data files
Short answer
fprintf and fscanf perform formatted text I/O on files. fwrite and fread transfer binary blocks of data directly between memory and file, useful for structures. They require a file pointer opened with fopen and closed with fclose.
What are different input/output functions used with data files in C? Explain with syntax and examples. (Explain fscanf, fprintf, fread, fwrite functions.)
Data files are accessed through a file pointer (FILE *fp) opened with fopen(). The main input/output functions (all in <stdio.h>) are:
| Function | Purpose | Syntax |
|---|---|---|
fprintf | Write formatted text | fprintf(fp, "format", list); |
fscanf | Read formatted text | fscanf(fp, "format", &list); |
fputc / fgetc | Write / read one character | fputc(ch, fp); ch = fgetc(fp); |
fputs / fgets | Write / read a string | fputs(str, fp); fgets(str, n, fp); |
fwrite | Write blocks (binary) | fwrite(ptr, size, count, fp); |
fread | Read blocks (binary) | fread(ptr, size, count, fp); |
fprintfandfscanfwork likeprintfandscanf, but on a file.fscanfreturns the number of items read (or EOF).fwriteandfreadcopycountblocks ofsizebytes directly between memory and the file, so a whole structure is stored in one call. Hereptris the address of the data, for example&sfor a structure variables. They return the number of blocks transferred.
#include <stdio.h>
struct rec {
char name[20];
int roll;
};
int main(void)
{
FILE *fp;
struct rec s = {"Ram", 7}, t;
char name[20];
int roll;
/* formatted I/O on a text file */
fp = fopen("text.txt", "w");
if (fp == NULL) { printf("Cannot open file\n"); return 1; }
fprintf(fp, "%s %d\n", "Sita", 12);
fclose(fp);
fp = fopen("text.txt", "r");
if (fp == NULL) { printf("Cannot open file\n"); return 1; }
fscanf(fp, "%s %d", name, &roll);
printf("fscanf read: %s %d\n", name, roll);
fclose(fp);
/* block I/O on a binary file */
fp = fopen("rec.dat", "wb");
if (fp == NULL) { printf("Cannot open file\n"); return 1; }
fwrite(&s, sizeof(s), 1, fp);
fclose(fp);
fp = fopen("rec.dat", "rb");
if (fp == NULL) { printf("Cannot open file\n"); return 1; }
fread(&t, sizeof(t), 1, fp);
printf("fread read : %s %d\n", t.name, t.roll);
fclose(fp);
return 0;
}
Output:
fscanf read: Sita 12
fread read : Ram 7
✓ Answer checked by Claude Opus 5.5 (high effort)
Related questions
- Formatted I/O in FORTRAN with syntax · Computer Programming, chapter 11
- Syntax of while loop · Computer Programming, chapter 5
- Syntax of if...else · Computer Programming, chapter 5
- Syntax of static · Computer Programming, chapter 6
- Syntax of struct · Computer Programming, chapter 8
- Odd and even numbers to separate files · Computer Programming, chapter 10 · asked 2×
- LTI output for h(t)=e^(-2t), rectangular input · Data Communication, chapter 2 · asked 3×
- Data logger input scanner and conditioner design · Instrumentation II, chapter 5
- Output of e^(−2t)u(t) to a rectangular input · Signals and Systems, chapter 5 · asked 4×
- CE configuration input/output characteristics · Basic Electronics Engineering, chapter 3 · asked 3×