Writing User Input to binary file.

Hello everyone, I've been trying to do this for a while now and I'm just not getting where I'm going wrong. What I'm trying to do is take user input for both the filename and then what they want to write to the file (it's a binary file). Once that happens, I take that and paste it to an offset in the file.

Code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <cstring>

int main()

{
    std::string NameInput, TextInput;

    std::cout << "Please enter the name of file (e.g Hello.bin)";
    std::cin >> NameInput;

    std::cout << "And what would you like to write to the file?";
    std::cin >> TextInput;

    std::fstream file(NameInput,std::ios::binary | std::ios::in | std::ios::out);

    std::ofstream outfile ("TEST.bin");

    file.seekg(0);

    int i = 1;

	    while (file.good() && i != 956){

	        file.get(TextInput);
	        outfile << file;

	        i++;

	    }

    outfile.close();

    file.close();

    return 0;

}


Any help is appreciated.
Last edited on
Start with the first compiler error and fix that.

Also, this: file.get(TextInput); makes no sense. What do you think it does?

outfile << file; What's this trying to do?

If you're trying to write something to a file, what's the input file for?
Last edited on
That seems to be the line that's giving me the error. Basically, that line was meant to take in user input and paste it to the position in the file.
What I'm trying to do is take user input for both the filename and then what they want to write to the file (it's a binary file). Once that happens, I take that and paste it to an offset in the file.

The given code doesn't seem to match that description.

There are two files, std::fstream file and std::ofstream outfile
It seems the program is trying to read something from one file and then output something to the other. There doesn't seem to be any code to write the user input TextInput to either file.

That seems to be the line that's giving me the error. Basically, that line was meant to take in user input and paste it to the position in the file.


http://www.cplusplus.com/reference/istream/istream/get/

This function is for reading data from the object (in this case, from a file).
Last edited on
I've now updated the code thanks to both of your feedback:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <fstream>
#include <cstring>

int main()

{
   std::string FileName;

    std::cout << "Enter the name of an existing text file: ";
    std::cin >> FileName;

    std::ifstream is(FileName);     
    is.seekg(15);

    char c;
    std::cout << "Enter what you wish to change it to: ";
    std::cin.get(c);

    if (is.get(c) && is.is_open()){         
    is >> c;
    }else{
        std::cout << "Error opening file";
    }

    is.close();

    return 0;

	}


The issue I'm having now is that it prompts for the first input and then prints the next input as well and then terminates.

So it looks like this:
1
2
Enter the name of an existing text file: test.bin
Enter what you wish to change it to:
Seems to have headed off at a tangent. The title of this thread. "Writing User Input to binary file" indicates you want a file opened in binary mode, and to be able to write something to it.

The latest code uses
std::ifstream is(FileName);
which is opened in text mode, for input only, thus missing the two original requirements. In addition, the prompt specifies that it must be a text file, "Enter the name of an existing text file: ".

What you had before was closer:
 
std::fstream file(NameInput,std::ios::binary | std::ios::in | std::ios::out);

Last edited on
Hmmm yeah definitely went off on a tangent. What would be the process of after getting user input, to open the file the user stated and also write to that file with specified string.

Not looking for code but something that could possibly lead me in the right direction.

Cause right know in pseudo-code I'm thinking:

- Get input file from user
- Call that file with fstream
- Get user input for what string they want to use for overwriting
- Seek out the offset in binary file to write to

Write the input to file and save
Last edited on
That is roughly ok. Let me just run through your pseudocode and try to make it a little more precise:

- Get name of input file from user
- Open that file with fstream (mode binary / input / output )
- Get user input for what string they want to use for overwriting
- Seek out the offset in binary file to write to
- Write the input to file

The file will be saved when it is closed - and a C++ stream is automatically closed when it goes out of scope (or the program ends).


I feel like I'm getting close now:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>
#include <cstring>

int main()

{
    std::string NameInput, TextInput;

    std::cout << "Please enter the name of file (e.g Hello.bin)";
    std::cin >> NameInput;

    std::cout << "And what would you like to write to the file?";
    std::cin >> TextInput;

    std::fstream file(NameInput,std::ios::binary | std::ios::in | std::ios::out);

    int i = 15;

    file.seekg(i);

    while (i != 18){

    file << TextInput;
    i++;

    }

    return 0;

}


Edit: Stupid mistake by me, code now works, thank you for all your help :)
Marking as solved
Last edited on

The issue now is that
it overwrites the data already inside the file

Yes, that is expected behaviour. If you want some other behaviour, perhaps you need to say specifically what you want the program to do.

and pastes the word three times.

Yes - at first I though it should be written 18 times, but since i starts from 15, your while loop repeats just three times.

Nevertheless, I think you've made progress - the program does in fact write something at a particular location, in binary mode.

One more comment. Strictly speaking, you should have used seekp() rather than seekg(). p means 'put' i.e. write output and g means 'get' i.e, read input. But for a disk file they usually give the same result. (Some other types of stream may maintain two separate pointers).

http://www.cplusplus.com/reference/ostream/ostream/seekp/
Last edited on
Topic archived. No new replies allowed.