C 将文本文件的全部内容复制到字符数组变量中

C 将文本文件的全部内容复制到字符数组变量中,c,file,C,File,我必须将名为Bond.in的文本文档的内容放入字符数组变量中。我尝试了几种方法将Bond.in的内容保存到字符数组中,但下面的方法似乎是唯一有效的方法。但是,每次我尝试使用my_getline函数(我们在类中编写的函数,必须使用)打印变量数组的内容时,它都会进入一个无限循环。我无法确定这是因为调用my_getline的for循环,还是Bond.in的内容没有正确复制到文本[]。任何指导都将不胜感激。另外,如果我遗漏了任何有帮助的内容,请告诉我 /* Include the standard

我必须将名为Bond.in的文本文档的内容放入字符数组变量中。我尝试了几种方法将Bond.in的内容保存到字符数组中,但下面的方法似乎是唯一有效的方法。但是,每次我尝试使用my_getline函数(我们在类中编写的函数,必须使用)打印变量数组的内容时,它都会进入一个无限循环。我无法确定这是因为调用my_getline的for循环,还是Bond.in的内容没有正确复制到文本[]。任何指导都将不胜感激。另外,如果我遗漏了任何有帮助的内容,请告诉我

/*   Include the standard input/output and string libraries             */
#include <stdio.h>
#include <string.h>

/*   Define the maximum lines allowed in an input text and NEWLINE for getline funct.   */
#define MAXPATTERN 15
#define MAXWORDS 150
#define NEWLINE '\n'

/*   function prototypes                                */
void my_getline(char text[]);
int find_string(char text[], char pattern[], int length_text, int length_pattern);

int main()
{
   FILE *fp;
   char text[MAXWORDS];
   int i = 0, j;
   char fileName[15] = "Bond.in";
   char pattern[MAXPATTERN], c;
   int length_text, length_pattern, count;

   fp = fopen(fileName, "r");
   if (fp == NULL)
   {
      printf("fopen failed.\n");
      return(-1);
   }

   while(feof(fp))
      text[i++] = fgetc(fp);
   text[i] = '\0';

   printf("%s has been copied.", fileName);

   for (j = 0; text[j] != EOF; j++)
   {
      my_getline(text);
      printf("%d  %s \n", j, text);
   }

   printf("Enter the pattern you would like to search for: ");
   scanf("%s", pattern);
   printf("\nYou have chosen to search for: %s\n", pattern);

   //printf("%s appears %d times in %s.\n", pattern, find_string(text, pattern, length_text, length_pattern), fileName);
   fclose(fp);

   return(0);
}

void my_getline(char text[])
{
   int i = 0;
   while ((text[i] = getchar()) != NEWLINE)
      ++i;
   text[i] = '\0';
}

您从未将
EOF
放入
text
,因此循环从未停止也就不足为奇了。您确实在末尾添加了一个
'\0'
,因此您的打印循环应该查找它。

这是:

   fp = fopen(fileName, "r");
   if (fp == NULL)
   {
      printf("fopen failed.\n");
      return(-1);
   }

   while(feof(fp))
      text[i++] = fgetc(fp);
   text[i] = '\0';
读取零字节,因为文件打开后,
feof(fp)
立即为false

还有这个

void my_getline(char text[])
{
   int i = 0;
   while ((text[i] = getchar()) != NEWLINE)
      ++i;
   text[i] = '\0';
}
text
中的所有内容替换为从
stdin
读取的数据,直到读入换行符,从而覆盖您可能已经读取的所有内容

这将读取文件内容并NUL终止它(假定POSIX环境中的文件内容中没有NUL字符):


错误检查只是一个练习。

我不明白您的程序试图做什么,编写程序是一种非常糟糕的方法,首先是
while(feof())
循环,然后是
for(;text[I]!=EOF;)
它没有重新编译背后的逻辑。最好提到
getchar()
fgetetc()
return
int
s,因此程序可能会溢出
text[i]
,这也是
EOF
无法存储在
text
中的原因。即使将for(j=0;text[j]!=EOF;j++)更改为for(j=0;text[j]!='\0';j++)后,我仍然有一个无限循环。为什么不满足这个条件?我不需要只看它的内容。我还需要将它的内容保存到一个字符中array@PatMulvihill
char*contents=calloc(1,sb.st_size+1)
是一个动态分配的字符数组。一个足够大的文件,可以容纳文件的全部内容,加上一个字节作为一个终止的NUL字符。
void my_getline(char text[])
{
   int i = 0;
   while ((text[i] = getchar()) != NEWLINE)
      ++i;
   text[i] = '\0';
}
int fd = open( filename, O_RDONLY );
struct stat sb;
fstat( fd, &sb );
char *contents = calloc( 1, sb.st_size + 1 );
read( fd, contents, sb.st_size );
close( fd );