Thread: C++ file handling

  1. #1
    unregistered
    Guest

    C++ file handling

    i have written a program which reads from and writes to a text file.
    the info read from the text file is stored as an array of structures.
    structures are as follows:
    { char surname[20];
    char firstname[20];
    char telephone[12];
    int custid;
    }
    I have succesfully coded the program which calls functions to add a customers details, sort customers by surname and output the customers details to screen.
    i am having real problems with deleting a customers record using the custid.
    any help would be greatly appreciated.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    94
    It is impossible to tell what you are doing wrong if you dont put up your code. So if you show the code you have for your delete fnct along with any err msg's you are receiving we may be able to help.

    Sophie

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    3

    code as requested

    as requested, the code for the function.
    I'm not getting errors, compile's fine, but does not delete correct record.
    it's probably my logic - i'm prompting user for cust id to delete,
    then reading in all records using while !.eof, and if the custid read in is not equal to cust id entered by user that element is copied to an array of structures.
    array of structures is then is then output to text file.
    sometimes all the files are deleted and sometimes none, or the wrong one is deleted.
    any help would be greatly appreciated.

    code is attached.

  4. #4
    Registered User
    Join Date
    May 2002
    Posts
    3

    code again

    didn't work too well as an attachment,
    i'll post below:void delcust()
    {
    int x=0;
    int i=0;
    int y=0;
    int currentid;
    cout<<"Please enter the Customer ID you wish to delete: ";
    cin>>currentid;
    cin.get();
    ifstream infile ("a:\\customerfile.txt",ios::in);
    if (!infile)
    {
    cout<<"cannot open file";
    }
    x=0;
    while (!infile.eof())
    {

    infile.getline(c_temp[y].surname,20,',');
    infile.getline(c_temp[y].firstname,20,',');
    infile.getline(c_temp[y].telephone,12,',');
    infile >> c_temp[y].custid;
    infile.get();
    if (currentid != c_temp[y].custid)
    {
    infile.getline(c_file[x].surname,20,',');
    infile.getline(c_file[x].firstname,20,',');
    infile.getline(c_file[x].telephone,12,',');
    infile >> c_file[x].custid;
    infile.get();
    x++;
    }
    y++;
    }
    infile.close();

    ofstream outfile("a:\\customerfile.txt",ios::out);
    if (!outfile)
    {
    cout <<"cannot open file";
    }
    for (i=0;i<x;i++)
    {
    outfile << c_file[i].surname << "," << c_file[i].firstname << "," <<
    c_file[i].telephone << "," << c_file[i].custid << endl;
    }
    outfile.close();
    }

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    void delcust()
    {
    int x=0; 
    int i=0; 
    int currentid;
    cout<<"Please enter the Customer ID you wish to delete: "; 
    cin>>currentid; 
    cin.get(); 
    ifstream infile ("a:\\customerfile.txt",ios::in);
    if (!infile) 
    { 
       cout<<"cannot open file";
       return; 
    } 
    x=0; 
    while (!infile.eof()) 
    { 
    
       infile.getline(c_file[x].surname,20,','); 
       infile.getline(c_file[x].firstname,20,','); 
       infile.getline(c_file[x].telephone,12,','); 
       infile >> c_file[x].custid;
       infile.get();
       if ((strcmp(c_file[x].surname,"")!= 0) && currentid != c_file[x].custid)
          x++;
    } 
    infile.close(); 
    
    ofstream outfile("a:\\customerfile.txt",ios::out);
    if (!outfile) 
    { 
       cout <<"cannot open file";
       return;
    } 
    for (i=0;i<x;i++) 
    { 
       outfile << c_file[i].surname << "," << c_file[i].firstname << "," <<
       c_file[i].telephone << "," << c_file[i].custid << endl; 
    }
    outfile.close(); 
    }

  6. #6
    Registered User
    Join Date
    May 2002
    Posts
    3

    Smile Cheers

    Thanks very much, I'll have a go now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating File Handling Functions
    By td4nos in forum C Programming
    Replies: 6
    Last Post: 06-26-2009, 11:43 AM
  2. basic file handling problem
    By georgen1 in forum C Programming
    Replies: 4
    Last Post: 03-05-2009, 06:21 AM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM
  5. Need a suggestion on a school project..
    By Screwz Luse in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:58 AM