Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Reading from File(C++)

clarabellclarabell Posts: 5Member


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 %s %s %s %s
", &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

  • GabeGabe Posts: 61Member
    Hi!

    Look at the while loop you have made for a moment. It reads one line from the file and stores the data into the local variables (first_name, last_name, etc.) After that you pass the data to the appropriate text boxes. It's ok, but the loop continues reads the next line and [b]overwrites the existing data in your text box[/b]. After it readed the last line from the file it stops and your text boxes will contain the [b]last[/b] record in your file.
    There isn't any problem with the loop. Just keep in mind that if you use scanf or fscanf and you read strings with them your strings can't contain spaces because scanf thinks space as a separator.

    Gabe
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : Hi!
    :
    : Look at the while loop you have made for a moment. It reads one line from the file and stores the data into the local variables (first_name, last_name, etc.) After that you pass the data to the appropriate text boxes. It's ok, but the loop continues reads the next line and [b]overwrites the existing data in your text box[/b]. After it readed the last line from the file it stops and your text boxes will contain the [b]last[/b] record in your file.
    : There isn't any problem with the loop. Just keep in mind that if you use scanf or fscanf and you read strings with them your strings can't contain spaces because scanf thinks space as a separator.
    :
    : Gabe
    :
    [blue]If we talking about the Windows edit boxes then it is not a problem. At the moment of '=' edit box gets its own copy of the text, so what you will do with a pointer does not matter. It does not hold that pointer as a ref. to a text. The problem may be in the code to access the database. The lines of text are not the same length or something else. Also, maybe you will be better reading the lines with [italic]fgets()[/italic] and parsing them with [italic]strtok()[/italic]?[/blue]
Sign In or Register to comment.