0% found this document useful (0 votes)
50 views

Formatted I/O Operations in C: The Scanf, Sscanf and Fscanf Functions

Formatted I/O in C uses functions like scanf(), sscanf(), fscanf(), printf(), sprintf(), and fprintf() rather than read and write statements. These functions use format specifiers like %f for floating point numbers and %d for integers to read from and write to terminals, strings, and files. Files must first be opened using fopen() which returns a file pointer that is then passed to functions like fscanf() and fprintf() to perform I/O on files.

Uploaded by

Mahesh Phalke
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views

Formatted I/O Operations in C: The Scanf, Sscanf and Fscanf Functions

Formatted I/O in C uses functions like scanf(), sscanf(), fscanf(), printf(), sprintf(), and fprintf() rather than read and write statements. These functions use format specifiers like %f for floating point numbers and %d for integers to read from and write to terminals, strings, and files. Files must first be opened using fopen() which returns a file pointer that is then passed to functions like fscanf() and fprintf() to perform I/O on files.

Uploaded by

Mahesh Phalke
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 1

Formatted I/O Operations in C

Unlike Fortran, C is not equipped with read and write statements. Instead, a set of functions is provided for formatted I/O. The scanf, sscanf and fscanf Functions The scanf, sscanf and fscanf functions all uses the same formats for the input variables. The function scanf reads interactively from the CRT terminal, sscanf reads from a string variable which contains the input and fscanf reads from a le. To call these functions, use scanf(format,argument list); sscanf(string,format,argument list); fscanf(file pointer,format,argument list); in which format is a character string which contains all the specications to be used for reading the data, e.g., %f for oating numbers and %d for decimal integers, etc. argument list is a list of pointers which direct the converted data into a memory location. If a memory location is identied by a variable name x, then the pointer must be written &x. string is a the string which contains the data to be read from. It is sometimes convenient to read an entire string from the terminal and then read from that string on one or several ocassions. file pointer is the identier of type FILE for an opened le; it was assigned to a le using the fopen statement. The printf, sprintf and fprintf Functions The printf, sprintf and fprintf functions all uses the same formats for output variables. The function printf writes to the CRT terminal, sprintf writes to a string variable and fprintf writes to a le. To call these functions, use printf(format,argument list); sprintf(string,format,argument list); fprintf(file pointer,format,argument list); in which format, string and file pointer all have the same meanings as those dened above. The argument list, on the other hand, does not required pointers, just the values. The fopen Function To do input/output from a le, rather than the terminal, the le must be opened before any read or write can be performed. The fopen function can be called by fp=fopen(name,mode); in which fp is the le pointer to be used later for referencing a le when fscanf or fprintf is used. fp must be declared as FILE *fp at the beginning of the program. name is the name of the le to be opened. mode is "r" for read, "w" for write and "a" for append.

You might also like