How to Write Strings Into Binary File
Solution 1
There is so much wrong here, I'm just going to list everything I see:
Your for loop condition should be i < 10.
Why are you using a template but not the templatized parameter T?
You're calling the method front() on CStr, but CStr is a char, not a string, so I don't even know how that compiles.
Assuming CStr was a string, you don't want to take the address of the front() iterator using &, instead you want to say something like:
fn.write(St.c_str(), St.size());
And you don't want to loop for St.size() iterations. Just do the above.
Solution 2
omg, it have a lot of errors:
- int main - should return value;
- you don't use template< typename T > in your function;
- Mystr - not correct name in function call, names in c++ are case sencetive;
- char CStr - doesn't have method front, and std::string too;
- you couldn't get address of first element such as in case with vector;
- it will be better if you will accept std::string as const reference;
- you forgot to include string header;
- ...
fixed your example, with your code organize and your naming:
#include <iostream>
#include <fstream>
#include <string>
void WriteStr2BinFh( const std::string& St, std::ostream &out )
{
out.write( St.c_str(), St.size() );
}
int main()
{
std::string MyStr = "Foo";
std::ofstream myfile( "OuputFile.txt", std::ios::binary | std::ios::out );
for (size_t i = 0; i < 9; ++i)
{
WriteStr2BinFh( MyStr+"\t", myfile );
}
myfile.close();
return 0;
}
but I've reccomended to use std::fill_n
algorithm
std::fill_n( std::ostream_iterator< std::string >( myfile, "\t" ), 10, MyStr );
Solution 3
First, char CStr
says that CStr
is a single character. Second, fn.write(&CStr.front(), CStr.size());
treats that character as a container, like std::vector<>
, which of course cannot compile.
Assuming that everything up to WriteStr2BinFh
is ok, which I haven't checked, this is how WriteStr2BinFh
should (could) look:
void WriteStr2BinFh(const string& St, ostream &fn)
{
for(string::iterator it = St.begin(); it != St.end(); ++it)
{
fn.put(*it);
}
}
or, preferably
void WriteStr2BinFh(const string& St, ostream &fn)
{
fn.write(St.c_str(), St.length());
}
Solution 4
Important points for doing an io operation in binary mode:
- The file has to be opened in output and binary mode using the flags ios::out (output mode) and ios::binary( binary mode)
- The function write takes two parameters. The first parameter is of type char* for the data to be written and the second is of type int asking for the size of data to be written to the binary file.
File has to be closed at the end.
void write_to_binary_file(WebSites p_Data) { fstream binary_file("c:\\test.dat",ios::out|ios::binary|ios::app); binary_file.write(reinterpret_cast<char *>(&p_Data),sizeof(WebSites)); binary_file.close(); }
This I/O binary function writes some data to the function.
The file is opened in output and binary mode with ios::out and ios::binary. There's one more specifier ios::app, which tells the Operating system that the file is also opened in append mode. This means any new set of data will be appended to the end of file.
The write function used above, needs the parameter as a character pointer type. So we use a type converter reinterpret_cast to typecast the structure into char* type.
Danf
Updated on June 22, 2022Comments
-
Danf almost 2 years
With this code I tried to print the string "foo" 10 times in binary format. But why doesn't the function to do it work?
#include <iostream> #include <fstream> using namespace std; template <typename T> void WriteStr2BinFh (string St, ostream &fn) { for (unsigned i = 0; i < St.size(); i++) { char CStr = St[i]; fn.write(&CStr.front(), CStr.size()); } return; } int main() { string MyStr = "Foo"; ofstream myfile; myfile.open("OuputFile.txt", ios::binary|ios::out); // We want to print it 10 times horizontally // separated with tab for (int i = 0; i < 9; i++) { WriteStr2BinFh(Mystr+"\t", myfile); } myfile.close(); }