944,101 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5788
  • C RSS
Nov 8th, 2004
0

reading a same input twice from the console

Expand Post »
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.
Similar Threads
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Nov 9th, 2004
0

Re: reading a same input twice from the console

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....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
kunal_ktr is offline Offline
10 posts
since Oct 2004
Nov 9th, 2004
0

Re: reading a same input twice from the console

>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:
  1. 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 12th, 2004
0

Re: reading a same input twice from the console

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.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Nov 12th, 2004
0

Re: reading a same input twice from the console

>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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Nov 13th, 2004
0

Re: reading a same input twice from the console

"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.
Reputation Points: 113
Solved Threads: 3
Posting Whiz
Asif_NSU is offline Offline
353 posts
since Apr 2004
Nov 13th, 2004
0

Re: reading a same input twice from the console

>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:
  1. #include <stdio.h>
  2.  
  3. int main ( void )
  4. {
  5. int data1, data2;
  6. int n;
  7.  
  8. printf ( "Enter an integer: " );
  9. if ( scanf ( "%d%n", &data1, &n ) == 1 ) {
  10. printf ( "%d\n", data1 );
  11.  
  12. fseek ( stdin, -n, SEEK_CUR );
  13. scanf ( "%d", &data2 );
  14.  
  15. printf ( "%d\n", data2 );
  16. }
  17.  
  18. return 0;
  19. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: problem solving
Next Thread in C Forum Timeline: Displaying Square root symbol in output





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC