所以我似乎无法重定向输入,以便我的程序读取它.
它在赋值中说程序不应该打印任何东西以提示用户输入.由于必须从stdin中读取许多数字来测试程序,因此您需要使用INPUT REDIRECTION来读取文件中的输入.
我有这个主要功能:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
如何编辑此主函数以便将seq.1或任何其他文本文件重定向到C程序?任何帮助,将不胜感激!
只需使用stdin而file不是打开stdin,不要关闭它.
在调用类似的程序时./program < seq.1,操作系统会将内容传递seq.1给您的程序,就好像它是通过控制台输入的一样.因此,使用stdin代表控制台输入的工作将完成工作.
请注意,stdin默认情况下,程序启动时已经打开.
| 归档时间: |
|
| 查看次数: |
2039 次 |
| 最近记录: |