Linux下使用C语言写文件 (linux c write file)

Introduction

Linux is an open-source operating system that is widely used in server environments and for scientific computing. The command-line interface and the utilities provided with Linux make it a powerful tool for developers and researchers. C is a popular programming language that is efficient and usually used to write system-level software. In this tutorial, we will explore how to write a file in C language using the Linux environment.

Creating and Opening a File

The first step for writing a file in C on Linux is to create and open the file. We can define a file variable using the data type “FILE” and a pointer variable using the data type “FILE *”. Next, we need to use the “fopen()” function to open the file with a specific mode. The modes are used to define the access and operation that we can perform on the file. The most commonly used modes are “r”, “w”, “a”, and “r+”. The “r” mode is used for reading the file, “w” mode is for opening the file for writing and deleting previous file contents, “a” mode is for opening the file for writing and appending the new data to the end of the file, and “r+” mode is for opening the file for both reading and writing.

Here is the code snippet to create and open the file in write mode:

“`

#include

#include

int mn() {

FILE *fp;

fp = fopen (“file.txt”, “w”);

if (fp == NULL) {

printf(“Error opening file!\n”);

exit(1);

}

fprintf(fp, “Hello World!\n”);

fclose(fp);

return 0;

}

“`

In the above code, we have opened the file “file.txt” in write mode and added a string “Hello World!” to the file. Further, we have used the “fclose()” function to close the file.

Writing to a File

After opening the file, we can use various functions to write data to the file. We can use the “fprintf()” function to print data to the file. The syntax of the function is similar to the “printf()” function. We can specify the stream to which we want to write the data, the format specifier, and the values that we want to print.

Here is the code example to write integer values to a file using fprintf():

“`

#include

#include

int mn() {

FILE *fp;

int i, n = 10;

fp = fopen (“numbers.txt”, “w”);

if (fp == NULL) {

printf(“Error opening file!\n”);

exit(1);

}

for (i = 0; i

fprintf(fp, “%d “, i);

fclose(fp);

return 0;

}

“`

In the above code, we have used a for loop to print the numbers from 0 to 9 to the file “numbers.txt”. We have printed the integers with a space in between using the “%d ” format specifier.

Reading from a File

Reading data from a file is also a common task in C programming. We can use the “fscanf()” function to read data from the file. The function works similar to the “scanf()” function used for input from the console. We need to pass the file stream, the format specifier for the data we want to read, and the variables where we want to store the data.

Here is the code example to read integers from a file using fscanf():

“`

#include

#include

int mn() {

FILE *fp;

int i, n = 10, num;

fp = fopen (“numbers.txt”, “r”);

if (fp == NULL) {

printf(“Error opening file!\n”);

exit(1);

}

for (i = 0; i

fscanf(fp, “%d”, &num);

printf(“%d\n”, num);

}

fclose(fp);

return 0;

}

“`

In the above code, we have opened the file “numbers.txt” in read mode and used a for loop to read integers from the file using the “fscanf()” function. We have printed the integers to the console using the “printf()” function.

Conclusion


数据运维技术 » Linux下使用C语言写文件 (linux c write file)