合并文件的C程序找不到EOF,陷入无限循环!

发布于 2024-09-18 22:07:40 字数 1741 浏览 5 评论 0原文

我在尝试运行我正在开发的这个程序时遇到一些问题...要求说我不允许使用排序功能...我自己做了一些事情...等等。

几乎,该程序编译但在执行后挂起...我猜它陷入了无限循环...但我似乎找不到它...:(

该程序读取已经存在的数据文件从最小到最大排序并将它们合并(排序)到第三个 .txt 文件中...

这两个文件是 Data1.txt 和 Data2.txt 包含:

Data1.txt

2
2
2
2

Data2.txt

1
3
5
7
9

merge.c

#include <stdio.h>
#include <stdlib.h>


void sortData(FILE *fpData1, FILE *fpData2)
{

 int n, m; 
 FILE *fpMerge;


 fpMerge = fopen("Merge.txt", "w+");


 fscanf(fpData2, "%i", &n);
 fscanf(fpData1, "%i", &m);
 while(n != EOF || m != EOF)
 {

  if(n == EOF)
  {
   fscanf(fpData1, "%i", &m);

   while(m != EOF)
   {

    fprintf(fpMerge, "%i\n", m);
    fscanf(fpData1, "%i", &m);
   }
  }
  if(m == EOF)
  {
   fscanf(fpData2, "%i", &n);

   while(n != EOF)
   {

    fprintf(fpMerge, "%i\n", n);
    fscanf(fpData2, "%i", &n);
   }

  }

  if(n < m)
  {
   fprintf(fpMerge, "%i\n", n);
   fscanf(fpData2, "%i", &n);
  }
  if(n > m)
  {
   fprintf(fpMerge, "%i\n", m);
   fscanf(fpData1, "%i", &m);
  }
  if(n == m)
  {
   fprintf(fpMerge, "%i\n", n); 
   fprintf(fpMerge, "%i\n", m);
   fscanf(fpData2, "%i", &n);
   fscanf(fpData1, "%i", &m);

  }
 }

 fclose(fpMerge);
}

int main (void)
{
 FILE *fpData1;
 FILE *fpData2;


 fpData1 = fopen("Data1.txt", "r");
 if(fpData1 == NULL)
 {
  printf("There was an error opening the file...program terminating..\n");
  exit(1);
 }

 fpData2 = fopen("Data2.txt", "r");
 if(fpData2 == NULL)
 {
  printf("There was an error opening the file...program terminating..\n");
  exit(1);
 }



 sortData(fpData1, fpData2);


 fclose(fpData1);
 fclose(fpData2); 

 return 0;
}

I'm having some problems trying to run this program I am working on...The requirements say I was not allowed to use a sort function...I had do something myself....etc.

Pretty much, the program compiles but hangs after executed...I'm guessing it's stuck in an infinite loop...but I can't seem to find it... :(

This program reads to data files that will already be ordered least to greatest and merges them (ordered) into a third .txt file...

The two files are Data1.txt and Data2.txt
contains:

Data1.txt

2
2
2
2

Data2.txt

1
3
5
7
9

combine.c

#include <stdio.h>
#include <stdlib.h>


void sortData(FILE *fpData1, FILE *fpData2)
{

 int n, m; 
 FILE *fpMerge;


 fpMerge = fopen("Merge.txt", "w+");


 fscanf(fpData2, "%i", &n);
 fscanf(fpData1, "%i", &m);
 while(n != EOF || m != EOF)
 {

  if(n == EOF)
  {
   fscanf(fpData1, "%i", &m);

   while(m != EOF)
   {

    fprintf(fpMerge, "%i\n", m);
    fscanf(fpData1, "%i", &m);
   }
  }
  if(m == EOF)
  {
   fscanf(fpData2, "%i", &n);

   while(n != EOF)
   {

    fprintf(fpMerge, "%i\n", n);
    fscanf(fpData2, "%i", &n);
   }

  }

  if(n < m)
  {
   fprintf(fpMerge, "%i\n", n);
   fscanf(fpData2, "%i", &n);
  }
  if(n > m)
  {
   fprintf(fpMerge, "%i\n", m);
   fscanf(fpData1, "%i", &m);
  }
  if(n == m)
  {
   fprintf(fpMerge, "%i\n", n); 
   fprintf(fpMerge, "%i\n", m);
   fscanf(fpData2, "%i", &n);
   fscanf(fpData1, "%i", &m);

  }
 }

 fclose(fpMerge);
}

int main (void)
{
 FILE *fpData1;
 FILE *fpData2;


 fpData1 = fopen("Data1.txt", "r");
 if(fpData1 == NULL)
 {
  printf("There was an error opening the file...program terminating..\n");
  exit(1);
 }

 fpData2 = fopen("Data2.txt", "r");
 if(fpData2 == NULL)
 {
  printf("There was an error opening the file...program terminating..\n");
  exit(1);
 }



 sortData(fpData1, fpData2);


 fclose(fpData1);
 fclose(fpData2); 

 return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

枫以 2024-09-25 22:07:40

您不想比较 n != EOF,而是比较 fscanf 的返回值:

int count_1;
count_1 = fscanf(fpData1, "%i", &m);
if (count_1 == EOF) // EOF (or error)
{
  // ...
}

fscanf 也会返回 EOF< /code> 出错。如果您需要区分 EOF 和错误条件,请使用 ferror(fpData1),然后查找错误代码(存储在 errno 中)。

You don't want to compare n != EOF, but rather the return value of fscanf:

int count_1;
count_1 = fscanf(fpData1, "%i", &m);
if (count_1 == EOF) // EOF (or error)
{
  // ...
}

fscanf will also return EOF on error. If you need to tell EOF and error conditions apart, use ferror(fpData1), say, and then look up the error code (stored in errno).

桃扇骨 2024-09-25 22:07:40

您对 EOF 的测试不太正确

void sortData(FILE *fpData1, FILE *fpData2)
{
    int data1;
    int data2; 
    FILE *fpMerge;

    fpMerge = fopen("Merge.txt", "w+");
    fscanf(fpData1, "%i", &data1);
    fscanf(fpData2, "%i", &data2);

    // While one file still has data        
    while(!feof(fpData1) && !feof(fpData2))
    {
        // Choose 1 file to test
        // Read from that file and put into merge file until either we
        // run out of data or the condition fails.
        if(data1 < data2)
        {
            do {fprintf(fpMerge, "%i\n", data1);}
            while ((fscanf(fpData1, "%i", &data1) != 0) && (data1 <= data2));
        }
        else
        {
            do {fprintf(fpMerge, "%i\n", data2);}
            while ((fscanf(fpData2, "%i", &data2) != 0) && (data2 <= data1));
        }
        // NOTE: if fscanf() returns 0 it has failed to read (EOF)
    }
    // One of the files has reached the EOF
    // Dump the other file.
    while(fscanf(fpData1, "%i", &data1) != 0) {fprintf(fpMerge, "%i\n", data1);}
    while(fscanf(fpData2, "%i", &data2) != 0) {fprintf(fpMerge, "%i\n", data2);}
}

Your testing of EOF is not quite correct

void sortData(FILE *fpData1, FILE *fpData2)
{
    int data1;
    int data2; 
    FILE *fpMerge;

    fpMerge = fopen("Merge.txt", "w+");
    fscanf(fpData1, "%i", &data1);
    fscanf(fpData2, "%i", &data2);

    // While one file still has data        
    while(!feof(fpData1) && !feof(fpData2))
    {
        // Choose 1 file to test
        // Read from that file and put into merge file until either we
        // run out of data or the condition fails.
        if(data1 < data2)
        {
            do {fprintf(fpMerge, "%i\n", data1);}
            while ((fscanf(fpData1, "%i", &data1) != 0) && (data1 <= data2));
        }
        else
        {
            do {fprintf(fpMerge, "%i\n", data2);}
            while ((fscanf(fpData2, "%i", &data2) != 0) && (data2 <= data1));
        }
        // NOTE: if fscanf() returns 0 it has failed to read (EOF)
    }
    // One of the files has reached the EOF
    // Dump the other file.
    while(fscanf(fpData1, "%i", &data1) != 0) {fprintf(fpMerge, "%i\n", data1);}
    while(fscanf(fpData2, "%i", &data2) != 0) {fprintf(fpMerge, "%i\n", data2);}
}
野侃 2024-09-25 22:07:40

EOF 不是一个字符。
EOF 不是整数。
任何文件中都不会包含 EOF(既不是 char 也不是 int)。

EOF 是一个条件。
文件要么处于该状态,要么(通常)不处于该状态。

您应该检查fscanf()的返回值以检测EOF或其他问题。

EOF is not a character.
EOF is not an integer.
No files will ever have EOF (neither char nor int) in them.

EOF is a condition.
Files will either be on that condition or (usually) not.

You should check the return value of fscanf() to detect EOF or other problems.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文