My c++ program first write file content the second time its run - c++

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.

Related

iostream not writing to binary file despite correct flag

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.

Changing the first line of file and going to the last without losing data c++

I'm working on a school project for C++ with involves on working with polimorfism, dynamic objects, heritage. My project consists on an main.cpp with will run a loop with options for the user to select. I will be storing some data in a file, the data will be appended so I don't loose the data that was already in the file, but my file works like the following example:
2
Data;12;21;0
Data2s;22;61;7
This is what I want to happen:
3
Data;12;21;0
Data2s;22;61;7
Data3s;8;2;9
What I'm attempting to do is, I have to change the first line to the next variable number, and I can't loose the data under it.
After I change the line, I will go to the end of the file (after numb 7) so I can add another line of data.
attempt 1:
void Empresa::save_data(){
ofstream arquivoE;
arquivoE.open("data.txt", ios::app);
if (temp > 0){
arquivoE.seekp(0) << num_func;
arquivoE.seekp(0, ios_base::end);
arquivoE << endl;
}else{
arquivoE << num_func << endl;
}
for (int i = temp; i < num_func; i++)
{
arquivoE << func[i]->getNome() << ";" << func[i]->getEmissao() << ";" << func[i]->getSalario() << ";" << func[i]->last_field() << endl;
}
cout << " Sucesso ao gravar arquivo!" << endl;
arquivoE.close();
}
attempt 2:
void Empresa::save_data(){
ofstream arquivoE;
arquivoE.open("data.txt", ios::out);
arquivoE.seekp(0, ios::beg) << num_func;
arquivoE.seekp(0, ios::end);
arquivoE << endl;
for (int i = temp; i < num_func; i++)
{
arquivoE << func[i]->getNome() << ";" << func[i]->getEmissao() << ";" << func[i]->getSalario() << ";" << func[i]->last_field() << endl;
}
cout << " Sucesso ao gravar arquivo!" << endl;
arquivoE.close();
}
num_func is the the variable from the first line, which I need to change
Temp variable is just to keep track of where to add the line
This is the data line:
arquivoE << func[i]->getNome() << ";" << func[i]->getEmissao() << ";" << func[i]->getSalario() << ";" << func[i]->last_field() << endl;

Not able to receive the OnBarCodeEvent for zebra scanner in linux c++

I am using the zebra linux sdk for scanner to capture the barcode that is scanned.
I want to listen to the OnBarcodeEvent and capture the code.
I have tried the sample console-app code and gui app code
I have called the
::ExecCommand( CMD_REGISTER_FOR_EVENTS , inXml, outXml, &eStatus);
The OnBarcodeEvent doesn't get invoked when I scan a bar code.
Code provided below.
void SampleEventListener::connect_corescanner()
{
cout <<"connect_corescanner startp"<< endl;
int iScannerType = SCANNER_TYPE_ALL;
StatusID eStatus ;
::Open(this, SCANNER_TYPE_ALL , &eStatus );
std::cout << "Open eStatus line 634 ConsoleSampleEventListener.cpp" << ": " << eStatus << std::endl;
if ( eStatus != STATUS_OK )
{
cout <<eStatus<< endl;
cout <<"can't connect to the corescanner. "<< endl;
exit(0);
}
// register for all events //
std::string inXml;
std::ostringstream oss;
oss << "<inArgs>" << std::endl;
oss << " <cmdArgs>" << std::endl;
oss << " <arg-int>6</arg-int>" << std::endl;
oss << " <arg-int>1,2,4,8,16,32</arg-int>" << std::endl;
oss << " </cmdArgs>" << std::endl;
oss << "</inArgs>" ;
inXml = oss.str();
std::string outXml;
::ExecCommand( CMD_REGISTER_FOR_EVENTS , inXml, outXml, &eStatus);
std::cout << "ExecCommand eStatus line 658 ConsoleSampleEventListener.cpp" << ": " << eStatus << std::endl;
if( eStatus != STATUS_OK ){
cout <<"unable to register for events"<< endl;
exit(-1);
}
cout <<"subscribe to events successful."<< outXml;
cout <<"\nsubscribe to events successful."<< endl;
cout <<"connect_corescanner endp\n"<< endl;
}
void SampleEventListener::OnBarcodeEvent(short int eventType, std::string & pscanData)
{
cout << "Barcode Detected" << endl;
cout << "Out XML" << endl;
cout << pscanData << endl;
}

fstream not writing to file

I'm having trouble with fstream not writing to my file when it is located in a specific place. In the following code when the commented line is uncommented the file is written to, however when it is commented the file is not written to at all. I've added a bunch of console output and it says both of the outputs around the "outputFile << newWord << endl;" are completed but the file is never actually written to.
void write_index( string newWord )
{
fstream outputFile( "H:\\newword.txt", ios::app );
int same = 0;
string currWord;
currWord.resize(5);
//outputFile << newWord << endl;
while( !outputFile.eof() )
{
getline( outputFile, currWord );
cout << "Checking if " << newWord << "is the same as " << currWord << endl;
if( newWord == currWord )
{
cout << "It is the same" << endl;
same = 1;
break;
}
}
if( same != 1 )
{
cout << "Writing " << newWord << "to file" << endl;
outputFile << newWord << endl;
cout << "Done writing" << endl;
}
outputFile.close();
}
I'm somewhat confident you're looking for something along these lines:
void write_index( const string& newWord )
{
fstream outputFile( "H:\\newword.txt", ios::in);
bool same = false;
if (outputFile)
{
string currWord;
while (getline(outputFile, currWord))
{
cout << "Checking if " << newWord << " is the same as " << currWord << endl;
same = (newWord == currWord);
if (same)
{
cout << "It is the same" << endl;
break;
}
}
}
if (!same)
{
outputFile.close();
outputFile.open("H:\\newword.txt", ios::app);
cout << "Writing " << newWord << "to file" << endl;
outputFile << newWord << endl;
cout << "Done writing" << endl;
}
outputFile.close();
}
There are better ways to do this, but that will likely be a decent place to start.

Core dump when main function returns

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