如何将文本文件存储到 C 中的数组中
How do I store a text file into an array in C
我正在尝试打开用户输入的文本文件并读取该文本文件,但一次打印文本文件 60 个字符,所以我认为为了让我这样做,我需要将文本存储到数组,如果一行超过 60 个字符,则应另起一行。但是,当我 运行 下面的代码显示一条错误消息时说:C^@
#include <stdio.h>
#include <stdlib.h>
int main()
{
char arr[];
arr[count] = '[=10=]';
char ch, file_name[25];
FILE *fp;
printf("Enter file name: \n");
gets(file_name);
fp = fopen(file_name,"r"); // reading the file
if( fp == NULL )
{
perror("This file does not exist\n"); //if file cannot be found print error message
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
while( ( ch = fgetc(fp) ) != EOF ){
arr[count] = ch;
count++;
printf("%s", arr);}
fclose(fp);
return 0;
}
fgetc 总是读取下一个字符,直到 EOF。使用 fgets() 代替:
char *fgets(char *s, int size, FILE *stream)
fgets() reads in at most one less than size characters from stream and
stores them into the buffer pointed to by s. Reading stops after an EOF
or a newline. If a newline is read, it is stored into the buffer. A
terminating null byte (aq[=10=]aq) is stored after the last character in the
buffer.
三个问题:
变量count
没有初始化,所以它的值是不确定的,使用它会导致未定义的行为。
调用 printf(arr)
将 arr
视为字符串,但 arr
未终止,这再次导致 未定义的行为.
count
的增量在外循环。
要解决前两个问题,您必须先将 count
初始化为零,然后必须在循环后终止字符串:
arr[count] = '[=10=]';
但是,您的printf(arr)
调用仍然非常有问题,如果用户输入一些printf
格式化代码,会发生什么?这就是为什么你应该 永远不会 使用用户提供的输入字符串调用 printf
,而不是简单地做
printf("%s", arr);
如果你读取的文件内容超过59个字符,你也会遇到很大的问题,然后你就会溢出数组。
1) 您的 while
循环未正确分隔。在没有 { }
块的情况下,指令 arr[count] = ch;
是唯一重复的指令。
我想它也应该包括 count
的增量
while( ( ch = fgetc(fp) ) != EOF )
{
arr[count] = ch;
count++;
....
}
除其他外(测试计数器等)。
2) 没有必要读取和存储在数组中。完全可以每个字符一读就转,需要的时候加一个换行符(换行,超过60个限制)。
char arr[];
是invalid.you需要指定尺寸。
array[count] = '[=12=]';
: 计数未初始化。
gets(file_name);
: gets 已弃用 dangerous.use 另一个函数,如 scanf.
试试下面的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ch , count = 0;
char file_name[25];
FILE *fp;
printf("Enter file name: \n");
scanf(" %24s",file_name);
fp = fopen(file_name,"r"); // reading the file
if( fp == NULL )
{
perror("This file does not exist\n"); //if file cannot be found print error message
exit(EXIT_FAILURE);
}
fseek(fp, 0L, SEEK_END);
long sz = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char arr[sz];
while( ( ch = fgetc(fp) ) != EOF )
{
if( count < sz )
{
arr[count] = ch;
count++;
}
}
arr[sz] = '[=10=]';
printf("The contents of %s file are :\n", file_name);
printf("arr : %s\n",arr);
fclose(fp);
return 0;
}
我正在尝试打开用户输入的文本文件并读取该文本文件,但一次打印文本文件 60 个字符,所以我认为为了让我这样做,我需要将文本存储到数组,如果一行超过 60 个字符,则应另起一行。但是,当我 运行 下面的代码显示一条错误消息时说:C^@
#include <stdio.h>
#include <stdlib.h>
int main()
{
char arr[];
arr[count] = '[=10=]';
char ch, file_name[25];
FILE *fp;
printf("Enter file name: \n");
gets(file_name);
fp = fopen(file_name,"r"); // reading the file
if( fp == NULL )
{
perror("This file does not exist\n"); //if file cannot be found print error message
exit(EXIT_FAILURE);
}
printf("The contents of %s file are :\n", file_name);
while( ( ch = fgetc(fp) ) != EOF ){
arr[count] = ch;
count++;
printf("%s", arr);}
fclose(fp);
return 0;
}
fgetc 总是读取下一个字符,直到 EOF。使用 fgets() 代替:
char *fgets(char *s, int size, FILE *stream)
fgets() reads in at most one less than size characters from stream and
stores them into the buffer pointed to by s. Reading stops after an EOF
or a newline. If a newline is read, it is stored into the buffer. A
terminating null byte (aq[=10=]aq) is stored after the last character in the
buffer.
三个问题:
变量
count
没有初始化,所以它的值是不确定的,使用它会导致未定义的行为。调用
printf(arr)
将arr
视为字符串,但arr
未终止,这再次导致 未定义的行为.count
的增量在外循环。
要解决前两个问题,您必须先将 count
初始化为零,然后必须在循环后终止字符串:
arr[count] = '[=10=]';
但是,您的printf(arr)
调用仍然非常有问题,如果用户输入一些printf
格式化代码,会发生什么?这就是为什么你应该 永远不会 使用用户提供的输入字符串调用 printf
,而不是简单地做
printf("%s", arr);
如果你读取的文件内容超过59个字符,你也会遇到很大的问题,然后你就会溢出数组。
1) 您的 while
循环未正确分隔。在没有 { }
块的情况下,指令 arr[count] = ch;
是唯一重复的指令。
我想它也应该包括 count
的增量
while( ( ch = fgetc(fp) ) != EOF )
{
arr[count] = ch;
count++;
....
}
除其他外(测试计数器等)。
2) 没有必要读取和存储在数组中。完全可以每个字符一读就转,需要的时候加一个换行符(换行,超过60个限制)。
char arr[];
是invalid.you需要指定尺寸。
array[count] = '[=12=]';
: 计数未初始化。
gets(file_name);
: gets 已弃用 dangerous.use 另一个函数,如 scanf.
试试下面的代码:
#include <stdio.h>
#include <stdlib.h>
int main()
{
int ch , count = 0;
char file_name[25];
FILE *fp;
printf("Enter file name: \n");
scanf(" %24s",file_name);
fp = fopen(file_name,"r"); // reading the file
if( fp == NULL )
{
perror("This file does not exist\n"); //if file cannot be found print error message
exit(EXIT_FAILURE);
}
fseek(fp, 0L, SEEK_END);
long sz = ftell(fp);
fseek(fp, 0L, SEEK_SET);
char arr[sz];
while( ( ch = fgetc(fp) ) != EOF )
{
if( count < sz )
{
arr[count] = ch;
count++;
}
}
arr[sz] = '[=10=]';
printf("The contents of %s file are :\n", file_name);
printf("arr : %s\n",arr);
fclose(fp);
return 0;
}