Thread: Need help with FileI/O and Linked lists.

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    13

    Need help with FileI/O and Linked lists.

    Totally stuck! I can enter and save info fine(the program only hadnles First names right now, as a test. However, the load function fails.

    Ex.
    INPUT

    Joe

    Shmoe

    it reads out...
    #0
    Joe
    (it then saves)
    #1
    Shmoe
    (It then Saves)
    (The program breaks to menu)
    I press L for the login function

    it outputs JoeSchmoe as one line, and no matter what I do, I cannot get it to stop reading and outputting the entire file as a single line of text. Any help would be appreciated, the code is below.
    Code:
    #include <stdlib.h>
    #include <iostream.h>
    #include <fstream.h>
    #include <conFX2.h>
    #include <apstring.h>
    #include <apstring.cpp>
    #include <time.h>
    
    typedef struct Save
    {
    	char SaveFname[12];
    }data;
    typedef struct Student
    {
    	int UserID;
    	char Fname[12];
    	char Sname[12];
    	int SepID;
    	
    } astudent;
    
    struct Users
    {
    	Student user;
    	Users *next;
    }Userrecord;
    //userrecord->user.UserID
    //Userfile subject[25];
    //Userfile tsubject;
    
    //Function Prototypes
    data savestudent[25];
    data saveAstudent;
    void NewUser(int i,Users* &head, Users* &current_list);
    void Login(int i,Users* &head);
    void Save(Users* &head,int i);
    
    //done
    
    
    
    void NewUser(int i,Users* &head, Users* &current_list)
    {
    	char cho;
    	int endloop=0;
    	//apstring Fname; 
    	//apstring Sname;
    	 //ID;
    	Users *list = new Users, *current_address, *previous_address;
    	current_list=list;
    	cout << "Welcome to the User creation Function"<<endl;
    	cout << "Please answer the following questions."<<endl;
    	for (;; list = list->next) 
    	{
    		//add previous addresses etc
    		cout <<"What is your first name? " <<endl;
    		//cout<<endl;
    		
    		cin.getline(list->user.Fname,12);
    		cout <<"\n";
    		//cout << list->user.Fname<<endl;
    		cout << "Now, your last name"<<endl;
    		cin.getline(list->user.Sname,12);
    		cout <<"\n";
    		cout << "Finally, enter your ID"<<endl;
    		list->user.UserID=rand();
    		cout <<"\n"<<"This User has been assigned an ID: "<< list->user.UserID<<"Please remember this number if you wish access this user."<<endl;
    		cout <<"Would you like to make a new User?[Y/N]"<<endl;
    		previous_address=current_address;
    		i++;
    		cho=getch();
    	if (cho=='n')
    	{
    		//endloop=1;
    		list = previous_address->next = NULL;
    		//delete current_address;
    		break;
    		
    	}
    	else
    	{
    		cout << " Preparing for new entry" <<endl;
    		//getch();
    		current_address = list->next = new Users;
    		//list=list->next;
    	}
    	}//while(endloop!=1);
    	Save(current_list,i);
    	 
    }
    void Save(Users* &head,int i)
    {
    	
    	Users *savelist;
    	ofstream outfile;
    	outfile.open("Profiles.dat",ios::out| ios::app |ios::binary);
    	//savelist=head;
    	//int i=0;
    	int j =0;
    	for(savelist=head;savelist;savelist=savelist=savelist->next)
    	{
    		cout <<j<<endl;
    		strcpy(savestudent[j].SaveFname,savelist->user.Fname);
    		//string copy savelist to struct SAVE
    		//cout <<j<<endl;
    		//outfile << savelist->user.UserID;
    		//outfile <<"name 1" <<savestudent[j].SaveFname<<"*";//<<endl;
    		//outfile<< savestudent[j].SaveFname <<endl;
    		//cout <<"name "<<j<<" :"<<savelist->user.Fname;
    		getch();
    		outfile<< savestudent[j].SaveFname;// <<endl;
    		cout << savestudent[j].SaveFname <<endl;
    	//	outfile << savelist->user.Fname;//<<endl;
    		 
    		j++;
    		getch();
    		
    	}
    	//cout << savelist->user.Fname<<endl;
    	//clrscr();
    
    	//j=0;
    	//cout<< savestudent[j].SaveFname <<endl;
    	getch();
    	//cout<<"2"<<endl;
    	//j=1;
    	//cout<< savestudent[j].SaveFname <<endl;
    	getch();
    	savelist=NULL;
    	outfile.close();
    
    	
    
    }
    void Login(int i,Users* &head)
    {
    	cout << "login Function " <<endl;
    	Users *loadlist=new Users;
    	ifstream infile;
    	int j=0;
    	cout <<j<<endl;
    	infile.open("Profiles.dat",ios::in| ios::app | ios::binary);
    	//infile.seekg(ios::beg); 
    	do
    	{
    		//cout <<"brak"<<endl;
    		//load info INTO new list
    		//infile >> loadlist->user.UserID;
    		infile >> savestudent[j].SaveFname;
    		cout <<j<<endl;
    		//cout << savestudent[j].SaveFname <<endl;
    		getch();
    		strcpy(loadlist->user.Fname,savestudent[j].SaveFname);
    		cout <<j<<endl;
    	
    		cout << loadlist->user.Fname <<endl;
    		//infile >> loadlist->user.Sname;
    		cout << " where do I crash??" <<endl;
    		loadlist=loadlist->next;
    			j++;
    		cout << " where do I crash??" <<endl;
    		
    	}while(j==1);
    	cout << " Do I break the loop? " <<endl;
    	infile.close();
    	loadlist=head;
    	for(loadlist=head;loadlist;loadlist=loadlist=loadlist->next)
    	{
    		//cout << loadlist->user.UserID <<endl;                                       
    		cout << loadlist->user.Fname <<endl;
    		//cout << loadlist->user.Sname <<endl;
    	}
    	
    }
    
    char menu()
    {
    	char ch;
    	clrscr();
    	gotoxy(15,5);
    
    	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;
    	
    	const int i=0;
    	if(ch == 'c')
    		NewUser(i,head,current_list);
    	else if(ch == 'l') 
    		Login(i,head);
    	else if(ch == 's') 
    		Save(head,i);
    
    	
    }
    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;
    }

  2. #2
    PC Fixer-Upper Waldo2k2's Avatar
    Join Date
    May 2002
    Posts
    2,001
    no offense, but when you ask a question that is pretty much aimed at a part of your code...don't insert your entire source file into the message, attach it and only type into the message a few lines, it's probably discouraging people from helping, i know i don't feel like diving into that right now to find one bug
    try again in a day or so and just put a little code on the page, attach the rest, good luck
    PHP and XML
    Let's talk about SAX

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Singly Linked Lists: Clarification Needed
    By jedispy in forum C++ Programming
    Replies: 4
    Last Post: 12-14-2006, 05:30 PM
  2. Linked Lists 101
    By The Brain in forum C++ Programming
    Replies: 5
    Last Post: 07-24-2004, 04:32 PM
  3. Map file formats and linked lists
    By Spitball in forum Game Programming
    Replies: 2
    Last Post: 03-04-2004, 11:32 PM
  4. need help w/ linked lists
    By MKashlev in forum C++ Programming
    Replies: 11
    Last Post: 08-05-2002, 08:57 PM
  5. doubly linked lists
    By qwertiop in forum C++ Programming
    Replies: 3
    Last Post: 10-03-2001, 06:25 PM