Reputation: 15
after tirelessly looking for an explanation I've decided to ask the greats at stackoverflow. So I'm currently trying to read each input line by line from a file called data.txt. The program works perfectly fine using simple scanf and such, but when I want to read input values from a file the program only reads the first 3 lines of the txt and it keeps repeating in an infinite loop. My code is shown below. I kept out the majority of my code in case others may be tempted to use it. Program will just read 1, 12, 0 infinitely. sample data.txt file is shown below
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
// Global variables
char *Hstring = NULL;
int maxLength, parity;
char *temp = NULL;
int userChoice = 0;
void option1() {
// User inputs length and even or odd parity bit
printf("\n*** Maximum Code Length: %d", maxLength);
//scanf("%d",&maxLength);
printf("\n*** Parity: %d", parity);
//scanf("%d",&parity);
// allocate memory for hamming string based on maximum length and
//size of character element
Hstring = (char *)malloc(maxLength * sizeof(char));
return;
}
void option2() {
/* declare local vars */
int aLength, k, parBits, parValue, errorPos, i, j;
/* prompt for hamming code as a "string"*/
printf("\nEnter the Hamming Code: ");
scanf("%s", Hstring);
temp = Hstring;
aLength = strlen(Hstring);
parBits = ceil(log(aLength) / log(2));
}
int main() {
FILE *fp;
fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("ERROR OPENING THE FILE\n");
}
fscanf(fp, "%d %d %d", &userChoice, &maxLength, &parity);
//end file open
while (userChoice != 3) {
printf("\nEnter Selection: %d", userChoice);
//scanf("%d",&userChoice);
switch (userChoice) {
case 1:option1();
break;
case 2:option2();
break;
case 3:
printf("\n*** Program Terminated Normally\n");
break;
default: printf("invalid input please input another number\n\n");
break;
}
}
/* print out menu, prompt for choice, and call appropriate procedure
until user quits */
return 1;
}
SAMPLE data.txt
1
12
0
2
1000
1
Code starts to loop when it reads the third integer(parity) in option1()
Any help would be appreciated. Thank you!
Upvotes: 0
Views: 751
Reputation: 44256
The answer here https://stackoverflow.com/a/53475412/4386427 is correctly describing the problem, i.e. that you have an endless loop because userChoice
is only read once.
Here is a suggestion for a fix.
int main() {
FILE *fp;
fp = fopen("data.txt", "r");
if (fp == NULL) {
printf("ERROR OPENING THE FILE\n");
}
while (userChoice != 3) {
// Check that exactly 3 items are read from the file.
// If not terminate the program
if (fscanf(fp, "%d %d %d", &userChoice, &maxLength, &parity) != 3) {
printf("Illegal input from file or EOF. Terminating program\n");
break;
}
switch (userChoice) {
case 1:option1();
break;
case 2:option2();
break;
case 3:
printf("\n*** Program Terminated Normally\n");
break;
default: printf("invalid input please input another number\n\n");
break;
}
}
return 1;
}
Upvotes: 0
Reputation: 392
You never modify userChoice
in your while loop, so it's gonna loop forever.
Anyways, even if you were using fscanf
in the while loop, and therefore reading the whole file until you find userChoice == 3
, it's a bad idea to have your loop termination condition only depending on the content of a file, you should also check the result of fscanf
for termination of the file. Your example data would still loop forever because it contains no 3
.
Upvotes: 1