file - Why is seekp not doing its job? C++/CLI -
my function "writetofile" writing piece of data file @ end of line indicated int variable zone. file set @ beginning text indicate put data, seekp() doesn't seem placing output marker need go.
can point out i'm missing please?
this file creation in main function:
string myfilename = txtboxcont; myfilename.append(".txt"); myfilename="test.txt"; //temporary name, delete before publish <<. ofstream prefile(myfilename, ios::trunc); int i; (i=1;i<=4;i++) prefile << "dc" << << "\n"; prefile << "junk:"; prefile.flush(); prefile.close(); fstream myfile(myfilename,ios::in | ios::out | ios::ate); if (!myfile.is_open()) throw;
and writetofile function: (fyi: data "0.95" or "623")
void writetofile(fstream &myfile, string& data, int zone) { data = numbercheck(data); //filters out non-numeric data, returns "null" if nothing left. if (data=="null") //if filter leaves no valid output, no need write. return; myfile.seekg(0, ios::beg); //set initial position @ start? string fileline, srch="dc"; stringstream ss; //for int string conversion ss << zone; srch.append(ss.str()); int fs=1, marker=string::npos; while(!myfile.eof()) //test each line if begins dc(zone) { getline(myfile, fileline); marker = marker + fileline.length(); if (srch==fileline.substr(0,3)) break; } marker++; //can't avoid overwriting so... gon' suck... string temp; myfile.seekg(0,ios::end); int length = myfile.tellg(), curiosity; length = length - marker; temp.resize(length); myfile.seekg(marker); curiosity = myfile.tellg(); myfile.read(&temp[0],temp.size()); //#### here's problem #### myfile.seekp(marker); curiosity = myfile.tellp(); myfile << ',' << data << temp; myfile.flush(); myfile.clear(); data.clear(); }
(the curisity variable says on tin , helping me debug) the problem is after curiosity = myfile.tellg();
, bit @ correct point in file seems. after curiosity = myfile.tellp();
, put bit @ -1 (or std::npos assume). file never writes @ myfile << ',' << data << temp;
don't why.
Comments
Post a Comment