Error in the I/O operation

已回答

Hi,

I run this code:

fprintf(stdout, "\n--- The program acquires data ---\nInsert the dimension of the vector: ");
fscanf(stdin, "%d", &i);
assert(i <= 30);
(*v).length = i;
(*v).v = (int *) malloc((*v).length * sizeof(int));
assert((*v).v != NULL);
fprintf(stdout, "Insert the element of the vector:\n");
for (i = 0; i < (*v).length; i++) {
fprintf(stdout, "\t%d element: ", i + 1);
fscanf(stdin, "%d", &(*v).v[i]);
}

and the output is:

4
9
8
7
6

--- The program acquires data ---
Insert the dimension of the vector: Insert the element of the vector:
1 element: 2 element: 3 element: 4 element:

while the correct output must be:

--- The program acquires data ---
Insert the dimension of the vector: 4
Insert the element of the vector:
1 element: 9
2 element: 8
3 element: 7
4 element: 6

Anyone could help me ?
0

Hi, Giulio!

Are you on Windows? Do you use MinGW as a toolchain in CLion?

Please try disabling PTY: go to Help | Find Action, type "Registry...", select it and in the opened list find and disable the run.processes.with.pty option. Does it help?

0
Avatar
Permanently deleted user

Hi, Anna!

I have already done those instruction. This situation is a conseguence of those instruction.

0

Please answer the following questions:

  1. Are you on Windows?
  2. Do you use MinGW as a toolchain in CLion?
  3. How does the output look like with disabled run.processes.with.pty?
  4. How does the output look like with enabled run.processes.with.pty?
0
Avatar
Permanently deleted user

1. Yes

2. Yes

3.

4
9
8
7
6

--- The program acquires data ---
Insert the dimension of the vector: Insert the element of the vector:
1 element: 2 element: 3 element: 4 element:

4.

--- The program acquires data ---
Insert the dimension of the vector: 4
4
Insert the element of the vector:
1 element: 9
9
2 element: 8
8
3 element: 7
7
4 element: 6
6
0

If it's possible, please use Cygwin instead of MinGW. In this case the output in CLion will be ok if run.processes.with.pty is enabled.

In case of MinGW you can either

1) enable run.processes.with.pty and endure the "double output" problem,

or

2) disable run.processes.with.pty and add fflush(stdout); after each fprintf line.

And please follow https://youtrack.jetbrains.com/issue/CPP-2580 in order to get updates.

0

请先登录再写评论。