c语言读取文件并打印
时间: 2023-11-13 22:02:18 浏览: 65
C语言可以使用fopen函数打开文件,使用fread函数读取文件内容,使用printf函数将读取到的内容打印到终端上,最后使用fclose函数关闭文件。下面是一个简单的示例代码:
```
#include <stdio.h>
int main() {
FILE *fp;
char buffer[1024];
int len;
fp = fopen("example.txt", "r");
if (fp == NULL) {
printf("Failed to open file\n");
return 1;
}
while ((len = fread(buffer, sizeof(char), sizeof(buffer), fp)) > 0) {
printf("%.*s", len, buffer);
}
fclose(fp);
return 0;
}
```
这个程序会打开名为"example.txt"的文件,读取文件内容并将其打印到终端上,最后关闭文件。需要注意的是,如果文件不存在或者无法打开,程序会输出一条错误信息并退出。
相关问题
c语言读取二进制文件并打印
以下是一个简单的c语言程序,用于读取二进制文件并将其打印到屏幕上:
```c
#include <stdio.h>
int main()
{
FILE *fp;
char buffer[100];
fp = fopen("binaryfile.bin", "rb"); // 打开二进制文件
if (fp == NULL) // 如果文件无法打开,输出错误信息并退出程序
{
printf("Error opening file\n");
return 1;
}
while (fread(buffer, sizeof(char), 100, fp) == 100) // 每次读取100个字节,并打印到屏幕上
{
printf("%s", buffer);
}
fclose(fp); // 关闭文件
return 0;
}
```
在上面的代码中,我们使用了fopen函数来打开二进制文件。其中,第一个参数是文件名,第二个参数是文件模式。模式中的“rb”表示以二进制模式打开文件。
在while循环中,我们使用了fread函数来读取文件中的内容。该函数的第一个参数是一个指向内存块的指针,第二个参数是每个元素的大小,第三个参数是要读取的元素数量,第四个参数是文件指针。在本例中,我们每次读取100个字节,并将它们存储在一个字符缓冲区中。然后,我们使用printf函数将缓冲区中的内容打印到屏幕上。
最后,我们使用fclose函数来关闭文件。
c语言链表文件存入链表并读取
C语言中的链表是一种常用的数据结构,它可以动态地存储和访问数据。在文件操作中,我们可以通过将链表中的数据存入文件中,也可以通过读取文件中的数据来构造链表。下面是一个简单的示例,演示了如何将链表中的数据存入文件并读取:
```
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int data;
struct node *next;
} Node;
void insert(Node **head, int data) {
Node *new_node = (Node*)malloc(sizeof(Node));
new_node->data = data;
new_node->next = *head;
*head = new_node;
}
void print_list(Node *head) {
Node *current = head;
while(current != NULL) {
printf("%d ", current->data);
current = current->next;
}
}
void save_list(Node *head, const char* filename) {
FILE *fp = fopen(filename, "w");
Node *current = head;
while(current != NULL) {
fprintf(fp, "%d ", current->data);
current = current->next;
}
fclose(fp);
}
void load_list(Node **head, const char* filename) {
FILE *fp = fopen(filename, "r");
int data;
while(fscanf(fp, "%d ", &data) != EOF) {
insert(head, data);
}
fclose(fp);
}
int main() {
Node *head = NULL;
insert(&head, 3);
insert(&head, 5);
insert(&head, 7);
printf("Original list: ");
print_list(head);
save_list(head, "list.txt");
printf("\nList saved to file.\n");
Node *new_head = NULL;
load_list(&new_head, "list.txt");
printf("Loaded list: ");
print_list(new_head);
return 0;
}
```
在这个示例中,我们定义了一个链表结构体`Node`,包含了一个整数类型的数据`data`和一个指向下一个节点的指针`next`。我们通过`insert`函数向链表中插入新的节点,并通过`print_list`函数打印链表中的所有数据。
在`save_list`函数中,我们打开一个文件,并将链表中的每个节点的数据依次写入文件中。在`load_list`函数中,我们打开这个文件,并逐行读取文件中的数据,然后通过`insert`函数将这些数据插入到一个新的链表中。
在`main`函数中,我们先创建一个链表并向其中插入一些数据,然后调用`save_list`函数将链表中的数据存入一个文件中。接着,我们调用`load_list`函数读取这个文件,并构造出一个新的链表。最后,我们分别打印原始链表和新的链表中的所有数据。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)