C++ Standard Library <cstdio>
<cstdio>It is a header file in the C++ standard library. It contains the C++ wrapper of the C standard I/O library and is mainly used for file input and output operations.
<cstdio>The library defines a set of functions for performing input and output operations. These functions can be used to read and write files and the console.
Syntax
When using<cstdio>Before using the functions in the library, you need to include this header file at the top of the C++ program:
#include <cstdio>
Common Functions
<cstdio>The library contains many functions for file I/O. The following are some commonly used functions:
fopen: Open file.fclose: Close file.fread: Read data from a file.fwrite: Write data to a file.fprintf: Write formatted output to a file.fscanf: Read formatted input from a file.fgetc: Read a character from a file.fputc: Write a character to a file.fgets: Read a line from a file.fputs: Write a line to a file.
Example
1. Open and close files:
Example
int main() {
FILE *file = fopen("example.txt", "w"); // Open file for writing
if (file == NULL) {
perror("Error opening file");
return 1;
}
fclose(file); // Close file
return 0;
}
2. Write data to a file:
Example
int main() {
FILE *file = fopen("example.txt", "w");
if (file == NULL) {
perror("Error opening file");
return 1;
}
fprintf(file, "Hello, World!n");
fclose(file);
return 0;
}
Wrote "Hello, World!" to the "example.txt" file.
3. Read data from a file:
Example
#include <iostream>
int main() {
FILE *file = fopen("example.txt", "r");
if (file == NULL) {
perror("Error opening file");
return 1;
}
char buffer[100];
while (fgets(buffer, 100, file) != NULL) {
std::cout << buffer;
}
fclose(file);
return 0;
}
Read from the "example.txt" file and output "Hello, World!".
4. Use fscanf and fprintf for formatted input and output:
Example
int main() {
FILE *file = fopen("data.txt", "w");
if (file == NULL) {
perror("Error opening file");
return 1;
}
fprintf(file, "%d %fn", 42, 3.14159);
fclose(file);
file = fopen("data.txt", "r");
if (file == NULL) {
perror("Error opening file");
return 1;
}
int number;
float pi;
fscanf(file, "%d %f", &number, &pi);
fclose(file);
std::printf("Number: %d, Pi: %fn", number, pi);
return 0;
}
Output result:Number: 42, Pi: 3.141590
fopen and fclose
Used to open and close files.
Example
int main() {
FILE* file = fopen("example.txt", "r");
if (file) {
// File operations
fclose(file);
} else {
// Handle errors
}
return 0;
}
fread and fwrite
Used to read and write data from and to files.
Example
int main() {
FILE* file = fopen("example.bin", "wb");
if (file) {
int data = 12345;
fwrite(&data, sizeof(data), 1, file);
fclose(file);
}
return 0;
}
fseek and ftell
Used to move the file pointer in a file and get the position of the file pointer.
Example
int main() {
FILE* file = fopen("example.txt", "r");
if (file) {
fseek(file, 0, SEEK_END); // Move to end of file
long size = ftell(file); // Get file size
fclose(file);
}
return 0;
}
fflush
Flush the file stream to write the data in the buffer to the file.
Example
int main() {
FILE* file = fopen("example.txt", "w");
if (file) {
fputs("Hello, World!", file);
fflush(file); // Ensure data is immediately written to file
fclose(file);
}
return 0;
}
printf and fprintf
Used for formatted output to standard output or a file.
Example
int main() {
int value = 42;
printf("Value: %d\n", value); // Output to standard output
FILE* file = fopen("output.txt", "w");
if (file) {
fprintf(file, "Value: %d\n", value); // Output to file
fclose(file);
}
return 0;
}
scanf and fscanf
Used for formatted input from standard input or a file.
Example
int main() {
int value;
scanf("%d", &value); // Read from standard input
FILE* file = fopen("input.txt", "r");
if (file) {
fscanf(file, "%d", &value); // Read from file
fclose(file);
}
return 0;
}
sprintf and sscanf
Used for formatted output to a string and reading from a string.
Example
int main() {
char buffer[50];
int value = 42;
sprintf(buffer, "Value: %d", value); // Output to string
int readValue;
sscanf(buffer, "Value: %d", &readValue); // Read from string
return 0;
}
fgets and fputs
Used to read a string from a file and write a string to a file.
Example
int main() {
FILE* file = fopen("example.txt", "r");
if (file) {
char buffer[100];
if (fgets(buffer, sizeof(buffer), file)) {
// Read success
}
fclose(file);
}
file = fopen("example.txt", "w");
if (file) {
fputs("Hello, World!\n", file); // Write string to file
fclose(file);
}
return 0;
}
getc and putc
Used to read characters from a file and write characters to a file.
Example
int main() {
FILE* file = fopen("example.txt", "r");
if (file) {
int c = getc(file); // Read a character
fclose(file);
}
file = fopen("example.txt", "w");
if (file) {
putc('A', file); // Write a character
fclose(file);
}
return 0;
}
feof and ferror
Used to detect end-of-file and file errors.
Example
#include <cstdio>
int main() {
FILE* file = fopen("example.txt", "r");
if (file) {
while (!feof(file)) {
int c = getc(file);
if (ferror(file)) {
// Handle errors
break;
}
// Process character
}
fclose(file);
}
return 0;
}
EOF
Indicates the end-of-file flag.
Example
int main() {
FILE* file = fopen("example.txt", "r");
if (file) {
int c;
while ((c = getc(file)) != EOF) {
// Process character
}
fclose(file);
}
return 0;
}
NULL
Represents a null pointer.
Example
int main() {
FILE* file = fopen("example.txt", "r");
if (file == NULL) {
// Handle file open error
} else {
fclose(file);
}
return 0;
}
Notes
- Usage
<cstdio>When using the library, you need to ensure that file open and close are handled correctly to avoid resource leaks. - When using
fopenWhen using it, you need to provide the correct file path and mode. - Usage
fprintfandfscanfWhen using it, the format string should match the variable types.