Eclipse CDT火星:来自文件的输入 [英] Eclipse CDT Mars: Input from a file

查看:84
本文介绍了Eclipse CDT火星:来自文件的输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近安装了用于运行C程序的Eclipse CDT Mars版本。我从文件中获取输入时遇到问题。所以,我有一个看起来像这样的程序:

I have recently installed Eclipse CDT Mars release for running C Programs. I am facing problems with taking input from a file. So, I have a program that looks like this:

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语句应该接受输入,而我希望输入来自文件。因此,我执行了以下操作:

Well, of course its not the whole program. This is the glimpse of it. The problem is the scanf statements are supposed to take input and I want the input to be taken from a file. So, I did the following:


  1. 首先,我在工作区目录中创建了一个包含以下内容(输入)的文本文件

    3

    2

    -3

    2

    0

    5

    3

    8

  2. 然后,我将Eclipse配置为从控制台获取输入。

请注意,编码已设置为MS932(默认)

Notice that the Encoding has been set to MS932(default)

现在,我构建它并对其进行调试。我进入输入步骤。然后,我等了几分钟,什么也没有。而已。该程序似乎需要很长时间,但控制权不会移至下一行。

Now, I build it and debug it. I step up to the Input step. Then, I wait for like minutes and nothing. That's it. The program seems to be taking long time but control doesn't move to the next line.

现在,您可能会认为程序中存在一些错误本身,是的,但是我也使用自定义输入在Ideone.com上运行了该程序,它确实接受了输入。另外,我用Console的输入运行了该程序,它确实一步一步正确地接受了输入。

Now, you may think that there is some fault in the program itself and yeah it may be but I also ran this program on Ideone.com with custom inputs and it does take the inputs. Also, I ran this program with input from Console and it did take the inputs properly step by step.

所以,为什么它不接收来自控制台的输入呢?这样直接归档吗?是Notepad / Eclipse或其他问题的编码问题吗?

So, why it wouldn't take inputs from a file directly this way? Is it an encoding issue with Notepad/Eclipse or something else?

我们将提供任何帮助。

顺便说一句,如果有人想要完整的程序,我很乐意提供。它不是一些专有内容。我纯粹是出于教育目的而写的,目的是解决某些问题。

Any help is appreciated.
Btw, if anyone wants the complete program, I am happy to provide it. Its not some proprietary content. I wrote purely for educational purpose as solution to some problem.

推荐答案

我还没有看到您告诉过您程序从文件中读取。因此,它只是等待您的输入。

您应该在开头添加它:

I haven't seen that you tell your program to read from file. So it just waits your input.
You should add this at the beginning:

freopen("input.txt", "r", stdin);

功能:它重新打开 s stdin 文件描述符,并将 input.txt 附加到文件描述符。它附有用于读取的内容-第二个参数是 r

What it does: it reopens stdin file descriptor and attaches input.txt to it. It attaches it for reading - second argument is "r"

但是如果您希望能够读取控制台和文件,您需要再添加一个:

But if you want to be able to read from console AND files, you need to add one more:

FILE *in;
in = fopen("input.txt", "r");  

您应该替换 scanf(...) fscanf(in,...)

fscanf(in, "%d", number);

这篇关于Eclipse CDT火星:来自文件的输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