Troubleshooting Infinite Loops in C File I/O

Click For Summary
SUMMARY

The forum discussion addresses an infinite loop issue in a C program that reads from two text files and outputs their contents in ascending order. The problem arises from incorrect use of the file reading logic, specifically the misuse of the file pointer in the final loop, which reads from the first file instead of the second. The solution involves ensuring that the correct file pointer is used when checking for EOF conditions. The suggested fix is to replace the erroneous file pointer in the last loop to prevent the infinite loop.

PREREQUISITES
  • Understanding of C programming language
  • Familiarity with file I/O operations in C
  • Knowledge of EOF and feof() functions
  • Basic debugging techniques in C
NEXT STEPS
  • Review C file I/O operations and best practices
  • Learn about the differences between EOF and feof() in C
  • Explore debugging techniques for C programs
  • Study sorting algorithms and their implementation in C
USEFUL FOR

Programmers working with C, software developers troubleshooting file I/O issues, and anyone interested in optimizing file reading logic in C applications.

camel-man
Messages
76
Reaction score
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
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).
 
I love you very much :) may you be a blessed and happy man for eternity.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 2 ·
Replies
2
Views
4K
Replies
3
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
5K
Replies
2
Views
2K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 8 ·
Replies
8
Views
4K