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();
}
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.
omg, it have a lot of errors:
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 );
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());
}
Important points for doing an io operation in binary mode:
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.