I am trying to write a string to a binary file, to that effect I made this snippet:
std::string outname = filename.substr(0, filename.size() - 4) + std::string("_bin") + std::string(".sdf");
std::cout << "Writing results to: " << outname << "\n";
std::ofstream outfile(outname.c_str(), std::ofstream::binary);
std::stringstream ss;
ss << phi_grid.ni << " " << phi_grid.nj << " " << phi_grid.nk << std::endl;
ss << min_box[0] << " " << min_box[1] << " " << min_box[2] << std::endl;
ss << dx << std::endl;
for (unsigned int i = 0; i < phi_grid.a.size(); ++i)
{
ss << phi_grid.a[i] << std::endl;
}
outfile.write(ss.str().c_str(), ss.str().size());
outfile.close();
This however always writes to a text file, not a binary file. I don't udnerstand why, the flag is explicitely passed to the constructor.
Related
I need to convert string to hex format and append "0x" prefix to hex value.
For Example:
Input: std::string s = "0x06A4";
Output: int num = 0x06A4
I have tried this code:
{
std::stringstream ss;
std::string s = "0x06A4";
int num = std::stoi(s, 0, 16);
std::cout << "value in decimal = " << num << '\n';
std::cout << "value in hexadecimal = " << std::hex << num << '\n';
ss << "0x" << std::hex << num << '\n'; //
std::string res = ss.str();
std::cout << "result " << res << '\n';
}
#yogita, std::hex is just one of the configuration you need. You are probably missing the setfill and the setw configuration, as following:
#include <iostream>
#include <sstream>
#include <iomanip>
int main()
{
std::stringstream ss;
std::string s = "0x06A4";
int num = std::stoi(s, nullptr, 16);
std::cout << "value in decimal = " << num << '\n';
std::cout << "value in hexadecimal = " << std::hex << num << '\n';
ss << "0x" << std::hex << std::setfill('0') << std::setw(4) <<num << '\n';
std::string res = ss.str();
std::cout << "result " << res << '\n';
return 0;
}
I have a c++ program that wries a simple json file using ofstream.
if( GDALGetGeoTransform( m_dataset, adfGeoTransform ) == CE_None )
{
cout <<std::fixed << std::setprecision(3) << adfGeoTransform[0] << ", "<< adfGeoTransform[1] << ", "<< adfGeoTransform[2] << ", "<< adfGeoTransform[3] << ", "<< adfGeoTransform[4] << ", "<< adfGeoTransform[5] << ", "<<endl;
ofstream myfile;
myfile.open ( outPath + "/tiles.json");
myfile << std::fixed << std::setprecision(9) ;
myfile << "{" <<endl;
myfile << "\"title\": \"" << filename << "\"," << endl;
myfile << "\"ul\" : [" << dfGeoX<<", "<< dfGeoY<<"]," << endl;
myfile << "\"lr\" : [" << dfGeoX1<<", "<< dfGeoY1<<"]," << endl;
myfile << "\"res\" : [" << adfGeoTransform[1]<<", "<< adfGeoTransform[5] <<"]," << endl;
if( m_dataset->GetProjectionRef() != NULL )
{
//printf( "Projection is `%s'\n", poDataset->GetProjectionRef() );
myfile << "\"projection\" : \"";
auto proj = m_dataset->GetProjectionRef();
for(int i =0 ; proj[i] != '\0';i++){
if(proj[i] == '"')
myfile << '\\';
myfile << proj[i];
}
myfile << "\"," << endl;
}
myfile << "\"maxZoom\" : "<< max(ceil(log2(nRows / tileSize)), ceil(log2(nCols / tileSize))) << endl;
myfile << "}" <<endl;
myfile.flush();
myfile.close();
}
I do see the console output from cout, so it is entering the if branch.
But funny thing is that when i run it the first time it do not generate tiles.json file. But running it again it creates the file.
The problem was that the folder that it was trying to create the file in did not exist.
The application did not crash due to this, just continued and then later in the application the folder was created by 3th party lib as a output of other files generated.
Then running the second time, the folder exists and the file is written.
is there any library or example for reading a csv file in C++ like the csv module in Python?
What I need is a function to read a csv file and put each column element of a row in a map with the header name as the key value.
I can answer myself. I wrote a CSVDict class.
#include <iterator>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>
#include <map>
class CSVDict {
public:
CSVDict(std::string fileName, int headerRow) {
file = std::ifstream(fileName);
for (int i = 0; i < headerRow; i++){ readNextRow(file); }
m_header = m_data;
}
std::string const& operator[](std::size_t index) const {
return m_data[index];
}
std::string const& operator[](std::string index) const {
return m_dataMap.find(index)->second;
}
bool readNextRowMap() {
readNextRow(file);
if (!file) return false;
m_dataMap.clear();
auto it_data = m_data.begin();
for (auto it = m_header.begin(); it != m_header.end(); ++it) {
m_dataMap[*it] = *it_data;
++it_data;
}
return true;
}
private:
void readNextRow(std::istream& str) {
std::string line;
std::getline(str, line);
if (!str) return;
std::stringstream lineStream(line);
std::string cell;
m_data.clear();
while (std::getline(lineStream, cell, ';')) {
m_data.push_back(cell);
}
}
std::vector<std::string> m_data;
std::vector<std::string> m_header;
std::map<std::string, std::string> m_dataMap;
std::ifstream file;
};
int main()
{
CSVDict dict("1.csv", 2);
while (dict.readNextRowMap()) {
std::cout << dict[0] << " " << dict[1] << " " << dict[2] << " " << dict[3] << " " << dict[4] << " " << dict[5] << " " << dict[6] << "\n";
}
CSVDict dict1("1.csv", 2);
dict1.readNextRowMap();
std::cout << dict1["ipField"] << " " << dict1["mdBeamEnergy"] << " " << dict1["mdBeamCurrent"] << " " << dict1["mcoBeamSizeId"] << " " << dict1["mdGantryAngle"] << " " << dict1["miLayerNumber"] << "\n";
dict1.readNextRowMap();
std::cout << dict1["ipField"] << " " << dict1["mdBeamEnergy"] << " " << dict1["mdBeamCurrent"] << " " << dict1["mcoBeamSizeId"] << " " << dict1["mdGantryAngle"] << " " << dict1["miLayerNumber"] << "\n";
dict1.readNextRowMap();
std::cout << dict1["ipField"] << " " << dict1["mdBeamEnergy"] << " " << dict1["mdBeamCurrent"] << " " << dict1["mcoBeamSizeId"] << " " << dict1["mdGantryAngle"] << " " << dict1["miLayerNumber"] << "\n";
dict.readNextRowMap();
std::cout << dict[0] << " " << dict[1] << " " << dict[2] << " " << dict[3] << " " << dict[4] << " " << dict[5] << " " << dict[6] << "\n";
return 0;
}
Example csv file:
#VALUES;;;;;;
ipField;mdBeamEnergy;mdBeamCurrent;mcoBeamSizeId;mdGantryAngle;miLayerNumber;mbRoomSwitchingLayer
24.30815;172.152971;24.30815;4;65;1;1
24.30815;172.152971;24.30815;4;65;2;0
24.30815;172.152971;24.30815;4;65;3;0
24.30815;172.152971;24.30815;4;65;4;0
24.30815;172.152971;24.30815;4;65;5;0
24.30815;172.152971;24.30815;4;65;6;0
24.30815;172.152971;24.30815;4;65;7;0
24.30815;172.152971;24.30815;4;65;8;0
usage (see main function in example):
class constructor needs to have the csv filename and the csv header line number
every readNextRowMap gets the values of the next line in the csv file
you can address the values either by number index or by header name
downside:
csv file has to have a header line
I'm trying to initialize a file with 100 empty records with the code below:
void initializeInventory() {
std::ofstream out("hardware.dat", std::ios::binary);
Hardware h;
for (int i = 0; i < 100; ++i) {
h.ID = i;
h.name = "try"; // std::string();
h.quantity = 0;
h.price = 0;
h.notes = "try2"; //std::string();
out.write(reinterpret_cast<const char*>(&h), sizeof(Hardware));
}
out.close();
}
But when I try to print them out, it always print only 25 elements or crashes.
This is the function to print out elements:
void readInventory() {
std::ifstream in("hardware.dat", std::ios::in);
std::cout << std::setiosflags(std::ios::left) << std::setw(4) << "ID"
<< std::setw(16) << "Name"
<< std::setw(11) << "Quantity"
<< std::setw(10) << std::resetiosflags(std::ios::left)
<< "Price"
<< std::setw(50) << "Notes" << '\n';
Hardware h;
while (!in.eof()) {
in.read(reinterpret_cast<char*>(&h), sizeof(Hardware));
//if (!in.eof())
printOut(std::cout, h);
}
in.close();
}
void printOut(std::ostream &output, const Hardware& h) {
output << std::setiosflags(std::ios::left) << std::setw(4) << h.ID
<< std::setw(16) << h.name
<< std::setw(11) << h.quantity
<< std::setw(10) << std::setprecision(2)
<< std::resetiosflags(std::ios::left)
<< std::setiosflags(std::ios::fixed | std::ios::showpoint )
<< h.price
<< std::setw(50) << h.notes << '\n';
}
I also noted that if I increase the number of cycles in the for (I tried putting 400 instead of 100) the file hardware.dat seems to grow, so I thought that should be a problem in the print function. Any idea? Is there something I'm missing?
Thanks in advance.
It would be the best to overload the operator <<
friend ostream& operator<<(ostream& out, const Hardware& h) // output
{
out << "(" << h.id() << ", " << h.whatever() << ")";
return out;
}
and read the file line by line to the Hardware object.
By the way, overloading the input operator >> is also possible.
I have a class that's supposed to write to a gml file defined below. The class has one method that does the writing. If I call the function, I get a core dump when the main function returns. I can create objects of the class with no problem, it only happens when the write function is called. The function also returns with no error and the rest of the program runs.
GML Writer:
class GMLWriter {
public:
void write(List<User*> usr, const char* filename);
};
void GMLWriter::write(List<User*> usr, const char* filename)
{
cout << "Filename: " << filename << endl;
ofstream outfile;
outfile.open(filename);
if (!outfile.is_open())
cout << "Couldn't open the file..." << endl;
outfile << "graph [\n";
// Write user data
for (int n = 0; n < usr.size(); n++) {
cout << "Writing node..." << endl;
outfile << "node [\n";
outfile << "id " << usr[n]->getID() << "\n";
outfile << "name \"" << usr[n]->getName() << "\"\n";
outfile << "age " << usr[n]->getAge() << "\n";
outfile << "zip " << usr[n]->getZip() << "\n";
outfile << "]\n";
}
// Write associations
for (int n = 0; n < usr.size(); n++) {
List<int> tList = usr[n]->getFriends();
cout << "Writing edge..." << endl;
//List<int> tempL = usr[n]->getFriends();
for (int i = 0; i < tList.size(); i++) {
outfile << "edge [\n";
outfile << "source " << usr[n]->getID() << "\n";
outfile << "target " << tList[i] << "\n";
outfile << "]\n";
}
}
outfile << "]"; // end graph
cout << "End function" << endl;
outfile.close();
}
User simply contains the variables to write to the file, and those methods work fine.
I've spent hours with this in a debugger and haven't been able to find the problem. Any help would be greatly appreciated.
Thanks!
Try looking at the core dump: http://www.network-theory.co.uk/docs/gccintro/gccintro_38.html