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.
#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;
}
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.
#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: ".
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
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).
#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
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).