Thread: Saving and Retrieving Disk File

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

Popular pages Recent additions subscribe to a feed