String to binary file? Best way ?

I've done a lot of research and personally I can't figure out which way would be the best way to prompt the user for a name and write it to a binary file.

Any suggestions or opinions of what would be the best way?

I know some of my options are...

1. Create string then covert to char array then write to file
2. Prompt the user to enter in one char at a time then add a null char at the end of the char array
Last edited on
1
2
3
4
5
std::string MyString;
std::getline(std::cin, MyString);
std::ofstream MyFile ("My File.txt");
MyFile << MyString << std::ends;
MyFile.close();
Last edited on
The string that I need to write cannot be larger then 20 so could i just do a check like.

1
2
3
4
while(MyString.length() < 20)
{

}


and keep overwriting my string until they enter one less then 20 characters big ? or will this lead to problems ?
1
2
3
4
5
std::string MyString;
std::getline(std::cin, MyString);
std::ofstream MyFile ("My File.txt");
MyFile << MyString.substr(0, 20) << std::ends;
MyFile.close();
Last edited on
Topic archived. No new replies allowed.