Home > Common Problem > body text

How to implement file reading and writing operations in C language

尊渡假赌尊渡假赌尊渡假赌
Release: 2023-06-08 17:27:26
Original
4462 people have browsed it

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.

How to implement file reading and writing operations in C language

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; // 返回成功码
}
Copy after login

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;
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template