A random file access question

This is a discussion on A random file access question within the C++ Programming forums, part of the General Programming Boards category; ok so i am doing a homework with random access files . I created a class with all the variables ...

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    21

    A random file access question

    ok so i am doing a homework with random access files .
    I created a class with all the variables and made the implementation. Now i am trying to create a .dat file and create 50 blank records in it but i cannot seem to get the data written into it at all with the write() function, here is parts of the code:
    Code:
    //record.h
    #include <iostream>
    using namespace std;
    class record
    {
    private:
    	int recordNum;
    	char toolname[25];
    	int quantity;
    	double cost;
    
    public:
    	void setRecordNumber(int);
    	int getRecordNumber() const;
    
    	void setToolName(string);
    	string getToolName() const;
    
    	void setQuantity(int);
    	int getQuantity() const;
    
    	void setCost(double);
    	double getCost() const;	
    };
    Code:
    //record.cpp
    #include <iostream>
    #include <cstring>
    #include <string.h>
    #include "record.h"
    
    
    using namespace std;
    
    void record::setRecordNumber(int number)
    {
    	recordNum = number;
    }
    
    int record::getRecordNumber() const
    {
    	return recordNum;
    }
    
    void record::setToolName(string tool)
    {
    	const size_t newsize = 25;
        char name[25];
        strcpy_s(name, tool.c_str());
    	toolname[25] = name[25];
    }
    
    string record::getToolName() const 
    {
    	return toolname;
    }
    
    void record::setQuantity(int q)
    {
    	quantity = q;
    }
    
    int record::getQuantity() const
    {
    	return quantity;
    }
    
    void record::setCost(double price)
    {
    	cost = price;
    }
    
    double record::getCost() const
    {
    	return cost;
    }
    Code:
    //function to create and initialize .dat file with 50 blank records
    void initializefile()
    {
    	record blank;
    	blank.setCost(0.0);
    	blank.setQuantity(0);
    	blank.setRecordNumber(0);
    	blank.setToolName(" ");
    	ofstream create("hardware.dat", ios::out|ios::binary);
    
    	if (!create)
    	{
    		exit(1);
    	}
    	create.write("Record# Tool name Quantity Cost\r\n",sizeof(record) );
    	for(int x =0 ;x < 50 ; x++)
    	{
    	create.seekp((x+1)sizeof(record), ios::beg);
    	create.write(reinterpret_cast <char*> (&blank) , sizeof(record));
    	}
    when using notepad to view the DAT file, its just garbage and the data is all over the place with weird characters, i want 0 to be in all the fields except toolname where its just blank but i cant seem to get any data written to the file because of the reinterpret_cast as i couted the result of each data before that line and the answer was fine , it wasn't garbled or anything.
    I would appreciate some help with this.

    thanks in advance.
    Last edited by Shadow20; 11-23-2011 at 09:13 PM.

  2. #2
    and the hat of Marriage Rodaxoleaux's Avatar
    Join Date
    Sep 2011
    Location
    ntdll.dll
    Posts
    178
    Code:
    create.write(reinterpret_cast <char*> (&blank) , sizeof(record));
    Sorry. It's not that easy. You have to serialize your class and write each individual member (since all are basic types) to the file. What you're doing now is writing memory values pointing to the class to the file (I think.)
    How to ask smart questions

    Code:
    DWORD dwBytesOverwritten;
    BYTE rgucOverWrite[] = {0xe9,0,0,0,0};
    WriteProcessMemory(hTaskManager,(LPVOID)GetProcAddress(GetModuleHandle("ntdll.dll"),"NtQuerySystemInformation"),rgucOverWrite,5,&dwBytesWritten);

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    2,760
    You are writing a Binary file, a binary file is not readable by Notepad. You will have problems with your file because of the following line:
    Code:
    create.write("Record# Tool name Quantity Cost\r\n",sizeof(record) );
    You do not need nor do you want a header line for your file. Also not the second parameter to write is the number of bytes to write, I doubt that record has the same number of bytes as your constant C-string. When you read this binary file you need to know exactly how the file was written. You must read exactly the same number of bytes that were written, and in the exact order. There does not seem anything wrong with the following line:
    Code:
    create.write(reinterpret_cast <char*> (&blank) , sizeof(record));
    This should write out the complete structure/class to the file correctly.

    If you want to read the file with Notepad you should abandon the binary file and just use the C++ stream insertion/extraction operator<<, operator>> to write your data.

    @Rodaxoleaux: Since all the data members are Plain Old Data (POD) with no pointers or other containers you do not "have" to serialize your data by printing each individual member. If things like std::string, std::vector, or pointers were involved then you would have to write each individual item.


    Jim
    Last edited by jimblumberg; 11-24-2011 at 10:34 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help compiling random access file
    By newbc in forum C Programming
    Replies: 5
    Last Post: 04-08-2011, 12:54 PM
  2. Random Access Text file
    By Dkamaque in forum C Programming
    Replies: 2
    Last Post: 03-11-2011, 08:29 AM
  3. pointer to an open random-access file
    By lio in forum C Programming
    Replies: 2
    Last Post: 11-30-2010, 12:52 AM
  4. random file access
    By bob20 in forum C++ Programming
    Replies: 6
    Last Post: 11-27-2002, 09:28 PM
  5. question on random file access
    By ygfperson in forum C++ Programming
    Replies: 1
    Last Post: 06-16-2002, 05:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21