11 Replies - 12467 Views - Last Post: 13 May 2002 - 07:43 PM Rate Topic: -----

#1 RedArmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-May 02

Saving a linked list using fwrite?

Posted 01 May 2002 - 04:44 PM

I have been trying to set up an fwrite function that will save the information in a linked list to  a binary file, and , I m stuck, I have 5 different versions of this, none of them work, 6 classes of effort, nothing. This is for my final project, and if someone could show me the basics, I would be grateful!

Oh , Vc++ 6.0 :D

Is This A Good Question/Topic? 0
  • +

Replies To: Saving a linked list using fwrite?

#2 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4422
  • View blog
  • Posts: 28,350
  • Joined: 21-March 01

Re: Saving a linked list using fwrite?

Posted 01 May 2002 - 04:53 PM

ummm, fwrite? i always use ofstream
ofstream outfile;
outfile.open("data.dat",ios::out);

Was This Post Helpful? 0
  • +
  • -

#3 RedArmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-May 02

Re: Saving a linked list using fwrite?

Posted 01 May 2002 - 06:02 PM

we haven't learned that yet.... any tutorials around for ofstream?
Was This Post Helpful? 0
  • +
  • -

#4 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4422
  • View blog
  • Posts: 28,350
  • Joined: 21-March 01

Re: Saving a linked list using fwrite?

Posted 01 May 2002 - 07:17 PM

actually, its very simple, i can show you real fast.

first thing you need to do is in your file includes you need to put: #include <fstream.h> this allows for you to write to an outfile, and read from an infile.

next, you need to declare a keyword for writing out.
i put ofstream outfile; which means im using the keyword outfile, but you could put anything, like duck or whatever, just use that keyword in your later code.

next you need to open/create a file
outfile.open("data.dat",ios:out); creates/opens a file data.dat that can be written to with the keyword outfile. once again, you can replace outfile with your keyword. out means your writing out, you can also use app to add to a file instead of overwriting it everytime. if you use ifstream to read in files you can read in by putting in.

next you can write to your outfile. use your keyword to replace cout in a statement. so in my case outfile << "whatever";

when you done writing, close you file by putting outfile.close(); outfile is once again your keyword.

thats pretty much it. very easy. here is a sample of my save function for a linked list database i wrote:

void save()
{
current=head;
ofstream outfile;
outfile.open("data.dat",ios::out);
do
{
outfile << current->brand << "
";
outfile << current->type << "
";
outfile << current->year << "
";
outfile << current->gross << "
";
current=current->next;
}
while(current!=NULL);
outfile << "END OF FILE";
outfile.close();
cout << "Save succesful";
}

hope this is helpful. tell your friends where you got help at. :)

Was This Post Helpful? 1
  • +
  • -

#5 RedArmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-May 02

Re: Saving a linked list using fwrite?

Posted 01 May 2002 - 07:36 PM

Great! thanks for the help. I will spread the word :D
Was This Post Helpful? 0
  • +
  • -

#6 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4422
  • View blog
  • Posts: 28,350
  • Joined: 21-March 01

Re: Saving a linked list using fwrite?

Posted 01 May 2002 - 07:45 PM

:)
Was This Post Helpful? 0
  • +
  • -

#7 Null and Void  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 29-April 01

Re: Saving a linked list using fwrite?

Posted 01 May 2002 - 09:06 PM

Supersloth just led you on a complete tangent that probably has nothing to do with your program. Not that it matters though. Pseudocode to save a linked list to file:
Current = List's Head
while(Current is Valid) {
  write Current's data to File
  increment Current
}


To read it back in:
while(File pointer isn't at EOF) {
  read Data
  push onto end of List
}


Simple.

(Edited by Null and Void at 12:07 am on May 2, 2002)

Was This Post Helpful? 0
  • +
  • -

#8 supersloth  Icon User is offline

  • serial frotteur - RUDEST MEMBER ON D.I.C.
  • member icon


Reputation: 4422
  • View blog
  • Posts: 28,350
  • Joined: 21-March 01

Re: Saving a linked list using fwrite?

Posted 02 May 2002 - 03:49 PM

well, it sounded like he didnt know how to write... not figure out how to run thru each value. im guessing he knows how linked lists work if he wrote the code for the rest of the program. if i'm wrong please correct me RedArmy.
Was This Post Helpful? 0
  • +
  • -

#9 RedArmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-May 02

Re: Saving a linked list using fwrite?

Posted 02 May 2002 - 07:02 PM

Here is the Whole code, with SuperSloth's reccomendations, it runs, and the link works, and it will now save to a binary file, but how do I read them back? My teacher mentioned that I lose the pointer when I close the program and restart it, and i am @ a bit of a lost on how to get around it.

#include <stdlib.h>
#include <iostream.h>
#include <fstream.h>
#include <conFX2.h>
#include <apstring.h>
#include <apstring.cpp>
#include <time.h>

struct Student { int UserID; apstring Fname; apstring Sname; int SepID; } astudent;

struct Users { Student user; Users *next; }Userrecord; //userrecord->user.UserID //Userfile subject[25]; //Userfile tsubject;

