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!

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();

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.

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!

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.