| | |
reading a same input twice from the console
![]() |
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.
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
•
•
Join Date: Oct 2004
Posts: 10
Reputation:
Solved Threads: 0
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....
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:
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.
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:
C Syntax (Toggle Plain Text)
char *str = malloc ( ( list_size - 1 ) * MAX_STR + strlen ( last_node->str ) + 1 );
I'm here to prove you wrong.
>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.
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.
I'm here to prove you wrong.
"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.
"He who mixes with people and endures the harm they do is better than he who does not mix and endures." (Tirmidhi)
>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:
No, because I'm not aware of a system that meets all of the requirements. But theoretically you could do something like this:
C Syntax (Toggle Plain Text)
#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; }
I'm here to prove you wrong.
![]() |
Similar Threads
- Reading Input (Java)
- Input from console/batch (Java)
- TMT Pascal Input Console problem (Pascal and Delphi)
- reading input ... quick C++ question ... (C++)
Other Threads in the C Forum
- Previous Thread: problem solving
- Next Thread: Displaying Square root symbol in output
| Thread Tools | Search this Thread |
#include * adobe ansi api array asterisks binarysearch centimeter changingto char character cm copyimagefile cprogramme creafecopyofanytypeoffileinc csyntax database directory dynamic execv feet fgets file fork function getlasterror getlogicaldrivestrin givemetehcodez global grade gtkgcurlcompiling gtkwinlinux hacking hardware highest histogram ide include incrementoperators infiniteloop input interest kernel keyboard kilometer license linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match matrix meter microsoft motherboard mqqueue number odf opendocumentformat opensource owf pattern pdf performance pointer posix probleminc process program programming radix recursion recv repetition research reversing segmentationfault sequential single socket socketprograming standard string systemcall threads turboc unix user voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi






