strange issue with reading files...

rory rorywalsh at gmail.com
Sat Jan 5 05:45:15 PST 2008


On Jan 4, 9:59 pm, rwa... at gmail.com ("Robert Anderson") wrote:
> On Jan 4, 2008 11:15 AM, rory <rorywa... at gmail.com> wrote:
>
>
>
> > > Did you compare the contents of buf and the value of pos in your
> > reference
> > > (command line) implementation with your wx implementation?  Are they
> > > identical?
>
> > > Bob
>
> > pos seems to be the same but the contents of buf are not
>
> That doesn't make much sense, since pos is derived from buf.  Did atoi(buf)
> return something non-zero in the reference implementation?
>
> I would suggest cout'ing all of your intermediate results in the reference
> implementation and comparing the results one step at a time until you find
> the first point of discrepancy.  This will also tell you if your reference
> implementation is really doing what you thought it was.
>
> Bob

I've gone through both implementations cout'ing and message boxing
everything and have found when if I open the binary file with the
ios::ate mode and call tellg the get pointer positions are different
in the two implementations. In my wx App it reports a larger position
than in my standard c++ app. I also tried leaving out that mode and
doing a seekg(0, ios::end), again both get pointer positions differ by
the same amount, i.e., 1673. I tried to offset my seekg in the wx ap
by 1673 to see if that would work but that didn't work either.
Something else I checked was buf, it's is not returning null but it is
not picking up the 10 digits at the end of the file. It seems to me
that there is a problem with ifstream in my wx app. It is opening the
file Ok but it is not reporting the correct file size. Here is my
code, both for my c++ app and mw wx app. Sorry for the long post. I
hope someone can spot what is going on, I'm stumped.
Rory.


standard c++ implementation:

 csdText = "";
  char buf[10];
  ifstream inFile(argv[2], ios::binary);

  if(!inFile)  cerr << "Error: Could not open data";
  inFile.seekg(0, ios::end);
  cout << "File size:" << int(inFile.tellg()) << "\n";
  inFile.seekg(-10, ios::end);
  cout << "getP=" << int(inFile.tellg()) << "\n";
  inFile.get(buf, sizeof(char)*10);

  cout << buf << "\n";
  int pos = atoi(buf)+10;
  cout << pos << "\n";
  inFile.seekg(-pos, ios::end);
  cout << "getP=" << int(inFile.tellg()) << "\n";
  csdText = "";
  while(!inFile.eof()){
        getline(inFile, str);
        csdText = csdText+str;
    }
cout << csdText;


wx implementation:

  std::string str = "";
  std::string csdText = "";
  wxString test;

  char* buf = new char[10];			//appName.c_str()
  ifstream inFile(appName.c_str(), ios::binary);
  if((inFile.rdstate() & ifstream::failbit)!=0)
  wxMessageBox("Error seeking");
  inFile.seekg(0, ios::end);
  test.Printf("Position at end:%d", int(inFile.tellg()));
  wxMessageBox(test);

  inFile.seekg(-(10+1673), ios::end);
  if((inFile.rdstate() & ifstream::badbit)!=0)
  wxMessageBox("Error seeking");

  test.Printf("Position after 1st seek:%d", int(inFile.tellg()));
  wxMessageBox(test);

  inFile.get(buf, sizeof(char)*10);
  int pos = atoi(buf)+10+1673;
  if(!buf)wxMessageBox("nothing");
  else{
	  test.Printf("buf:%d", pos);
  wxMessageBox(test);
  }
  inFile.seekg(-pos, ios::end);

  test.Printf("Position after 2nd seek:%d", int(inFile.tellg()));
  wxMessageBox(test);

  csdText = "";                      //3914575
  while(!inFile.eof()){
        getline(inFile, str);
        csdText = csdText+str;
    }
  wxMessageBox(csdText.c_str());






More information about the wx-users mailing list