1 Replies - 4484 Views - Last Post: 09 February 2011 - 09:23 PM Rate Topic: -----

#1 Guest_DarkMage530*


Reputation:

ifstream vs fstream, does filetype matter?

Posted 09 February 2011 - 09:14 PM

I understand that ifstream is for reading and fstream can read or write. So I was testing a bit of code for a project and noticed that for a text file I was using fstream and for reading the binary of a .tif image file, I was using ifstream. And when I switched the two around neither would work.

So I was wondering if the file type makes a difference or if the way you are opening the file matters.

Everything is in separate functions, I have all the #includes
BTW running and compiling through the unix shell on a Knoppix live CD. Using Gedit for writing c/c++ and using a make file given to us for the project to compile.

This Works for a text file.
But changing fstream to ifstream, it freezes
   fstream myfile (filename.c_str());
   if (!myfile) {
      //Do Stuff
   }
   
   //Do Other Stuff

   myfile.close();



This Doesn't Work for reading in a .tif image file. This gives me junk values upon output.
fstream imfile (filename.c_str(), ios::binary);
   if (!imfile) {
      //Do Stuff
   }




This works for a .tif image file and returns proper output.
   ifstream imfile;
   imfile.open (filename.c_str(), ios::binary);





Just if anyone was wondering, for reading the .tif files this is the very next thing after opening the file with the above code
   imfile.seekg (0, ios::beg);

   imfile.read (line,2);
   imfile.read ((char *)&line2,2);
   imfile.read ((char *)&line3,4);

   imfile.seekg(line3,ios::beg);
   imfile.read((char*)&line4,2);

   imfile.close();

   line[2]='\0';
   cout << line << "\n";
   cout << line2 << "\n";
   cout << line3 << "\n";
   cout << line4 << "\n";



So reading in the information from the .tif file is exactly the same, printing out from the .tif file is exactly the same. So I don't understand why one works and the other doesn't.

If I didn't explain well enough let me know. Thanks

Is This A Good Question/Topic? 0

Replies To: ifstream vs fstream, does filetype matter?

#2 ishkabible   User is offline

  • spelling expret
  • member icon





Reputation: 1749
  • View blog
  • Posts: 5,901
  • Joined: 03-August 09

Re: ifstream vs fstream, does filetype matter?

Posted 09 February 2011 - 09:23 PM

files types are a matter of interpretation. i can rename a .zip file to .love and still open it with 7z or WinRAR. file extension and file type are just matters of interpretation, nothing more. how do you know that it dosn't work? what are you doing, have you read the file format specification? what is happening as apposed to what you expect to happen?

This post has been edited by ishkabible: 09 February 2011 - 09:25 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1