I have a weird situation. Suppose a string will be entered as an input. using the getchar() function i count the number of characters the string contains until a new line character is encountered. So i know the size of the string. And then i try to allocate enough memory dynamically to hold that string(itz possible since i know the size). After that i need to read that string again from the console(but the user should enter it only once) and put it in the allocated memory. I just want to know if theres any way i can read an input twice from the console. I know there are other ways i can solve my problem for e.g while i count each character i can write that in a file and later i can read the string from the file anytime i want. But i would like to know if i can read same input twice from the console, and if so how.

there is another way of doing this..
using realloc() function u can dynamically allocate the memory u need for ur string. first allocate memory just for a character. now start scanning characters from console. until the new line is entered, reallocate the extra memory u need. hope this will solve ur problem....

>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.

hmmm, i did thought of these possibilities , but i think saving it into a file is much a easier process. So theres actually no way to read things back from console, i reckon.

>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.

"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."
>>could u plz provide me with an example.

>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;
}
This article has been dead for over six months. Start a new discussion instead.