Well Guys! I was learning how to read and write class objects from & to file respectivily. And here is a small code that i made......
As far as the code goes its all well.... And i am getting what i want....

But what i want to know is, where is the file that i am using saved?? Here the file is BLAHBLAH.dat. I cannot find it in any of the folders in the solution folder. And even a system full search didn't help. So where is it saved.....

I use Microsoft Visual C++ 2010 Express Edition.

Here is the code:

#include<iostream>
#include<fstream>

using namespace std;

class test
{
    char name[10];
    int grade;
    int mark;
public:
    void getinfo();
    void showinfo();
};

void test::getinfo()
{
    cout<<"Enter the Name"<<endl;
    cin>>name;
    cout<<"Grade??"<<endl;
    cin>>grade;
    cout<<"Mark?"<<endl;
    cin>>mark;
}

void test::showinfo()
{
    cout<<"Name: "<<name<<endl;
    cout<<"Grade: "<<grade<<endl;
    cout<<"Mark: "<<mark<<endl;
}

int main()
{
    char opt;
    int i;
    fstream blah;
    blah.open("BLAHBLAH.dat",ios::in|ios::out);
    test a[5];
    for(i=1;i<=2;i++)
    {
        cout<<"Enter the Record "<<i+1<<endl;
        a[i].getinfo();
        cout<<endl<<endl;
        a[i].showinfo();
        cout<<endl;
        blah.write((char *)& a[i],sizeof(a[i]));
    }
    blah.seekg(0);
    do
    {
        cout<<"Which record would you like to print?"<<endl;
        cin>>i;
        blah.read((char *)& a[i], sizeof (a[i]));
        a[i].showinfo();
        cout<<endl;
        cout<<"Would you like to continue???"<<endl;
        cin>>opt;
    }while(opt=='y'||opt=='Y');
    blah.close();
    system("PAUSE");
    return 0;
}

I know, in this code, i just use the file for the runtime and not for multiple executions.

Usually at school, where we use turbo c++ (Horrible) the file could be seen in the folder where the cpp file is saved......

Thanks in advance guys........

Comments
Thanks for the your kind POST

Usually at school, where we use turbo c++ (Horrible) the file could be seen in the folder where the cpp file is saved......

Visual C++ does the same by default. Try running a simpler test:

#include <fstream>

int main()
{
    std::ofstream out("fooby.txt");
}

On my system that program places fooby.txt here:

C:\Users\deceptikon\Documents\Visual Studio 2012\Projects\CppScratch\CppScratch\fooby.txt

That folder is where the source files are stored, and that behavior hasn't changed between Visual Studio 2010 and 2012. It's also where the project (but not solution) file is stored. But as long as the name is unique, you can do a system search to find it. Don't forget to look in hidden folders too. ;)

The problem is open() fails because you're trying to open the file for reading when the file doesn't exist. The file must exist in order to use ios::read flag.

But as long as the name is unique, you can do a system search to find it. Don't forget to look in hidden folders too. ;)

I tried your code and yes it worked. But not in my code.. Having to do anything with the file extention?? In my code, i use dat file. Well Here is the Tree for your code and mine....

Users\Codnet\Desktop\C++ PROGRAM FOR EXAM\Test
   │
   ├[Debug]
   │   │
   │   ├Test.exe
   │   ├Test.ilk
   │   └Test.pdb
   ├[ipch]
   │   │
   │   └[test-9fba54c7]
   │       │
   │       └test-2434c52c.ipch
   ├[Test]
   │   │
   │   ├[Debug]
   │   │   │
   │   │   ├cl.command.1.tlog
   │   │   ├CL.read.1.tlog
   │   │   ├CL.write.1.tlog
   │   │   ├link-cvtres.read.1.tlog
   │   │   ├link-cvtres.write.1.tlog
   │   │   ├link.command.1.tlog
   │   │   ├link.read.1.tlog
   │   │   ├link.write.1.tlog
   │   │   ├mt.command.1.tlog
   │   │   ├mt.read.1.tlog
   │   │   ├mt.write.1.tlog
   │   │   ├rc.command.1.tlog
   │   │   ├rc.read.1.tlog
   │   │   ├rc.write.1.tlog
   │   │   ├stdafx.obj
   │   │   ├Test.exe.embed.manifest
   │   │   ├Test.exe.embed.manifest.res
   │   │   ├Test.exe.intermediate.manifest
   │   │   ├Test.lastbuildstate
   │   │   ├Test.log
   │   │   ├Test.obj
   │   │   ├Test.pch
   │   │   ├Test_manifest.rc
   │   │   ├vc100.idb
   │   │   └vc100.pdb
   │   ├**fooby.txt** //Here is your file
   │   ├ReadMe.txt
   │   ├stdafx.cpp
   │   ├stdafx.h
   │   ├targetver.h
   │   ├Test.cpp
   │   ├Test.vcxproj
   │   ├Test.vcxproj.filters
   │   └Test.vcxproj.user
   ├Test.opensdf
   ├Test.sdf
   ├Test.sln
   └Test.suo

