how to correctly write vector to binary file in c++?

You are writing to file the vector structure, not its data buffer. Try change writing procedure to: ofstream fout(“data.dat”, ios::out | ios::binary); fout.write((char*)&student[0], student.size() * sizeof(Student)); fout.close(); And instead of calculation size of vector from file size, it’s better write vector size (number of objects) before. In the case you can write to the same … Read more

Reading line from text file and putting the strings into a vector?

Simplest form: std::string line; std::vector<std::string> myLines; while (std::getline(myfile, line)) { myLines.push_back(line); } No need for crazy c thingies 🙂 Edit: #include <iostream> #include <fstream> #include <string> #include <vector> int main() { std::string line; std::vector<std::string> DataArray; std::vector<std::string> QueryArray; std::ifstream myfile(“OHenry.txt”); std::ifstream qfile(“queries.txt”); if(!myfile) //Always test the file open. { std::cout<<“Error opening output file”<< std::endl; system(“pause”); return … Read more

using fstream to read every character including spaces and newline

Probably the best way is to read the entire file’s contents into a string, which can be done very easily using ifstream’s rdbuf() method: std::ifstream in(“myfile”); std::stringstream buffer; buffer << in.rdbuf(); std::string contents(buffer.str()); You can then use regular string manipulation now that you’ve got everything from the file. While Tomek was asking about reading a … Read more

Best way to split a vector into two smaller arrays?

Use iterators. std::vector<int> lines; // fill std::size_t const half_size = lines.size() / 2; std::vector<int> split_lo(lines.begin(), lines.begin() + half_size); std::vector<int> split_hi(lines.begin() + half_size, lines.end()); Since iterator ranges represent half open ranges [begin, end), you don’t need to add 1 to the second begin iterator: lines.begin() + half_size isn’t copied to the first vector. Note that things … Read more

std::fstream doesn’t create file

You’re specifying std::fstream::in in your call to fstream::open(). This is known to force it to require an existing file. Either remove std::fstream::in from your mode argument, or specify std::fstream::trunc in addition to the other flags.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)