//Function Prototypes void NewUser(int i,Users* &head, Users* &current_list); void Login(Users* &head); void Save(Users* &head);

//done

void NewUser(int i,Users* &head, Users* &current_list) { //Login (current_list); cout<<"create Users"<<endl; Users *list = new Users, *current_address, *previous_address;

int link; //else //{ i=0;

apstring entername; apstring enterlastname; //    int number_entered; current_list = list;    for (;; list = list->next) { cout << "Enter an ID number, press 0 to quit"<<endl; cin >> link; if (link) // > '0')            {

cout << "Enter the subjects Firstname: "<<flush; cin >> entername; cout << " Enter the subjects Lastname: "<<flush; cin >> enterlastname;            previous_address = current_address;            //list->number = number_entered; list->user.Fname = entername; list->user.Sname = enterlastname; list->user.UserID = link; list->user.SepID=rand();

i++;

           } else //if(number_entered = '0')                {                list = previous_address->next = NULL; delete current_address; // removes the new NODE created

break;                }

                               current_address = list->next = new Users;

               }

               clrscr(); Save(current_list);

//Login (current_list);

cout<<"Great, thanks"<<endl; getch();

//}

} void Save(Users* &head) { Users *savelist;

savelist=head; int i=0; for(savelist=head;savelist;savelist=savelist=savelist->next) { //cout << savelist->Fname<<endl; //cout << savelist->Sname<<endl; i++; //getch(); } /*FILE *fp; if ((fp =fopen("D:Profiles.dat","r+b")) == NULL) { fp =fopen("D:Profiles.dat","w+b"); // error //tprintf("Creating File, retry to create user"); getch(); }

savelist=head; while (savelist!=NULL) { cout <<"saving" <<endl; fwrite(&savelist->Fname,sizeof(&savelist),1,fp); savelist=savelist->next; } fclose(fp);*/ savelist=head; ofstream outfile; outfile.open("Profiles.dat",ios::out| ios::app |ios::binary); //outfile <<head<<" ";

do { outfile <<savelist->user.Fname <<" "; outfile <<savelist->user.Sname <<" "; outfile <<savelist->user.UserID <<" "; outfile <<savelist->user.SepID <<" "; savelist=savelist->next; } while(savelist!=NULL);

outfile << "END OF FILE"; outfile.close(); cout << "Save succesful";

  } void Login(Users* &head) { char ch ='0

ifstream readfile; readfile.open("Profiles.dat",ios::in |ios::binary); cout << "file opened" <<endl;_getch(); //do Users *loadlist;

//readfile <<head; loadlist=head; readfile.read((char *) &loadlist, sizeof(Users)); cout << "file opened2" <<endl;_getch(); while (!readfile.eof() ) {

//loadlist=loadlist->next; cout << loadlist->user.Fname; cout <<loadlist->user.Sname; cout <<loadlist->user.UserID; cout <<loadlist->user.SepID; loadlist=loadlist->next; readfile.read((char *) &loadlist, sizeof(Users)); getch(); } //while(loadlist!=NULL); //outfile << "END OF FILE"; readfile.close(); cout << "Load succesful";

}

char menu() { char ch; clrscr(); gotoxy(15,5); cout <<"Welcome to LBHS Math 31 Tutorial!"<< endl; gotoxy(25,7); cout <<"Would you like to...."<<endl; gotoxy(25,8); cout <<"[L]ogin"<<endl; gotoxy(25,9); cout<<"[C]reate a new user"<<endl; gotoxy(25,10); cout<<"[Q]uit?" <<endl; ch = getch(); //ch=toupper(ch); return ch; } void process(char ch,Users* &head) { //Users* list; //static Users* head; static Users* current_list; int i=0; if(ch == 'c') NewUser(i,head,current_list); else if(ch == 'l') Login(head); else if(ch == 's') Save(head);

} int main() { char ch; Users *head = NULL; head = new(Users); initscreen(); settitlebar("Tutorial"); //textcolor(fgWhite); //textbackground(bgBlue); clrscr(); ch = menu(); while (ch != 'Q') { process(ch,head); ch = menu(); } uninitscreen(); return 0; }

(Edited by RedArmy at 10:04 pm on May 2, 2002)

Was This Post Helpful? 0
  • +
  • -

#10 Null and Void  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 204
  • Joined: 29-April 01

Re: Saving a linked list using fwrite?

Posted 02 May 2002 - 07:09 PM

Don't save the pointer, save the data it points to.
Was This Post Helpful? 0
  • +
  • -

#11 RedArmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-May 02

Re: Saving a linked list using fwrite?

Posted 09 May 2002 - 10:06 AM

Still stuck here. The save works, but load remains a mystery. I found SuperSloth's code that saves and loads on this site, but cannot use anything like it, because I use apstring, and lines like "infile.get(newrec->brand,20); " returns convert errors. I have been working on this for a week, and the teacher is MIA. You guys are my last hope, HELP!
Was This Post Helpful? 0
  • +
  • -

#12 RedArmy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-May 02

Re: Saving a linked list using fwrite?

Posted 13 May 2002 - 07:43 PM

c'mon guys, I take desperate to a whole new level.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1