5 Replies - 718 Views - Last Post: 10 March 2016 - 07:40 AM Rate Topic: -----

#1 wingki0114   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 16

problem on random file processing with link list

Posted 09 March 2016 - 10:44 AM

I am working on an address book in GUI and one of my task is to save my record to .dat file.When the program close and then reopen , the data remain.(I can use a form call ShowAll to verify whether the data still remain or not)
let say 4 records have already been save in .dat file
Every thing works fine,I can show all 4 records before I close the program.
However I have a strange problem that when I reopen the program and press "ShowAll" only the first record can be shown,but my .dat file still have all 4 records.
I have tried to do this in CMD,but is still occur the same problem.
Here is my doubly linklist:
#include<string>
using std::string;
class Record2 {
public:
	Record2()
	{
		prevPtr = 0; // point to null at the beginning
		nextPtr = 0; // point to null at the beginning
	}
	// get a number
	int GetNum()
	{
		return number;
	}
	// set a number
	void SetNum(int num)
	{
		number = num;
	}
	// get the prev pointer
	Record2 *GetPrev()
	{
		return prevPtr;
	}
	// set the prev pointer
	void SetPrev(Record2 *ptr)
	{
		prevPtr = ptr;
	}
	// get the next pointer
	Record2 *GetNext()
	{
		return nextPtr;
	}
	// set the next pointer
	void SetNext(Record2 *ptr)
	{
		nextPtr = ptr;
	}
private:
	int number;
	Record2 *prevPtr;
	Record2 *nextPtr;


And this is what I do in the GUI
Record2* load(Record2 *data) {
			Record2 *currPtr, *tempPtr, *firstPtr;
			currPtr = data;
			firstPtr = 0;
			if (currPtr) {
				while (currPtr->GetNext() != 0) {
					tempPtr = currPtr;
					currPtr = tempPtr->GetNext();
				}
				while (currPtr != 0) {
					tempPtr = currPtr;
					currPtr = tempPtr->GetPrev();
					delete tempPtr;
				}
			}
			ifstream inData("data.dat",ios::in);
			if (!inData) {	
				MessageBox::Show("File could not be opened.", "Message", MessageBoxButtons::OK);
				exit(1);
			}
			inData.read(reinterpret_cast<char *>(SaveRecord), sizeof(RecordAccess));
			inData.close();
			while (!inData.eof()) {

					if (!currPtr) {
						currPtr = new Record2;
						tempPtr = currPtr;
						currPtr->setName(SaveRecord->getDataName());
						currPtr->setEmail(SaveRecord->getDataEmail());
						currPtr->setPhone(SaveRecord->getDataPhone());
						firstPtr = currPtr;
					}
					else {
						tempPtr = new Record2;
						tempPtr->setName(SaveRecord->getDataName());
						tempPtr->setEmail(SaveRecord->getDataEmail());
						tempPtr->setPhone(SaveRecord->getDataPhone());
						tempPtr->SetNext(tempPtr);
						tempPtr->SetPrev(currPtr);
						currPtr = tempPtr;

				inData.read(reinterpret_cast<char *>(SaveRecord), sizeof(RecordAccess));
				inData.close();
			} 

			return firstPtr;
		}
//skip all the build in code
if (radioButton5->Checked) {
			int recordNumber = DataNumber(data);
			showAll^ F5 = gcnew showAll(recordNumber, data);
			F5->ShowDialog();
			load(data);
		}



Is This A Good Question/Topic? 0
  • +

Replies To: problem on random file processing with link list

#2 jimblumberg   User is offline

  • member icon

Reputation: 5765
  • View blog
  • Posts: 17,651
  • Joined: 25-December 09

Re: problem on random file processing with link list

Posted 09 March 2016 - 10:57 AM

I suggest you post the smallest possible complete program that illustrates the problem. For this problem you should be providing at least the write() method, the read() method and a small main that allows one to write the file with several records then allows one to read the file so we can compile and run said program and see the results ourselves. If possible avoid using any operating system specific code and stick with standard C/C++ code (we probably don't need a fancy GUI).

Also are you writing the actual data and not just writing the pointers to the file?

Jim
Was This Post Helpful? 0
  • +
  • -

#3 snoopy11   User is offline

  • Engineering ● Software
  • member icon

Reputation: 1556
  • View blog
  • Posts: 4,930
  • Joined: 20-March 10

Re: problem on random file processing with link list

Posted 09 March 2016 - 12:10 PM

Well a few things,

you read in a record on line 21 and then close the file on line 22.

Why do you do that ?

then in your while loop you attempt to read from a closed file on line 42

which you then close on line 43, yet its already closed on line 22.

Why do you do that ?

Regards

Snoopy.

This post has been edited by snoopy11: 09 March 2016 - 01:26 PM
Reason for edit:: typo

Was This Post Helpful? 0
  • +
  • -

#4 wingki0114   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 16

Re: problem on random file processing with link list

Posted 10 March 2016 - 05:51 AM

View Postsnoopy11, on 10 March 2016 - 03:10 AM, said:

Well a few things,

you read in a record on line 21 and then close the file on line 22.

Why do you do that ?

then in your while loop you attempt to read from a closed file on line 42

which you then close on line 43, yet its already closed on line 22.

Why do you do that ?

Regards

Snoopy.

I asked the teacher and his said I may try to close the file when I finish the file processing,I think I misunderstand his meaning so I put the close() in both line and line 42, however I delete this two line now.
Was This Post Helpful? 0
  • +
  • -

#5 wingki0114   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 09-March 16

Re: problem on random file processing with link list

Posted 10 March 2016 - 06:29 AM

View Postjimblumberg, on 10 March 2016 - 01:57 AM, said:

I suggest you post the smallest possible complete program that illustrates the problem. For this problem you should be providing at least the write() method, the read() method and a small main that allows one to write the file with several records then allows one to read the file so we can compile and run said program and see the results ourselves. If possible avoid using any operating system specific code and stick with standard C/C++ code (we probably don't need a fancy GUI).

Also are you writing the actual data and not just writing the pointers to the file?

Jim

Actually I use Record2* load(Record2 *data) and Record2* save(Record2 *data) to save and read the data from dat file.

And I am trying write the data on the link list to the dat file.

My desktop just crash and I lost the CMD test program....I think I need to spend sometime to convert my GUI work into CMD...
May be the header of random access file can help my question
#include <string>
using std::string;

class RecordAccess {
public:


	void setDataName(string name) {
		const char *Name = name.data();
		int length = name.size();
		length = (length < 30 ? length : 29);
		strncpy(saveName, Name, length);
		saveName[length] = '\0';
	}
	string getDataName() const {
		return saveName;
	}

	void setDataEmail(string email) {
		const char *Email = email.data();
		int length = email.size();
		length = (length < 30 ? length : 29);
		strncpy(saveEmail, Email, length);
		saveEmail[length] = '\0';
	}
	string getDataEmail() const {
		return saveEmail;
	}

	void setDataPhone(int tel) {
		savePhone = tel;
	}
	int getDataPhone() const {
		return savePhone;
	}


private:
	char saveName[30];
	char saveEmail[30];
	int savePhone;
};



Was This Post Helpful? 0
  • +
  • -

#6 kaa   User is offline

  • D.I.C Addict

Reputation: 225
  • View blog
  • Posts: 805
  • Joined: 15-April 11

Re: problem on random file processing with link list

Posted 10 March 2016 - 07:40 AM

Here's what your list looks like after the third record is read from the file. I think this diagram explains why you can only print 1 record.





(picpaste.com doesn't seem to work very well - apparently the url keeps changing. I see it works better if I just attach it as a file)

Attached image(s)

  • Attached Image

This post has been edited by kaa: 10 March 2016 - 08:24 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1