Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Troublesome Handling of Files in C++

Troublesome Handling of Files in C++

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++iostutorialquestion
6 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sujan Dhakal
    wrote on last edited by
    #1

    #include <iostream> #include <fstream> using namespace std; int main() { char data[5];//={'a','b','c','f','\0'}; //fstream file("c:\\test.txt",ios::out | ios::binary); fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); cout<<data; } file.close(); } else { cout<<"Error"; } return 0; } I've written "abcf" to text.txt file in binary mode. i'm reading back the data into the variable "data". why the heck the output is "abcfabcf". I should be "abcf" but what happened to this one. and another question do anybody knows how to write and read string data-type in binary mode. please help.

    CPalliniC S R 3 Replies Last reply
    0
    • S Sujan Dhakal

      #include <iostream> #include <fstream> using namespace std; int main() { char data[5];//={'a','b','c','f','\0'}; //fstream file("c:\\test.txt",ios::out | ios::binary); fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); cout<<data; } file.close(); } else { cout<<"Error"; } return 0; } I've written "abcf" to text.txt file in binary mode. i'm reading back the data into the variable "data". why the heck the output is "abcfabcf". I should be "abcf" but what happened to this one. and another question do anybody knows how to write and read string data-type in binary mode. please help.

      CPalliniC Offline
      CPalliniC Offline
      CPallini
      wrote on last edited by
      #2

      This works for me (error handling left to the reader)

      #include <iostream>
      #include <fstream>
      using namespace std;

      int main() {
      // write some characters in binary mode
      fstream file("C:\\test.txt", ios::out | ios::binary);
      char out[]="hello";
      file.write(out, sizeof(out));
      file.close();

      // read the written characters
      file.open("C:\\test.txt", ios::in | ios::binary);
      char in[sizeof(out)];
      file.read(in, sizeof(in));
      cout << out << ", " << in << endl;
      file.close();

      return 0;
      }

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
      [My articles]

      In testa che avete, signor di Ceprano?

      1 Reply Last reply
      0
      • S Sujan Dhakal

        #include <iostream> #include <fstream> using namespace std; int main() { char data[5];//={'a','b','c','f','\0'}; //fstream file("c:\\test.txt",ios::out | ios::binary); fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); cout<<data; } file.close(); } else { cout<<"Error"; } return 0; } I've written "abcf" to text.txt file in binary mode. i'm reading back the data into the variable "data". why the heck the output is "abcfabcf". I should be "abcf" but what happened to this one. and another question do anybody knows how to write and read string data-type in binary mode. please help.

        S Offline
        S Offline
        Stuart Dootson
        wrote on last edited by
        #3

        What's wrong with just using std::string and using the library provided operators, like here?

        fstream file("c:\\test.txt",ios::in | ios::binary);
        if(file.is_open())
        {
        std::string s;
        file >> s;
        std::cout << s << std::endl;
        file.close();
        }
        else
        {
        cout<<"Error";
        }

        Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

        modified on Wednesday, February 3, 2010 8:27 AM

        CPalliniC 1 Reply Last reply
        0
        • S Stuart Dootson

          What's wrong with just using std::string and using the library provided operators, like here?

          fstream file("c:\\test.txt",ios::in | ios::binary);
          if(file.is_open())
          {
          std::string s;
          file >> s;
          std::cout << s << std::endl;
          file.close();
          }
          else
          {
          cout<<"Error";
          }

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

          modified on Wednesday, February 3, 2010 8:27 AM

          CPalliniC Offline
          CPalliniC Offline
          CPallini
          wrote on last edited by
          #4

          The format... :-D On the serious side, while there is, of course, nothing wrong in your approach, there is, as well, nothing wrong in his one. Disclaimer: this is going on my... :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          In testa che avete, signor di Ceprano?

          S 1 Reply Last reply
          0
          • CPalliniC CPallini

            The format... :-D On the serious side, while there is, of course, nothing wrong in your approach, there is, as well, nothing wrong in his one. Disclaimer: this is going on my... :)

            If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
            This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
            [My articles]

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            I always like using idiomatic C++ rather than not :-)

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

            1 Reply Last reply
            0
            • S Sujan Dhakal

              #include <iostream> #include <fstream> using namespace std; int main() { char data[5];//={'a','b','c','f','\0'}; //fstream file("c:\\test.txt",ios::out | ios::binary); fstream file("c:\\test.txt",ios::in | ios::binary); if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); cout<<data; } file.close(); } else { cout<<"Error"; } return 0; } I've written "abcf" to text.txt file in binary mode. i'm reading back the data into the variable "data". why the heck the output is "abcfabcf". I should be "abcf" but what happened to this one. and another question do anybody knows how to write and read string data-type in binary mode. please help.

              R Offline
              R Offline
              Raj Indian
              wrote on last edited by
              #6

              fstream::eof() - returns true if the 'eofbit' has been set by a previous i/o operation. fstream::read(s, n) - sets 'eofbit' if the End-of-File is reached before n characters have been read. The file has only 5 characters and the while loop will execute twice. In the first pass you try to read 5 bytes and it will be success. In the second pass read will fail and sets the 'eofbit'. Since you are not checking how many bytes read, you cout the old buffer contents again. This is the reason why you got "abcfabcf". The code can be changed to print "abcf" once, by checking the bytes read using fstream::gcount() as ................. if(file.is_open()) { //file.write(data,sizeof(data)); while(!file.eof()) { file.read(data,sizeof(data)); if(file.gcount()) cout<<data; } file.close(); } ..................

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups