Saving and Retrieving Disk File

This is a discussion on Saving and Retrieving Disk File within the C++ Programming forums, part of the General Programming Boards category; Hey; quick question if anyone can help me. I am entering First name and Last name and then searching for ...

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    11

    Saving and Retrieving Disk File

    Hey; quick question if anyone can help me. I am entering First name and Last name and then searching for it to see if it is there. It finds the name before I save it to a disk file; but when I retrieve it back and search again it doesn't work. That's because I have to put carriage returns when saving to a disk file so that it knows which information is separated. Thus; when retrieving data I am trying to make it so that it gets rid of the carriage returns. But searching for name still doesn't work..

    I know it is in my Retrievedata. You can do a CTRL+F for Retrievedata; I am sorry for posting the whole code here; hehe.
    If anyone can hint me to the problem, would be great . Thanks!

    Code:
    #include <conio.h>
    #include <stdio.h>
    #include <iostream.h>
    #include <graphics.h>
    #include <dos.h>
    #include <fstream.h>
    #include <stdlib.h>
    #include <string.h>
     
    void inputdata();
    void printdata();
    void printdatatwo();
    void retrievedata();
    void searchfull();
    void searchlast();
    void searchzip();
    void searchcitystate();
    void sortabc();
    void sortcity();
    void sortzip();
    void savedata();
    void sortstate();
     
    char choice;
    char snum[25], sage[10],szip[10];
    char string[25][25],name[50];
    int num, namecmp, age[10], I, u;
     
     
    struct info {
    	char namel[15];
    	char namef[15];
    	char string[50];
    	char city[15];
    	char state[15];
    	char phone[15];
    	char zipcode[15];
    	};
    	info MyArr[25];
     
     
     
    int main()
    {
    	int a;
    	do{
    	clrscr();
    	cout<<"1. Create Database \n";
    	cout<<"2. Save Database \n";
    	cout<<"3. Retrieve Database \n";
    	cout<<"4. Print Database Before Retrieve\n";
    	cout<<"5. Print Database After Retrieve\n";
    	cout<<"6. Quit\n\n";
     
     
    	cout<<"7. Search by Last Name Only \n";
    	cout<<"8. Search by Full Name \n";
    	cout<<"9. Search by Zipcode \n";
    	cout<<"10. Search by City/State \n";
    	cout<<"11. Sort Alphabetically \n";
    	cout<<"12. Sort by City\n";
    	cout<<"13. Sort by State\n";
    	cout<<"14. Sort by Zipcode \n\n";
     
    	cout<<"Answer: ";cin>>a;
    	if(a==1)
    	{inputdata();}
     
    	if(a==2)
    	{savedata();}
     
    	if(a==3)
    	{retrievedata();}
     
    	if(a==5)
    	{printdata();}
     
    	if(a==4)
    	{printdatatwo();}
     
    	if(a==7)
    	{searchlast();}
     
    	if(a==8)
    	{searchfull();}
     
    	if(a==9)
    	{searchzip();}
     
    	if(a==10)
    	{searchcitystate();}
     
    	if(a==11)
    	{sortabc();}
     
    	if(a==12)
    	{sortcity();}
    	if(a==13)
    	{sortstate();}
     
    	if(a==14)
    	{sortzip();}
     
     
    	}while(a!=6);
    	if(a==6)
    	{return 0;}
     
     
     
    getch();
    return 0;
    }
     
    void inputdata()
    {  do{
    	num=num+1;
    	u++;
    cout<<"Enter First Name: "; cin>>MyArr[num].namef;
    cout<<"Enter Last Name: "; cin>>MyArr[num].namel;
    printf("Enter Address: ");gets(MyArr[num].string);
     
    cout<<"Enter City: "; cin>>MyArr[num].city;
    cout<<"Enter State: "; cin>>MyArr[num].state;
    cout<<"Enter Zipcode: "; cin>>MyArr[num].zipcode;
    printf("Enter Phone #: ");gets (MyArr[num].phone);
     
    cout<<"Anymore? Y/N: "; cin>> choice;
    }while(choice=='y' || choice=='Y');
    }
     
     
    void savedata()
    {   ofstream outfile;
     
    outfile.open("U:\\EXAMPLE.DAT",ios::out);
    if(outfile)
    { outfile<<num<<"\n";
      for(I=1; I<=num; I++)
     { outfile<<MyArr[I].namef<<"\n";
       outfile<<MyArr[I].namel<<"\n";
       outfile<<MyArr[I].city<<"\n";
       outfile<<MyArr[I].string<<"\n";
       outfile<<MyArr[I].state<<"\n";
       outfile<<MyArr[I].zipcode<<"\n";
       outfile<<MyArr[I].phone<<"\n";
     }
    }
    else
    {cout<<"what did u do wrong ";}
     
    outfile.close();
     
    }
     
    void retrievedata()
    {	ifstream infile;
    	infile.open("U:\\EXAMPLE.DAT",ios::in);
    	char tempf[10], templ[10], tempc[10], tempa[10], temps[10], tempz[10], tempp[10];
            int fl, ll, cl, al, sl, zl, pl;
     
    	if(infile)
    {
    	infile.getline(snum,10);
    	num=atoi(snum);
     
    	for(I=1; I<=num; I++)
         { infile.getline(tempf,10);
    	infile.getline(templ,10);
    	infile.getline(tempc,10);
    	infile.getline(tempa,20);
    	infile.getline(temps,10);
    	infile.getline(tempz,10);
    	infile.getline(tempp,50);
     
    	fl=strlen(tempf);
    	strncpy(MyArr[I].namef,tempf,fl-1);
    	ll=strlen(templ);
    	strncpy(MyArr[I].namel,templ,fl-1);
    	cl=strlen(tempc);
    	strncpy(MyArr[I].city,tempc,fl-1);
     
         }
     
    }
     
    else
    {cout<<"There was something wrong with the file!";}
     
     
    infile.close();
     
     
     
    }
     void printdata()
    {}
     
    void printdatatwo()
    {}
     
     
    void searchlast()
    {
    	int I, found,f,ptr;
    	char Sname[25];
    	ptr=0;
     
    	clrscr();
    	do{ 
    	cout<<"Last Name You Are Searching For: ";
    	gets (Sname);
    	cout<<"\n";
    	found=0;
     
    	for(I=1; I<=u; I++)
    	{       ptr=strcmpi(Sname, MyArr[I].namel);
    		if (ptr==0)
    		{
    			    cout<<"The Last Name " <<Sname<<" Was Found ";
    				cout<<"In: "; cout<<""<<MyArr[I].namef; cout<<" "<<MyArr[I].namel;cout<<"\n";
     
    				found++;
     
    		}
     
     
     
    	}
            	if (found==0)
                    { cout<<"No one has this last name.\n\n";}
     
    	cout<<"Search a new #? 1=Yes | 2=No: ";
    	cin>>f;
    	clrscr();		 
            }while(f!=2);
     
     
    }
     
     
    void searchfull()
    {}
    void searchzip()
    {}
    void searchcitystate()
    {}
    void sortabc()
    {}
    void sortcity()
    {}
    void sortstate()
    {}
    void sortzip()
    {}
    Last edited by Chronotrigga; 05-30-2007 at 06:29 PM.

  2. #2
    Registered User
    Join Date
    May 2006
    Posts
    903
    I'm sorry, but you'll have to choose. Are you using C or are you using C++ ? printf(), gets(), cout and cin.. I mean.. Either use C or C++. Besides, you are using outdated header files (exactly all files ending with .h) as well as unportable ones (graphics.h, conio.h, dos.h). Never use any of those files.

    Here's what you should include:

    Code:
    #include <iostream>
    #include <fstream>
    #include <string> //Different than <string.h> and <cstring> (better)
    Not one more.

    Also, you want to use std::string over character arrays. Also, why do you have so many globals ? I've made programs tens of dozens of times larger and I needed 1 or 2 globals maximum... Also, you want to indent your code. S p a a a a a a a a c e i t o u t.

    For example, this:
    Code:
    void savedata()
    {   ofstream outfile;
     
    outfile.open("U:\\EXAMPLE.DAT",ios::out);
    if(outfile)
    { outfile<<num<<"\n";
      for(I=1; I<=num; I++)
     { outfile<<MyArr[i].namef<<"\n";
       outfile<<MyArr[i].namel<<"\n";
       outfile<<MyArr[i].city<<"\n";
       outfile<<MyArr[i].string<<"\n";
       outfile<<MyArr[i].state<<"\n";
       outfile<<MyArr[i].zipcode<<"\n";
       outfile<<MyArr[i].phone<<"\n";
     }
    }
    else
    {cout<<"what did u do wrong ";}
     
    outfile.close();
     
    }
    Should look like this:
    Code:
    void savedata()
    {
      ofstream outfile;
      outfile.open("U:\\EXAMPLE.DAT", ios::out);
    
      if(outfile)
      {
        outfile << num << "\n";
        for(I = 1; I <= num; I++)
        {
          outfile << MyArr[i].namef << "\n";
          outfile << MyArr[i].namel <<"\n";
          outfile << MyArr[i].city <<"\n";
          outfile << MyArr[i].string <<"\n";
          outfile << MyArr[i].state <<"\n";
          outfile << MyArr[i].zipcode <<"\n";
          outfile << MyArr[i].phone <<"\n";
        }
      }
      else
      {
        cout << "what did u do wrong ";
      }
      outfile.close();
    }
    Doesn't that feel better to you ? I'm not even talking about mistakes and things completely wrong. I suspect you are using a very old compiler.

    This:
    Code:
    for(I=1; I<=num; I++)
    Is absolutely illegal in standard C++. Using a variable without declaring it.
    Last edited by Desolation; 05-30-2007 at 06:28 PM.

  3. #3
    Registered User
    Join Date
    May 2007
    Posts
    11
    Num is an int up top :P. And, yes, I am using Borland 97 which is about a 10 year old compiler.. Don't ask why.

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    903
    Put that junk in the thrash. Either use Code::Blocks, the latest Dev-C++ or Visual Studio 2005 Express (free !). They are all thousands of times better than what you're using.

    Edit: I just saw you made I a global... That's some nasty stuff. Never ever do that again =)

  5. #5
    Registered User
    Join Date
    May 2007
    Posts
    11
    Yeah, I know . I am supposed to use Borland though, although I can use better compilers.

  6. #6
    Registered User
    Join Date
    May 2006
    Posts
    903
    Then use them ?!

  7. #7
    Registered User
    Join Date
    May 2007
    Posts
    11
    I am supposed to use Borland . I am guessing it is the compiler my teacher used, and he is not willing to change. I suggested that we get Visual C++; and he said he will look into it.

    Anyway yeah :P

  8. #8
    Registered User
    Join Date
    May 2006
    Posts
    903
    Kick his ass if he doesn't let you use it.

  9. #9
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Your teacher is a quack.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > for(I=1; I<=num; I++)
    Arrays start at 0, not 1
    So this must be
    for(I=0; I<num; I++)

    > char tempf[10], templ[10]
    But all of the members of your info struct are longer than 10 chars, so there's no way that some of the information you saved will be loadable. getline() will just stop when your small buffer is full.

    > int I, found,f,ptr;
    You also have a global int I as well.
    This is very poor style.

    You need to get to grips with passing parameters to functions. A mass of global variables, and a mass of functions without any parameters is a weakness.

    > gets (Sname);
    NEVER use gets(), see the FAQ.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

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