Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Reading File(C++)

Options
  • 25-03-2003 1: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 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