Advertisement
Help Keep Boards Alive. Support us by going ad free today. See here: https://subscriptions.boards.ie/.
If we do not hit our goal we will be forced to close the site.

Current status: https://keepboardsalive.com/

Annual subs are best for most impact. If you are still undecided on going Ad Free - you can also donate using the Paypal Donate option. All contribution helps. Thank you.
https://www.boards.ie/group/1878-subscribers-forum

Private Group for paid up members of Boards.ie. Join the club.

Reading File(C++)

  • 25-03-2003 01:35PM
    #1
    Closed Accounts Posts: 6


    Hey all,
    I have created a database of information which is saved in a text file using tabs to seperate the fields. When I try to retrieve data and place it in text boxes for the user to view the only record that seems to be displayed is the last one each time I click the button, I suspect that there is a problem with the loop, using EOF,

    char first_name[30];
    char last_name[30];
    char email_address[60];
    char address1[50];
    char address2[50];
    FILE *in;


    in = fopen("MailData.txt", "r");

    while (fscanf(in, "%s\t%s\t%s\t%s\t%s\n", &first_name, &last_name,
    &email_address, &address1, &address2) != EOF){

    Form4->Edit1->Text = last_name;
    Form4->Edit2->Text = first_name;
    Form4->Edit3->Text = address1;
    Form4->Edit4->Text = address2;
    Form4->Edit5->Text = email_address;
    }


    fclose(in);

    It has been suggested to me to use ftell that monitors the cursor but as I am a beginner I have no idea how to use this, if anyone can point out where I'm going wrong I'd appreciate the help
    clarabell


Comments

  • Registered Users, Registered Users 2 Posts: 2,281 ✭✭✭DeadBankClerk


    [php]

    class Person
    {
    public:
    char first_name[30];
    char last_name[30];
    char email_address[60];
    char address1[50];
    char address2[50];

    public:
    friend istream& operator >> (istream& stream, Person& person);
    };

    istream& operator >> (istream& stream, Person& person)
    {
    return stream >> first_name >> last_name >> email_address >> address1 >> address2;
    }

    ifstream file; // declare an input file stream
    Person p;
    file.open("MailData.txt", ios::in); // open it
    while (file.good())
    {
    file >> p;

    // Fill the form or whatever

    }
    file.close();

    [/php]


Advertisement