And of mine

Users\Codnet\Desktop\C++ PROGRAM FOR EXAM\CLASS READ AND WRITE
   │
   ├[CLASS READ AND WRITE]
   │   │
   │   ├[Debug]
   │   │   │
   │   │   ├cl.command.1.tlog
   │   │   ├CL.read.1.tlog
   │   │   ├CL.write.1.tlog
   │   │   ├CLASS READ AND WRITE.Build.CppClean.log
   │   │   ├CLASS READ AND WRITE.exe.embed.manifest
   │   │   ├CLASS READ AND WRITE.exe.embed.manifest.res
   │   │   ├CLASS READ AND WRITE.exe.intermediate.manifest
   │   │   ├CLASS READ AND WRITE.lastbuildstate
   │   │   ├CLASS READ AND WRITE.log
   │   │   ├CLASS READ AND WRITE.obj
   │   │   ├CLASS READ AND WRITE.pch
   │   │   ├CLASS READ AND WRITE_manifest.rc
   │   │   ├link-cvtres.read.1.tlog
   │   │   ├link-cvtres.write.1.tlog
   │   │   ├link.4864-cvtres.read.1.tlog
   │   │   ├link.4864-cvtres.write.1.tlog
   │   │   ├link.4864.read.1.tlog
   │   │   ├link.4864.write.1.tlog
   │   │   ├link.command.1.tlog
   │   │   ├link.read.1.tlog
   │   │   ├link.write.1.tlog
   │   │   ├mt.command.1.tlog
   │   │   ├mt.read.1.tlog
   │   │   ├mt.write.1.tlog
   │   │   ├rc.command.1.tlog
   │   │   ├rc.read.1.tlog
   │   │   ├rc.write.1.tlog
   │   │   ├stdafx.obj
   │   │   ├vc100.idb
   │   │   └vc100.pdb
   │   ├CLASS READ AND WRITE.cpp
   │   ├CLASS READ AND WRITE.vcxproj
   │   ├CLASS READ AND WRITE.vcxproj.filters
   │   ├CLASS READ AND WRITE.vcxproj.user
   │   ├ReadMe.txt
   │   ├stdafx.cpp
   │   ├stdafx.h
   │   └targetver.h
   ├[Debug]
   │   │
   │   ├CLASS READ AND WRITE.exe
   │   ├CLASS READ AND WRITE.ilk
   │   └CLASS READ AND WRITE.pdb
   ├[ipch]
   │   │
   │   └[class read and write-8cd2d537]
   │       │
   │       └class read and write-d53c49fc.ipch
   ├CLASS READ AND WRITE.sdf
   ├CLASS READ AND WRITE.sln
   └CLASS READ AND WRITE.suo

So where am i wrong in my code???

The problem is open() fails because you're trying to open the file for reading when the file doesn't exist. The file must exist in order to use ios::read flag.

K! I think i got what i am doing wrong. Well! Will check on it and post tomorrow.

The problem is open() fails because you're trying to open the file for reading when the file doesn't exist. The file must exist in order to use ios::read flag.

So can i invoke a default behavoir, that if the file is non existent, the code makes one for the writing process????

Opening a file using constructor does that, but how to do it with open()???

Well! Here is the latest code.... I am getting the ouput written on a file (Opened the file from the Windows Explorer)

But the problem here is that, both of the class objects are getting saved to the file. But only one of them is getting read from the file..... The first record.....

How can i rectify this???

Here is the latest code.....(File exist on the disk, it was made when i used constructor, but i reverted back to open() )

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;

class test
{
    char name[10];
    int grade;
    int mark;
public:
    test()
    {
        strcpy(name," ");
        grade=0;
        mark=0;
    }
    void getinfo();
    void showinfo();
};

void test::getinfo()
{
    cout<<"Enter the Name"<<endl;
    cin>>name;
    cout<<"Grade??"<<endl;
    cin>>grade;
    cout<<"Mark?"<<endl;
    cin>>mark;
}

void test::showinfo()
{
    cout<<"Name: "<<name<<endl;
    cout<<"Grade: "<<grade<<endl;
    cout<<"Mark: "<<mark<<endl;
}

int main()
{
    char opt;
    int i;
    fstream blah;
    blah.open("BLAHBLAH.dat",ios::in|ios::out);
    test a[2],b[2];
    for(i=1;i<=2;i++)
    {
        cout<<"Enter the Record "<<i<<endl;
        a[i].getinfo();
        cout<<endl<<endl;
        a[i].showinfo();
        cout<<endl;
        blah.write((char *)& a[i],sizeof(a[i]));
    }
    do
    {
        blah.seekp(0);
        blah.seekg(0);
        cout<<"Which record would you like to print?"<<endl;
        cin>>i;
        blah.read((char *)& b[i], sizeof (b[i]));
        b[i].showinfo();
        cout<<endl;
        cout<<"Would you like to continue???"<<endl;
        cin>>opt;
    }while(opt=='y'||opt=='Y');
    blah.close();
    system("PAUSE");
    return 0;
}

