YouTip LogoYouTip

Cpp Libs Cstdio

`` is a header file in the C++ standard library that provides C++ wrappers for the C standard I/O library, primarily used for file input and output operations. The `` library defines a set of functions for performing input and output operations, which can be used for reading and writing files and the console. ### Syntax Before using functions from the `` library, you need to include this header file at the top of your C++ program: #include ### Common Functions The `` library contains many functions for file I/O. Here are some commonly used functions: * `fopen`: Open a file. * `fclose`: Close a 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. ## Examples 1. Opening and closing a file: ## Examples #include 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. Writing data to a file: ## Examples #include 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; } "Hello, World!" is written to the "example.txt" file. 3. Reading data from a file: ## Examples #include #include int main(){ FILE*file =fopen("example.txt", "r"); if(file ==NULL){ perror("Error opening file"); return 1; } char buffer; while(fgets(buffer, 100, file)!=NULL){ std::cout<< buffer; } fclose(file); return 0; } Reads and outputs "Hello, World!" from the "example.txt" file. 4. Using fscanf and fprintf for formatted input/output: ## Examples #include 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: `Number: 42, Pi: 3.141590` ### fopen and fclose Used to open and close files. ## Examples #include int main(){ FILE* file =fopen("example.txt", "r"); if(file){ // File operations fclose(file); }else{ // Handle error } return 0; } ### fread and fwrite Used to read and write data from/to files. ## Examples #include 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. ## Examples #include 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 a file stream, writing data from the buffer to the file. ## Examples #include int main(){ FILE* file =fopen("example.txt", "w"); if(file){ fputs("Hello, World!", file); fflush(file);// Ensure data is written immediately fclose(file); } return 0; } ### printf and fprintf Used for formatted output to standard output or files. ## Examples #include int main(){ int value =42; printf("Value: %dn", value);// Output to standard output FILE* file =fopen("output.txt", "w"); if(file){ fprintf(file, "Value: %dn", value);// Output to file fclose(file); } return 0; } ### scanf and fscanf Used for formatted input from standard input or files. ## Examples #include 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 strings and reading from strings. ## Examples #include int main(){ char buffer; 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 strings from files and write strings to files. ## Examples #include int main(){ FILE* file =fopen("example.txt", "r"); if(file){ char buffer; if(fgets(buffer, sizeof(buffer), file)){ // Read successful } 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 files and write characters to files. ## Examples #include int main(){ FILE* file =fopen("example.txt", "r"); if(file){ int c =getc(file);// Read one character fclose(file); } file =fopen("example.txt", "w"); if(file){ putc('A', file);// Write one character fclose(file); } return 0; } ### feof and ferror Used to detect end of file and file errors. ## Examples #include int main(){ FILE* file =fopen("example.txt", "r"); if(file){ while(!
← Cpp Libs MemoryCpp Libs Exception β†’