国产精品999,99热在线观看,99国产精品99久久久久久,欧美性猛交99久久久久99按摩

滿滿干貨!c語言寫入文本文件-c語言讀取文本內(nèi)容函數(shù)

編寫一個 C 語言程序可以打開一個文本文件并寫入內(nèi)容到文件中,以及從文件中讀取內(nèi)容并打印到控制臺上。本篇文章將介紹這兩個功能的實現(xiàn)。

打開文本文件并將其寫入到文件中

要打開一個文本文件并將其寫入到文件中,我們需要使用 C 語言中的文件函數(shù)。文件函數(shù)可以打開文件并讀取文件中的內(nèi)容。下面是一個簡單的示例程序,用于打開名為“example.txt”的文件并將其寫入到名為“example.txt”的新文件中:

```c

#include

#include

#include

#define MAX_FILE_SIZE 1024 * 1024 // 最大文件大小

// 打開文件并讀取文件內(nèi)容

void open_file(char *filename, int *open_mode) {

FILE *file;

if (*open_mode == -1) {

printf("無法打開文件: %sn", filename);

return;

}

file = fopen(filename, *open_mode);

if (file == NULL) {

printf("無法打開文件: %sn", filename);

fclose(file);

return;

}

printf("文件 %s 已打開n", filename);

}

// 寫入文件內(nèi)容到文件中

void write_file(char *filename, const char *content, int *content_size) {

FILE *file;

if (*content_size == -1) {

printf("無法寫入文件: %sn", filename);

return;

}

file = fopen(filename, "w");

if (file == NULL) {

printf("無法寫入文件: %sn", filename);

fclose(file);

return;

}

fprintf(file, "%s", content);

if (fprintf(file, " ", 1) == 1) {

printf("格式錯誤: %sn", filename);

fclose(file);

return;

}

fclose(file);

printf("文件 %s 已寫入n", filename);

}

// 從文件中讀取文件內(nèi)容并打印到控制臺上

int read_file(char *filename, int *content_size) {

FILE *file;

if (*content_size == -1) {

printf("無法讀取文件: %sn", filename);

return -1;

}

file = fopen(filename, "r");

if (file == NULL) {

printf("無法讀取文件: %sn", filename);

fclose(file);

return -1;

}

char buffer[MAX_FILE_SIZE];

int bytes_read;

while ((bytes_read = fread(buffer, 1, MAX_FILE_SIZE - 1, file)) != -1) {

if (bytes_read == MAX_FILE_SIZE - 1) {

printf("文件 %s 已超出最大容量限制n", filename);

fclose(file);

return -1;

}

*content_size = bytes_read;

printf("文件 %s 中讀取到 %s 字節(jié)n", filename, buffer);

}

fclose(file);

printf("文件 %s 中讀取到 %s 字節(jié)n", filename, *content_size);

return *content_size;

}

int main() {

char filename[MAX_FILE_SIZE];

int content_size;

int result = read_file(filename, &content_size);

if (result == -1) {

printf("無法讀取文件: %sn", filename);

return -1;

}

printf("文件 %s 中讀取到 %d 字節(jié)n", filename, content_size);

return 0;

}

```

以上程序使用了 `open_file` 函數(shù)打開了名為“example.txt”的文件,并使用 `write_file` 函數(shù)將文件內(nèi)容寫入到了新文件中。在 `read_file` 函數(shù)中,我們使用 `fread` 函數(shù)從文件中讀取了 1024 字節(jié)的內(nèi)容,并將其存儲在 `buffer` 數(shù)組中,然后將其打印到控制臺上。最后,我們檢查了文件的大小是否超出最大容量限制,如果沒有,我們打印了文件的內(nèi)容。

從文件中讀取文本內(nèi)容函數(shù)

接下來,我們介紹從文件中讀取文本內(nèi)容函數(shù)。這個函數(shù)使用了 `read_file` 函數(shù)從文件中讀取內(nèi)容,并將其存儲在 `content_size` 變量中。我們可以使用 `content_size` 變量來打印文件的內(nèi)容。

下面是一個簡單的示例程序,用于從文件中讀取內(nèi)容并打印到控制臺上:

```c

#include

#include

#include

#define MAX_FILE_SIZE 1024 * 1024 // 最大文件大小

// 從文件中讀取內(nèi)容并打印到控制臺上

int read_file(char *filename, int *content_size) {

FILE *file;

if (*content_size == -1) {

printf("無法讀取文件: %sn", filename);

return -1;

}

file = fopen(filename, "r");

if (file == NULL) {

printf("無法讀取文件: %sn", filename);

fclose(file);

return -1;

}

char buffer[MAX_FILE_SIZE];

int bytes_read;

while ((bytes_read = fread(buffer, 1, MAX_FILE_SIZE - 1, file)) != -1) {

if (bytes_read == MAX_FILE_SIZE - 1) {

printf("文件 %s 已超出最大容量限制n", filename);

fclose(file);

return -1;

}

*content_size = bytes_read;

printf("文件 %s 中讀取到 %s 字節(jié)n", filename, buffer);

}

fclose(file);

printf("文件 %s 中讀取到 %s 字節(jié)n", filename, *content_size);

return *content_size;

}

int main() {

char filename[MAX_FILE_SIZE];

int content_size;

int result = read_file(filename, &content_size);

if (result == -1) {

printf("無法讀取文件: %sn", filename);

return -1;

}

printf("文件 %s 中讀取到 %d 字節(jié)n", filename, content_size);

return 0;

}

```

以上程序使用了 `read_file` 函數(shù)從文件中讀取內(nèi)容并打印到控制臺上。我們可以使用 `content_size` 變量來打印文件的內(nèi)容。最后,我們檢查了文件的大小是否超出最大容量限制,如果沒有,我們打印了文件的內(nèi)容。

以上就是【滿滿干貨!c語言寫入文本文件-c語言讀取文本內(nèi)容函數(shù)】的全部內(nèi)容。

微信:N915888888
(歡迎您前來咨詢)

評論

留下您的評論

發(fā) 表
微信:N915888888

微信:N915888888
(歡迎您前來咨詢)

微信:N915888888
阿克陶县| 社会| 淳安县| 铜陵市| 西畴县| 开封县| 白城市| 卢湾区| 苏尼特左旗| 许昌市| 黎城县| 白河县| 鄂温| 丘北县| 丹巴县| 镇江市| 遂平县| 连州市| 新绛县| 黄浦区| 舟山市| 闽侯县| 莱阳市| 株洲市| 资源县| 铁力市| 东明县| 穆棱市| 青铜峡市| 高清| 屏东市| 增城市| 贵阳市| 曲松县| 天水市| 金坛市| 长乐市| 昌邑市| 宜丰县| 新兴县| 汉川市|