Tree

Users\Codnet\Desktop\C++ PROGRAM FOR EXAM\CLASS READ AND WRITE
   │
   ├[CLASS READ AND WRITE]
   │   │
   │   ├[Debug]
   │   │   │
   │   │   ├cl.command.1.tlog
   │   │   ├CL.read.1.tlog
   │   │   ├CL.write.1.tlog
   │   │   ├CLASS READ AND WRITE.Build.CppClean.log
   │   │   ├CLASS READ AND WRITE.exe.embed.manifest
   │   │   ├CLASS READ AND WRITE.exe.embed.manifest.res
   │   │   ├CLASS READ AND WRITE.exe.intermediate.manifest
   │   │   ├CLASS READ AND WRITE.lastbuildstate
   │   │   ├CLASS READ AND WRITE.log
   │   │   ├CLASS READ AND WRITE.obj
   │   │   ├CLASS READ AND WRITE.pch
   │   │   ├CLASS READ AND WRITE_manifest.rc
   │   │   ├link-cvtres.read.1.tlog
   │   │   ├link-cvtres.write.1.tlog
   │   │   ├link.2548-cvtres.read.1.tlog
   │   │   ├link.2548-cvtres.write.1.tlog
   │   │   ├link.2548.read.1.tlog
   │   │   ├link.2548.write.1.tlog
   │   │   ├link.3372-cvtres.read.1.tlog
   │   │   ├link.3372-cvtres.write.1.tlog
   │   │   ├link.3372.read.1.tlog
   │   │   ├link.3372.write.1.tlog
   │   │   ├link.3628-cvtres.read.1.tlog
   │   │   ├link.3628-cvtres.write.1.tlog
   │   │   ├link.3628.read.1.tlog
   │   │   ├link.3628.write.1.tlog
   │   │   ├link.4864-cvtres.read.1.tlog
   │   │   ├link.4864-cvtres.write.1.tlog
   │   │   ├link.4864.read.1.tlog
   │   │   ├link.4864.write.1.tlog
   │   │   ├link.command.1.tlog
   │   │   ├link.read.1.tlog
   │   │   ├link.write.1.tlog
   │   │   ├mt.command.1.tlog
   │   │   ├mt.read.1.tlog
   │   │   ├mt.write.1.tlog
   │   │   ├rc.command.1.tlog
   │   │   ├rc.read.1.tlog
   │   │   ├rc.write.1.tlog
   │   │   ├stdafx.obj
   │   │   ├vc100.idb
   │   │   └vc100.pdb
   │   ├BLAHBLAH.dat                                **\\\\Here is the file**
   │   ├CLASS READ AND WRITE.cpp
   │   ├CLASS READ AND WRITE.vcxproj
   │   ├CLASS READ AND WRITE.vcxproj.filters
   │   ├CLASS READ AND WRITE.vcxproj.user
   │   ├ReadMe.txt
   │   ├stdafx.cpp
   │   ├stdafx.h
   │   └targetver.h
   ├[Debug]
   │   │
   │   ├CLASS READ AND WRITE.exe
   │   ├CLASS READ AND WRITE.ilk
   │   └CLASS READ AND WRITE.pdb
   ├[ipch]
   │   │
   │   └[class read and write-8cd2d537]
   │       │
   │       └class read and write-d53c49fc.ipch
   ├CLASS READ AND WRITE.opensdf
   ├CLASS READ AND WRITE.sdf
   ├CLASS READ AND WRITE.sln
   └CLASS READ AND WRITE.suo

Here is the output...... See for both the records, the first one is getting printed....

Enter the Record 1
Enter the Name
Joffin
Grade??
12
Mark?
100


Name: Joffin
Grade: 12
Mark: 100

Enter the Record 2
Enter the Name
Jeffin
Grade??
10
Mark?
90


Name: Jeffin
Grade: 10
Mark: 90

Which record would you like to print?
2
Name: Joffin
Grade: 12
Mark: 100

Would you like to continue???
y
Which record would you like to print?
1
Name: Joffin
Grade: 12
Mark: 100

Would you like to continue???

the loop on line 46 of the code you posted is wrong. All arrays are numbered 0, 1, 2, 3, etc. a[2] has numbers 0 and 1, there is no number 2. The loop sould be
for(i = 0; i < 2; i++)

You may have to adjust other loops with similar construct.

This article has been dead for over six months. Start a new discussion instead.