file operations in Dev C++

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sksriharsha
    New Member
    • Oct 2007
    • 31

    #1

    file operations in Dev C++

    The fstream class is not creating a file when file.open(<file name>) is used. Why??
  • Meetee
    Recognized Expert Contributor
    • Dec 2006
    • 928

    #2
    Originally posted by sksriharsha
    The fstream class is not creating a file when file.open(<file name>) is used. Why??
    Can you post whatever you have done so far? It is not the case normally. So by seeing your code we can analyze the problem.
    Hope you have done something like this.
    [CODE=CPP] #include<fstrea m.h>
    fstream file_op("c:\\te st_file.txt",io s::in);
    [/CODE]

    Regards

    Comment

    • sksriharsha
      New Member
      • Oct 2007
      • 31

      #3
      This is the code.


      #include <iostream>
      #include <fstream>
      #include <string>
      #include <iomanip>
      using namespace std;
      int main()
      {
      string a("harsha");
      string b("Ratnesh");
      string c;
      string d;
      int x=1000,y;
      fstream file;


      file.open("TRIA L",ios::in|ios: :out);
      file.write((cha r *)&a,sizeof(a)) ;
      file.write((cha r *)&x,sizeof(x)) ;
      file.write((cha r *)&b,sizeof(b)) ;



      file.read((char *)&c,sizeof(b)) ;
      file.read((char *)&y,sizeof(y)) ;
      file.read((char *)&d,sizeof(c)) ;
      cout<<c<<endl;
      cout<<y<<endl;
      cout<<d<<endl;

      file.close();

      system("pause") ;
      }

      Now when I check my working directory, there is no file by name "TRIAL".
      The program runs without errors and the output is


      <empty line>
      0
      <empty line>

      But if execute the same program with ofstream and ifstream class there is no problem. The code is

      #include <iostream>
      #include <fstream>
      #include <string>
      #include <iomanip>
      using namespace std;
      int main()
      {
      string a("harsha");
      string b("Ratnesh");
      string c;
      string d;
      int x=1000,y;
      ofstream ofile;


      ofile.open("TRI AL");
      ofile.write((ch ar *)&a,sizeof(a)) ;
      ofile.write((ch ar *)&x,sizeof(x)) ;
      ofile.write((ch ar *)&b,sizeof(b)) ;

      ofile.close();

      ifstream ifile;
      ifile.open("TRI AL");
      ifile.read((cha r *)&c,sizeof(b)) ;
      ifile.read((cha r *)&y,sizeof(y)) ;
      ifile.read((cha r *)&d,sizeof(c)) ;
      cout<<c<<endl;
      cout<<y<<endl;
      cout<<d<<endl;

      ifile.close();

      system("pause") ;
      }


      The file TRIAL gets created.
      The output is

      harsha
      1000
      Ratnesh

      which is correct.
      But why is the fstream class not creating the file?

      Thank you for the response.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by sksriharsha
        But why is the fstream class not creating the file?
        Your first example was:
        Originally posted by sksriharsha
        file.open("TRIA L",ios::in|ios: :out);
        Because you said input or output, the file was not created due to the fact that input files are not created. If they were, they would be output files.

        Comment

        • sksriharsha
          New Member
          • Oct 2007
          • 31

          #5
          Hi,

          But the book says it is mandatory to specify mode with fstream class i.e. when we use open, we should use some ios:: mode.
          Moreover the mode has an 'or' operator. The very idea of using fstream is that a file can be opened both for reading and writing at the same time.
          Anyway, I'll try without specifying any mode first and then specifying only ios::out mode.

          Regards

          Comment

          • sksriharsha
            New Member
            • Oct 2007
            • 31

            #6
            Hi,
            The code


            fstream file;
            file.open("TRIA L",ios::out) ;
            --
            --
            --
            file.close();
            file.open("TRIA L",ios::in);
            --
            --
            --
            file.close;

            This seems to work fine. But why can't I write into and read from a file at the same time with

            file.open("TRIA L",ios::in|ios: :out);???


            Anyway, thank you for your suggestion.

            Comment

            • sksriharsha
              New Member
              • Oct 2007
              • 31

              #7
              Hi,

              By the way, the program of read/write simultaneously i.e.

              file.open("TRIA L", ios::in | ios::out);

              works fine in Turbo C++ compiler.

              So the problem is with the Bloodshed Dev C++ compiler.

              Can somebody tell them to correct it ??

              Thanks a lot. I have found my solution. You can close this thread...

              Regards.

              Comment

              Working...