如何在C中读取和打印txt文件的内容
How to read and print the contents of a txt file in C
我正在尝试寻找一种更简单的方式来读取文本文件。我以前从未用 C 编程过,所以这对我来说是全新的。我的目标是能够 运行 我的程序并让它自动打印到屏幕上。我下面的内容有效,但我每次都必须输入文件。任何帮助将不胜感激。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
printf("Enter name of a file you wish to see\n");
gets(file_name);
fp = fopen(file_name, "r"); // read mode
if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are:\n", file_name);
while((ch = fgetc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
return 0;
}
这是输出:
Enter name of a file you wish to see
warning: this program uses gets(), which is unsafe.
Data.txt
The contents of Data.txt file are:
1
2
3
4
5
6
7
8
9
10
有几种方法可以在无需用户干预的情况下定义文件名。在所有情况下,删除
printf("Enter name of a file you wish to see\n");
gets(file_name);
将gets(file_name);
替换为strcpy(file_name, "Data.txt");
。
您还需要 #include <string.h>
。
将file_name[25]
替换为file_name[] = "Data.txt"
将char ch, file_name[25];
替换为char ch; char *file_name = "Data.txt";
您还可以将字符串声明为常量:const char *file_name = "Data.txt";
.
将gets(file_name);
替换为snprintf(file_name, (sizeof(file_name)/sizeof(file_name[0]))-1, "Data.txt");
sizeof(file_name)/sizeof(file_name[0])
通过将总数组大小除以单个元素的长度来计算数组的最大长度。我们减1为字符串终止字符'[=22=]'
.
预留一个元素
snprintf()
将允许您以编程方式构建文件名。
移除, file_name[25]
.
将 fp = fopen(file_name, "r");
替换为 fp = fopen("Data.txt", "r");
。
将printf("The contents of %s file are:\n", file_name);
替换为printf("The contents of the file are:\n");
(注意功能丢失)
你会一直阅读 Data.txt 吗?如果是这样,您可以对文件名进行硬编码并将 gets(file_name);
替换为 char * file_name = "Data.txt"
。如果执行此操作,还会删除 file_name
的当前定义以避免重新定义错误。
我正在尝试寻找一种更简单的方式来读取文本文件。我以前从未用 C 编程过,所以这对我来说是全新的。我的目标是能够 运行 我的程序并让它自动打印到屏幕上。我下面的内容有效,但我每次都必须输入文件。任何帮助将不胜感激。
#include <stdio.h>
#include <stdlib.h>
int main()
{
char ch, file_name[25];
FILE *fp;
printf("Enter name of a file you wish to see\n");
gets(file_name);
fp = fopen(file_name, "r"); // read mode
if (fp == NULL)
{
perror("Error while opening the file.\n");
exit(EXIT_FAILURE);
}
printf("The contents of %s file are:\n", file_name);
while((ch = fgetc(fp)) != EOF)
printf("%c", ch);
fclose(fp);
return 0;
}
这是输出:
Enter name of a file you wish to see
warning: this program uses gets(), which is unsafe.
Data.txt
The contents of Data.txt file are:
1
2
3
4
5
6
7
8
9
10
有几种方法可以在无需用户干预的情况下定义文件名。在所有情况下,删除
printf("Enter name of a file you wish to see\n");
gets(file_name);
将gets(file_name);
替换为strcpy(file_name, "Data.txt");
。
您还需要 #include <string.h>
。
将file_name[25]
替换为file_name[] = "Data.txt"
将char ch, file_name[25];
替换为char ch; char *file_name = "Data.txt";
您还可以将字符串声明为常量:const char *file_name = "Data.txt";
.
将gets(file_name);
替换为snprintf(file_name, (sizeof(file_name)/sizeof(file_name[0]))-1, "Data.txt");
sizeof(file_name)/sizeof(file_name[0])
通过将总数组大小除以单个元素的长度来计算数组的最大长度。我们减1为字符串终止字符'[=22=]'
.
snprintf()
将允许您以编程方式构建文件名。
移除, file_name[25]
.
将 fp = fopen(file_name, "r");
替换为 fp = fopen("Data.txt", "r");
。
将printf("The contents of %s file are:\n", file_name);
替换为printf("The contents of the file are:\n");
(注意功能丢失)
你会一直阅读 Data.txt 吗?如果是这样,您可以对文件名进行硬编码并将 gets(file_name);
替换为 char * file_name = "Data.txt"
。如果执行此操作,还会删除 file_name
的当前定义以避免重新定义错误。