Eclipse CDT Mars: Input from a file [closed]

2019-09-10 05:43发布

问题:

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;
}

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. First I created a text file with the following contents(inputs), in the workspace directory itself.
    3
    2
    -3
    2
    0
    5
    3
    8
  2. Then, I configured Eclipse to take input from the console.

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.

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.

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.

回答1:

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);

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");  

And you should replace scanf(...) with fscanf(in, ...)

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