>I just want to know if theres any way i can read an input twice from the console.
The only portable way is to ask the user for the same input again. Once you read data from stdin, it's gone unless you saved it. Another option (you can decide if it's good or not) is to build a linked list of strings: Create a node and fill the string. If it's filled all of the way, create another node, attach it to the end of the list and fill that string. Repeat until the string is not completely filled. At that point you've read all of the data and you can allocate memory that way like so:
char *str = malloc ( ( list_size - 1 ) * MAX_STR + strlen ( last_node->str ) + 1 );
Then you can walk the list and append each string to the allocated memory. In general, this is less error prone than resizing the block of memory at regular intervals, but it does have it's drawbacks.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>but i think saving it into a file is much a easier process
Far less efficient though. Device I/O is about as slow as it gets aside from interactive input.
>So theres actually no way to read things back from console, i reckon.
Not unless the terminal stream supports seeking, doesn't discard previously read input, and you know exactly how many characters were read on the first pass.
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
>could u plz provide me with an example.
No, because I'm not aware of a system that meets all of the requirements. But theoretically you could do something like this:
#include <stdio.h>
int main ( void )
{
int data1, data2;
int n;
printf ( "Enter an integer: " );
if ( scanf ( "%d%n", &data1, &n ) == 1 ) {
printf ( "%d\n", data1 );
fseek ( stdin, -n, SEEK_CUR );
scanf ( "%d", &data2 );
printf ( "%d\n", data2 );
}
return 0;
}
Narue
Bad Cop
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401