How to implement file reading and writing operations in C language
The method of implementing file read and write operations in C language is: 1. Read operation, define a buffer buffer to store the file content, then use the "fopen()" function to open the file, obtain the file pointer, and then use a loop The structure reads the content of the file, and finally closes the file to release resources; 2. For writing operations, use the "fopen" function to open the txt file, and then the "fprintf()" function outputs the data contained in the character array "data" to the file in the specified format. , and finally the "fclose()" function closes the file.
Operating system for this tutorial: Windows 10 system, C99 version, Dell G3 computer.
C language implementation of file read and write operations
File reading operation:
#include <stdio.h> int main() { char buffer[1024]; // 用于存储文件内容的缓冲区 FILE* file = fopen("input.txt", "r"); // 打开文件 if (file == NULL) { printf("无法打开文件!\n"); return 1; // 返回错误码 } // 按行读取文件内容直到文件结束 while (fgets(buffer, sizeof(buffer), file)) { printf("%s", buffer); // 输出缓冲区内容 } // 关闭文件 fclose(file); return 0; // 返回成功码 }
In the above code, a buffer is first defined buffer to store the contents of the file. Then, use the standard library function fopen() to open the file and obtain the file pointer. If the file does not exist or cannot be accessed, the fopen() function returns NULL. Next, use a loop structure to read the file content, reading one line from the file into the buffer at a time until the end of file is encountered. Finally, the file is closed and the resources are released.
File writing operation:
The following is a simple c program demonstrating how to write data in a file:
#include <stdio.h>int main() { FILE *fp; char data[100] = "This is some text that has been written to a file.\n"; fp = fopen("example.txt", "w"); // 打开example.txt文件并创建文件指针 if(fp == NULL) { printf("Error opening file\n"); // 错误处理 return 1; } fprintf(fp, "%s", data); // 写入数据到文件中 fclose(fp); // 关闭文件 return 0; }
In this example , the fopen() function is used to open a file named "example.txt" for writing. If the file does not exist, it will be created. Then, the fprintf() function is used to output the data contained in the character array "data" to the file in the specified format, that is, the string type data "%s" is written to the file. Finally, use the fclose() function to close the file.
Note: When opening a file and performing a write operation, remember to close the file before the program ends.
The above is the detailed content of How to implement file reading and writing operations in C language. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver Mac version
Visual web development tools
