Thread: problm with reading/writing objects in file

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    3

    problm with reading/writing objects in file

    I am using turboC++ compiler. I am dealing with files, having a large numbe of records( in five figures). I have stored all the records in form of class objects, using write(). Though I can't be sure of this, but I think that I have written the records properly. But while reading them, I am facing problems. Someimes, it is not reading at all, sometimes, it is reading it a bit incorectly, like, I have some string members also, so, the string is shifted by 1 character. I am reading them using read() only.

    I remember, once my teacher in school told me that in file handling, it is advisable o create a dummy record and then delete it, before writing into the file for the very first time.
    I don't know the reason.
    Anyways, I have tried that also, still no use.

    If anyone of you can help even a little bit, please do reply.

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    got code?

    generally, it's a bad idea to do a raw dump of a class.... output the individual members instead. Especially when you're using a variable-length class like string inside of it.

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    I want all the records to be of same size. This is because I want to search for the records, for which I am using interpolation search, as the numbe of records is very high.
    If I output them individually, the strings will take variable length, and hence all the lengths of records will be different, and I'll have to apply linear search only.

    I tried to read the file byte by byte, then I realised, that the problem is while writing the record.

    The following is the definition of th class:
    Code:
    enum prtype{error=1, notice, info};
    struct priority
    {
    	char msg[15];
    	char pr;
    	enum prtype type;
    };
    enum msgcat{lineupdown=1,others};
    struct message
    {
    	enum msgcat category;
    	char updown;
    	char mssg[170];
    	char interface[15];
    
    };
    class record
    {
    	unsigned long int date, time;
    	priority prrty;
    	int ip[4];
    	message m1;
    	public:
    	record()
    	{
    		date=time=0;
    		prrty.msg[0]='\0';prrty.pr=0;prrty.type=0;
    		ip[0]=ip[1]=ip[2]=ip[3]=0;
    		m1.mssg[0]='\0';m1.category=0;m1.interface[0]='\0';m1.updown=0;
    	}
    	int extractinfo(char *);
    	void displayall();
    	void writetofile(char *);
    	int checkip(char *);
    	void filename(char *);
    
    
    };

    Now, when I am reading it byte by byte, for a particular file created by the program, which has only four records in form of the object of this class, the first four bytes are occupied by date, the next four by time, in the last two records; but there are only three bytes of time in the first two cases. one byte is missing, and its the third byte( out of four byte long), not the first or last.
    I am not able to understand the reason.

    If you can recommend a better way to save the record, then it also can be very helpful.

    And code, can I mail it to you??
    Else, I am using
    Code:
    fout.write((char*)this,sizeof(record));
    fout.read((char*)this,sizeof(record));
    only to read and write records.



    Thanks very much for the help.

  4. #4
    Massively Single Player AverageSoftware's Avatar
    Join Date
    May 2007
    Location
    Buffalo, NY
    Posts
    141
    You should write stream operators for your objects and make them friends of the class so they can dump all the data members individually.

    Code:
    ostream &operator << (ostream &out, const record &r)
    {
        out << r.date << endl;
        out << r.time << endl;
        //etc...
    
        return out;
    }
    
    istream &operator >> (istream &in, record &r)
    {
        in >> r.date >> r.time >> //etc...
    
        return in;
    }
    There is no greater sign that a computing technology is worthless than the association of the word "solution" with it.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by rishimathur View Post
    Else, I am using
    Code:
    fout.write((char*)this,sizeof(record));
    fout.read((char*)this,sizeof(record));
    only to read and write records.
    yeah that's exactly what I was describing what not to do. Do what AverageSoftware said.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    3
    Thanks for the suggestions.
    I actually havn't learnt about operator overloading. So, I think first should consult some text on the same, and then I'll try it on my code. I hope that this works.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM