954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

getting user's input directly into file

Hi there.

I'm trying to write a program that will establish a DB of names and Ids.
The thing is i don't want to scan the entered data to a temporary variable and then put it into the file, but to scan the user's input directly to the file itself.

Is there any way to do so?

If not, what is the most efficient (memory-wise) way to get user's input into the file? char by char?

Thanks!

shilosimi
Newbie Poster
4 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 

The best way so far I thought about is to get char by char to the file.
is there a better way?

1. ofstream inputFile ("input.txt",fstream::ate);
2.
3. while (cin.peek()!='\n')
4. inputFile << (char)cin.get();

shilosimi
Newbie Poster
4 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 
The thing is i don't want to scan the entered data to a temporary variable and then put it into the file


Why not?Is there any way to do so?
Nope. At some point during the process you'll be using memory.If not, what is the most efficient (memory-wise) way to get user's input into the file? char by char?
As far as memory footprint goes, character-by-character would be your best bet, but that implies a time trade-off. I honestly think you're going to an unnecessary extreme with this question. Maybe if you explained what you're trying to do and why, someone can offer a reasonable solution.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Thanks Narue.

I'm trying to solve a problem given in homework.
I know thats a bad tradeoff, but these are my orders. No dinamic memory allocation at all.
I mean, I can use know-size variables for flags, chars etc, but not for anything got to do with the data or any record from the DB.

So, I CAN use memory but not for storing name, Id etc.

Thanks again!

shilosimi
Newbie Poster
4 posts since May 2011
Reputation Points: 10
Solved Threads: 0
 
No dinamic memory allocation at all.


There's nothing stopping you from using, for example, an array of char to read blocks from input with unknown length. This is what we did in C (usually for purposes of building a dynamic string, but you can write directly to file as well):

char buf[BLOCK_SIZE];

while (fgets(buf, sizeof buf, in) != 0) {
    fputs(buf, out);

    if (strchr(buf, '\n') != 0)
        break;
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: