C 无法从文件中读取

C 无法从文件中读取,c,file-io,C,File Io,我正在绞尽脑汁想弄明白为什么这行不通: fprintf(stdout, "filename: %s\n", filename); // looks great // open the requested file if ((fd = fopen(filename, "r")) == NULL) { fprintf(stdout, "Failed to open file: %s\n", filename); exit(EXIT_FAILURE); } else { fp

我正在绞尽脑汁想弄明白为什么这行不通:

fprintf(stdout, "filename: %s\n", filename); // looks great

// open the requested file
if ((fd = fopen(filename, "r")) == NULL) {
    fprintf(stdout, "Failed to open file: %s\n", filename);
    exit(EXIT_FAILURE);
} else {
    fprintf(stdout, "Successfully opened file: %s\n", filename);
}

do {
    bzero(buffer, BUFFSIZE);  // size 1024 bytes
    bytes_read = fread( &buffer, sizeof(char), BUFFSIZE, fd );
    fprintf(stdout, "buffer contents: %s\n", buffer);
} while (!feof(fd));

fclose(fd);
这是我的输出:

filename: test.txt
Successfully opened file: test.txt
buffer contents: 
buffer contents: 
buffer contents: 009
BEGIN_LINE010This is a test file with exactly 10 lines of readable textEND_LINE010
请注意,它已完全“丢失”文本文件中的前8+行。它清楚地打开了文件并读取了最后一行以及第9行的尾随3个字符。其他的线路到哪里去了?我完全不知所措

以下是我试图读取的测试文件的全部内容:

BEGIN_LINE000This is a test file with exactly 10 lines of readable textEND_LINE000
BEGIN_LINE001This is a test file with exactly 10 lines of readable textEND_LINE001
BEGIN_LINE002This is a test file with exactly 10 lines of readable textEND_LINE002
BEGIN_LINE003This is a test file with exactly 10 lines of readable textEND_LINE003
BEGIN_LINE004This is a test file with exactly 10 lines of readable textEND_LINE004
BEGIN_LINE005This is a test file with exactly 10 lines of readable textEND_LINE005
BEGIN_LINE006This is a test file with exactly 10 lines of readable textEND_LINE006
BEGIN_LINE007This is a test file with exactly 10 lines of readable textEND_LINE007
BEGIN_LINE008This is a test file with exactly 10 lines of readable textEND_LINE008
BEGIN_LINE009This is a test file with exactly 10 lines of readable textEND_LINE009
BEGIN_LINE010This is a test file with exactly 10 lines of readable textEND_LINE010

我认为您应该尝试以下代码:

   FILE *fp;
   char buff[255];

   char *filename="input.txt";

   fprintf(stdout, "filename: %s\n", filename); // looks great

   if ((fp = fopen(filename, "r")) == NULL) 
   {
        fprintf(stdout, "Failed to open file: %s\n", filename);
        exit(EXIT_FAILURE);
   }   
   else 
   {
        fprintf(stdout, "Successfully opened file: %s\n", filename);
   }   
   while ( !feof(fp ) )
   {
        memset(buff, '\0', sizeof( buff) );
        fgets(buff, 255, (FILE*)fp);
        printf("%s", buff );
   }
   fclose(fp);

问题是,混合使用文本模式打开文件和使用fread通常不是一个好主意

如果您以二进制模式rb打开文件,则fread应按预期工作

另一件事是fread不会\0终止它读取的任何内容,可能您试图通过调用bzero来终止缓冲区,但如果您读取整个BUFFSIZE字节数,缓冲区将没有空间容纳尾随的\0

相反,只需执行循环并打印每个字符或ascii值:

do 
{
  bytes_read = fread( &buffer, sizeof(char), BUFFSIZE, fd );
  fprintf(stdout, "buffer contents:");
  for ( i = 0; i < bytes_read; ++i)
  {
    fprintf( stdout, "%2X", buffer[i] );
  }
} 
while (!feof(fd));

我认为您发布的代码有一些语法错误…在哪里?它为我编译。我认为您在这一行中遗漏了一些内容fprintfst输出,缓冲区内容:%s\nfrintfst输出,缓冲区内容:%s\n,您在binart模式下打开了文件,并希望它打印字符串,阅读有关文件打开模式的信息,是否使用turbo C?谢谢。它几乎起作用了。它会遗漏前两行,并正确打印最后8行。奇怪…请把文件的内容也贴出来。你能告诉我如何用一种很好的格式来做吗?如果我只是复制粘贴它,它很难阅读。只需张贴前15行或something@usr55410把文件编辑成你的问题,而不是SAK的答案。你能再告诉我一件事吗?如何修改fprintf语句以将每个字符追加到另一个字符数组的末尾?我真的很想完成这件事。谢谢。使用sprintf和strcat将值构建成字符串。