so I can't seem to redirect an input so that my program reads it.
It says in the assignment that the program should NOT print anything to prompt for user input. As many numbers have to be read from stdin to test your programs, you are expected to use INPUT REDIRECTION to read the input from a file.
I have this main function:
int main(int argc, char const *argv[])
{
int arr[100];
for (int i = 0; i < SIZE; i++)
{
values[i] = 0;
hashMapLinear[i] = 0;
}
FILE* file = fopen("file_name.txt", "r");
int index = 0;
int k = 0;
while (!feof(file))
{
fscanf(file, "%d", &k);
arr[index] = k;
index++;
}
fclose(file);
file = fopen("file_name.txt", "r");
int i = 0;
fscanf(file, "%d", &i);
float size = i;
fscanf(file, "%d", &i);
int thresh_hold = i;
int load_factor = size / 2;
int j = 0;
if (size <= 0)
{
printf("\nSize of file can not be zero or negative integer:\n");
}
else
{
while (!feof(file))
{
if (num_keys <= load_factor)
{
int check_valid_input= fscanf(file, "%d", &i);
if (check_valid_input != 0 || check_valid_input== -1)
{
insert_into_hashtable(i, size);
}
else
{
printf("\n invalid input:\n");
exit(1);
}
}
else
{
printf("\nError in inserting more numbers:\n");
exit(1);
}
}
fclose(file);
printHashMap(arr,size, thresh_hold);
}
}
How do I edit this main function so that it redirects seq.1 or any other text file to the C program? Any help would be appreciated!
stdin, which is already open when your program starts up. You don't have to callfopenat all. The redirection is done outside the program, by the shell, when you type./a.out < seq.1.feof()to control awhile()loop, That function does not work as the posted code expects. also need to avoid overflow of the array Suggest:while ( index < 100 && 1 == fscanf(file, "%d", &k) ) {fclose()and the second call tofopen()with a call torewind()gcc, at a minimum use:-Wall -Wextra -pedanticI also use:-Wconversion -std=gnu11)