I have a simple C console application and I’m trying to test it with an input of 100,000. But the program only receives about 4,096 characters each time. This is a simple example of input:
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 //Up to 100,000
It works if I manually write a number each time, but I want to just paste the input(100,000 space-separated numbers) into the console.
PS: Sorry for my English, it’s not very good
EDIT: Yes, the platform I’m using is Windows cmd. And I have already tried “Properties>Options>Buffer Size”.
Example of the code:
int a[100000],i;
for(i=0;i<100000;i++)
scanf("%i",&a[i]);
Print screen of the test:
The standard IO routines (such as
printf()andscanf()) use buffering to improve performance and provide some nice standardized routines (such asfgets()to read in a line at a time). Making function calls is usually significantly faster than making system calls; if you were to write an application using system calls to read one byte of data at a time it would run significantly slower than an application that reads 1024 bytes at a time or larger.This is almost always a wonderful thing.
But it does mean that you need to write your applications carefully. You cannot simply issue a single
read()-style function that will read all the data at once. (The amount of available data may be significantly greater than the amount of memory and swap space available on the machine, too, so even if you could, it would not be very reliable in all use cases.)Applications that handle a stream of input tend to have a loop something like these:
or
The first case is convenient because you never need to worry about input that’s been broken across two or more “buffers” — but the first case does mean
sscanf()and similar tools can’t help you parse numbers. The second case does allow you to usesscanf()when parsing, but you have to be prepared for an input to span across two buffers. This is easier to spot if the buffer is only eight bytes:buffer[8]and you wish to read in a 32-byte MD5sum or adoublethat requires more than eight characters to express:1234567890. The first read will get the first eight bytes, the second read will get the next two bytes, and you’ll have to construct your number.