Eclipse CDT Mars:来自文件的输入
Eclipse CDT Mars: Input from a file
我最近为 运行 C 程序安装了 Eclipse CDT Mars 版本。我在从文件中获取输入时遇到问题。所以,我有一个看起来像这样的程序:
int main(void) {
//Declarations
int number_of_segments;
int k_constraint;
segment_node *ptr_to_segment_array, *ptr_to_segment_sorted_array;
int no_of_coordinates;
int *ptr_to_solution_array;
int *ptr_to_list_of_segment_identifiers_currently;
int no_of_identifiers_currently=1;
int i,j=1,k=0, l=0;
//setvbuf(stdout, NULL, _IOLBF, 0);
//Input
printf("Enter the number of segments\n");
scanf("%d", &number_of_segments);
printf("Enter the k constraint \n");
scanf("%d", &k_constraint);
no_of_coordinates = number_of_segments*2;
//Dynamically allocate memory to the Array
ptr_to_segment_array = (segment_node*)malloc(sizeof(segment_node)*no_of_coordinates);
ptr_to_segment_sorted_array = (segment_node*)malloc(sizeof(segment_node)*no_of_coordinates);
ptr_to_solution_array = (int*)malloc(sizeof(int)*no_of_coordinates);
ptr_to_list_of_segment_identifiers_currently = (int*)malloc(sizeof(int)*number_of_segments);
/*Now, input the individual segments's coordinates from left to right
** while also assigning the unique numbers to an individual segments' coordinates*/
printf("Enter the coordinates of segments from left to right \n");
for(i=0; i<no_of_coordinates; i++,j++){
scanf(" %d", &(ptr_to_segment_array[i].coordinate));
if(j==1){
ptr_to_segment_array[i].position = 'l';
ptr_to_segment_array[i].identifier = i+1;
}
else if(j==2){
ptr_to_segment_array[i].position = 'r';
ptr_to_segment_array[i].identifier = i+1;
}
if(j==2){
//Reset
j=1;
}
}
return 0;
}
嗯,当然不是整个程序。这是它的一瞥。问题是 scanf 语句应该接受输入,而我希望从文件中获取输入。所以,我做了以下操作:
- 首先,我在工作区目录中创建了一个包含以下内容(输入)的文本文件。
3
2
-3
2
0
5
3
8
- 然后,我将 Eclipse 配置为从控制台获取输入。
请注意,编码已设置为 MS932(默认)
现在,我构建并调试它。我进入输入步骤。然后,我等了几分钟,什么也没有。而已。该程序似乎需要很长时间,但控制不会移动到下一行。
现在,您可能认为程序本身存在一些错误,是的,它可能是,但我也在 Ideone.com 上使用自定义输入 运行 这个程序,它确实接受了输入。此外,我 运行 这个程序使用来自控制台的输入,它确实一步一步正确地接受了输入。
那么,为什么它不以这种方式直接从文件中获取输入呢?是 Notepad/Eclipse 还是其他编码问题?
感谢任何帮助。
顺便说一句,如果有人想要完整的程序,我很乐意提供。它不是一些专有内容。我写这篇文章纯粹是为了教育目的,作为一些问题的解决方案。
我还没有看到你告诉你的程序从文件中读取。所以它只是等待您的输入。
您应该在开头添加:
freopen("input.txt", "r", stdin);
它的作用:reopen
s stdin
文件描述符并将 input.txt
附加到它。它附上它供阅读 - 第二个参数是 "r"
但是如果你想能够从控制台和文件中读取,你需要再添加一个:
FILE *in;
in = fopen("input.txt", "r");
并且您应该将 scanf(...)
替换为 fscanf(in, ...)
fscanf(in, "%d", number);
我最近为 运行 C 程序安装了 Eclipse CDT Mars 版本。我在从文件中获取输入时遇到问题。所以,我有一个看起来像这样的程序:
int main(void) {
//Declarations
int number_of_segments;
int k_constraint;
segment_node *ptr_to_segment_array, *ptr_to_segment_sorted_array;
int no_of_coordinates;
int *ptr_to_solution_array;
int *ptr_to_list_of_segment_identifiers_currently;
int no_of_identifiers_currently=1;
int i,j=1,k=0, l=0;
//setvbuf(stdout, NULL, _IOLBF, 0);
//Input
printf("Enter the number of segments\n");
scanf("%d", &number_of_segments);
printf("Enter the k constraint \n");
scanf("%d", &k_constraint);
no_of_coordinates = number_of_segments*2;
//Dynamically allocate memory to the Array
ptr_to_segment_array = (segment_node*)malloc(sizeof(segment_node)*no_of_coordinates);
ptr_to_segment_sorted_array = (segment_node*)malloc(sizeof(segment_node)*no_of_coordinates);
ptr_to_solution_array = (int*)malloc(sizeof(int)*no_of_coordinates);
ptr_to_list_of_segment_identifiers_currently = (int*)malloc(sizeof(int)*number_of_segments);
/*Now, input the individual segments's coordinates from left to right
** while also assigning the unique numbers to an individual segments' coordinates*/
printf("Enter the coordinates of segments from left to right \n");
for(i=0; i<no_of_coordinates; i++,j++){
scanf(" %d", &(ptr_to_segment_array[i].coordinate));
if(j==1){
ptr_to_segment_array[i].position = 'l';
ptr_to_segment_array[i].identifier = i+1;
}
else if(j==2){
ptr_to_segment_array[i].position = 'r';
ptr_to_segment_array[i].identifier = i+1;
}
if(j==2){
//Reset
j=1;
}
}
return 0;
}
嗯,当然不是整个程序。这是它的一瞥。问题是 scanf 语句应该接受输入,而我希望从文件中获取输入。所以,我做了以下操作:
- 首先,我在工作区目录中创建了一个包含以下内容(输入)的文本文件。
3
2
-3
2
0
5
3
8 - 然后,我将 Eclipse 配置为从控制台获取输入。
请注意,编码已设置为 MS932(默认)
现在,我构建并调试它。我进入输入步骤。然后,我等了几分钟,什么也没有。而已。该程序似乎需要很长时间,但控制不会移动到下一行。
现在,您可能认为程序本身存在一些错误,是的,它可能是,但我也在 Ideone.com 上使用自定义输入 运行 这个程序,它确实接受了输入。此外,我 运行 这个程序使用来自控制台的输入,它确实一步一步正确地接受了输入。
那么,为什么它不以这种方式直接从文件中获取输入呢?是 Notepad/Eclipse 还是其他编码问题?
感谢任何帮助。
顺便说一句,如果有人想要完整的程序,我很乐意提供。它不是一些专有内容。我写这篇文章纯粹是为了教育目的,作为一些问题的解决方案。
我还没有看到你告诉你的程序从文件中读取。所以它只是等待您的输入。
您应该在开头添加:
freopen("input.txt", "r", stdin);
它的作用:reopen
s stdin
文件描述符并将 input.txt
附加到它。它附上它供阅读 - 第二个参数是 "r"
但是如果你想能够从控制台和文件中读取,你需要再添加一个:
FILE *in;
in = fopen("input.txt", "r");
并且您应该将 scanf(...)
替换为 fscanf(in, ...)
fscanf(in, "%d", number);