Troubleshooting Infinite Loops in C File I/O

In summary, The program is supposed to read from two text files containing numbers in ascending order and output all the numbers in ascending order to a third file. However, the output file is reaching an infinite loop, repeating the last number forever. This may be due to using feof instead of EOF in the conditional statements. The last loop is also causing an issue by reading from i1 instead of i2. Assistance is needed to fix the problem.
  • #1
camel-man
76
0
I am supposed to read from 2 text files containing numbers in ascending order, then output the all the numbers in ascending order to an output file, For some reason my output file is reaching an infinite loop it orders the numbers correctly but the last number reapeats forever. I am thinking it has to do with my conditional statements such as feof, when maybe I should be using EOF. Can someone help, thanks.


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

int main(int argc, char* argv[])
{
FILE *i1;
FILE *i2;
FILE *o;

int num1, num2;

	if(argc!=4)
	{	
	printf("Error in valid number of arguments\n");
	exit(1);
	}

	
	if((i1=fopen(argv[1], "r"))==NULL)
	{
	printf("Failed to open First File\n");
	exit(1);
	}

	
	if((i2=fopen(argv[2],"r"))==NULL)
	{
	printf("Failed to open Second File\n");
	exit(1);
	}


	if((o=fopen(argv[3],"w"))==NULL)
	{
	printf("Failed to open Third File\n");
	exit(1);
	}


	while(!feof(i1) && !feof(i2))
	{
	fscanf(i1,"%d",&num1);
	fscanf(i2,"%d",&num2);

		if(num1<=num2)
		fprintf(o,"%d\n",num1);
		else
		fprintf(o,"%d\n",num2);
	}

	if(!feof(i1))	
	{
		fprintf(o,"%d\n",num1);
		while(!feof(i1))
		{
		fscanf(i1,"%d",&num1);
		fprintf(o,"%d\n",num1);
		}	
	}



	if(!feof(i2))
	{	
	fprintf(o,"%d\n",num2);
		while(!feof(i2))
		{
		fscanf(i1,"%d",&num2);
		fprintf(o,"%d\n",num2);
		}

	}



fclose(i1);
fclose(i2);
fclose(o);
return 0;

}
 
Technology news on Phys.org
  • #2
camel-man said:
Code:
		while(!feof(i2))
		{
		fscanf(i1,"%d",&num2);
                          ...
The problem is that you're reading from i1 in the last loop (while checking for eof from i2).
 
  • #3
I love you very much :) may you be a blessed and happy man for eternity.
 

1. What causes an infinite loop in FILE I/O in C?

There can be several reasons for an infinite loop in FILE I/O in C. One common reason is not properly closing the file after reading or writing to it. Another reason could be using the wrong loop condition, causing the program to continuously read or write to the file without ever reaching the end.

2. How do I avoid an infinite loop in FILE I/O in C?

To avoid an infinite loop in FILE I/O in C, make sure to properly close the file after reading or writing. Also, double check your loop condition to ensure it will eventually reach the end of the file. Using a break statement or a counter can also help prevent infinite loops.

3. Can a corrupted file cause an infinite loop in FILE I/O in C?

Yes, a corrupted file can cause an infinite loop in FILE I/O in C. If the file is not properly formatted or contains unexpected data, it can cause the program to continuously read or write to the file without reaching the end.

4. How do I fix an infinite loop in FILE I/O in C?

To fix an infinite loop in FILE I/O in C, first identify the cause of the loop. Then, make any necessary changes such as properly closing the file or fixing the loop condition. Using debugging tools and carefully reviewing your code can also help identify and fix the issue.

5. Are there any best practices for using FILE I/O in C to avoid infinite loops?

Yes, there are several best practices for using FILE I/O in C to avoid infinite loops. These include always properly closing the file, checking for errors when opening or reading/writing to a file, and using a clear and accurate loop condition. It is also helpful to familiarize yourself with common errors and how to handle them when working with FILE I/O.

Similar threads

  • Programming and Computer Science
Replies
4
Views
743
  • Programming and Computer Science
Replies
4
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
3K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
10
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
4K
Replies
2
Views
2K
Back
Top