# Data File handling problem...

This topic is 4227 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hello again. Was making a basic program for creating (and writing into) a binary data file (I hope I've done it right). I use Code::Blocks 1.0 Release Candidate 2.
[source lang=cpp]
#include&lt;iostream&gt;;
#include&lt;fstream&gt;;
using namespace std;
int main()
{
fstream file;
file.open("Student.dat", ios::in|ios::out|ios::binary);
file.write((char*)&obj, sizeof(obj)); //Line 8
return 0;
}


Two errors on line 8 - 1. 'obj' undeclared (first use this function) 2. (Each undeclared identifier is reported only once for each function it appears in) What am I supposed to do? o.O [Edited by - BlueBolt on May 20, 2007 6:51:48 AM]

##### Share on other sites
Well, I suggest considering what the compiler is actually telling you. Based on your code, you don't declare obj anywhere, which is of course the problem here. Did you perhaps get this code straight from somewhere else without entirely understanding how it works? The simple solution of course is to define your obj somewhere else, eg. as an integer, or whatever else you require.
int obj = 0;

However, rather then hacking something in to make it compile, you would be better served figuring out how the code works first, as you'll then be able to solve the problems that come up in the future for yourself.

##### Share on other sites
Quote:
 Original post by SirLuthorDid you perhaps get this code straight from somewhere else without entirely understanding how it works?

Perfectly true. ToT
Well, I know what to do now.
Thanks.

##### Share on other sites
More problems. x_x
A code fragment from the program...
[source lang=cpp]int main(){    Student arts[3];    fstream filin;    filin.open("Stu.dat", ios::in|ios::out|ios::binary);    if(!filin)    {        cout&lt;&lt;"\nCannot open file...";        return 1;    }

It always says "Cannot open file...". Sorta logical too, considering there *is* no file. Are we supposed to manually create it first, or is there something I've missed?

[Edited by - BlueBolt on May 20, 2007 6:23:24 AM]

##### Share on other sites
If there's no "Stu.dat" on your disk, how are your going to open it and what do you intend to read from it?

On the other hand, opening it for writing is a different story, because you can create and populate the file.

##### Share on other sites
For various reasons, it is usually a much better idea to separate the reading and writing of a file, like so:

int main() {    // an anonymus scope allows the file    // object to close itself    // we would normally put this in a function    // but then we'd have to talk about why arrays are evil    // and to suggest std::vector    // I sense this is for another day however :)    {       // try to read       std::ifstream in("somefile.txt");       if( in.is_open() ) {          // read current contents       } else {          std::cerr << "Couldn't open file, starting with no raw data :(\n";          // fill array with default values       }    }    // gather some data from user, etc    // when done, output everything    std::ofstream out("somefile.txt");    // do outputting.}

Reusing the same fstream object is a bit messy IMHO.

##### Share on other sites
Im actually having a bit of a problem with the basics of file handling.
Lets clear this-
When you open a file for writing (even if the file is not there) with first the open line and then the write line, you are creating the said file?
Edit - Eventually, I made this
#include<iostream>#include<fstream>using namespace std;int main(){    class test    {        private:            char lineone[10];        public:            void input()            {                gets(lineone);            }            void output()            {                puts(lineone);            }    };    test obj;    fstream filin;    filin.open("Student.dat", ios::in|ios::out|ios::binary);    filin.write((char*)&obj, sizeof(obj));    obj.input();    /*filin.read((char*)&obj, sizeof(obj));    obj.output();*/  //I change filin.write line to comment for read operation and vice versa.    return 0;}

When I write to the file, program is ok, but when I read the file, it just gives me junk data.

[Edited by - BlueBolt on May 21, 2007 10:14:35 AM]

##### Share on other sites
You're getting junk back because you're writing junk out. First off don't start off with binary files. You cannot verify what's been written is correct or what you expected at least. Secondly stop trying to write complex things like classes. First get basic text working.

#include <fstream>#include <string>#include <iostream>int main(){   std::fstream out("test.txt", ios::out);   // Always check it's open, don't just assume, ever.   if (out.is_open())   {       out << "Did this write out ok?";       out.close();   }   std::fstream in("test.txt", ios::in);   if (in.is_open())   {       std::string text;       std::getline(in, text);       std::cout << text;   }   return 0;}

I left out error handling as others have clearly shown how to do this and your last example just completely skipped it. Check for valid states or its going to bite you.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633722
• Total Posts
3013549
×