help: C++ Writing a file from Text Box data
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: help: C++ Writing a file from Text Box data

  1. #1
    Join Date
    Jul 2009
    Posts
    3

    Exclamation help: C++ Writing a file from Text Box data

    please help,
    Ive got as far on my own as i can and have hit a brick wall!


    I am making a member application where members details can be input by text boxes and stored to a file, the code below works until i add more than one member!

    The problem is when i add another member it just overwrites all the data currently in the file with the new member details and loses the old member details that used to exist in the file.

    I would like the new member data to be added below the existing member data in the file, here is the code inside my save button:




    //Start of btnSave

    private: System::Void btnSave_Click(System::Object^ sender, System::EventArgs^ e) {

    //btnSave Purpose: Open memberFile.dat and put in some data from text boxes

    //save record to file
    StreamWriter^ pwriter = gcnew StreamWriter("memberFile.dat");

    pwriter->WriteLine(Convert::ToString(tempMemberNo));
    pwriter->WriteLine(txtTitle->Text);
    pwriter->WriteLine(txtFirstName->Text);
    pwriter->WriteLine(txtLastName->Text);
    pwriter->WriteLine(txtContactNo->Text);
    pwriter->WriteLine(txtAddress->Text);
    pwriter->Close();

    lblError->Text = "Record Saved.";
    }
    }

    //end of btnSave





    When i move the "pwriter->Close();" code to the btnExit()
    and move the "StreamWriter^ pwriter = gcnew StreamWriter("memberFile.dat");" code inside the formload(), My application saves multiple records like i would like it to.


    But.. THE PROBLEM IS:
    I would like the program to do the following when the btnSave is clicked:
    Open the file.
    Write data to the file. <-- below the data that is already there (Not wipe the file and +new data)
    Close the file.





    I want my file to contain the 1st members details and then the 2nd members details below the 1st and 3rd below the second ect..

    like this:

    1
    mr
    bob
    marly
    01803222244
    17 madeup road, croydon, surrey.
    2
    miss
    danniella
    joevis
    01803444555
    74 chargrill road, madestone, kent.
    3
    ...
    .....
    ......
    ......
    4
    ....
    ...
    ...
    ...
    5
    ect..

  2. #2
    Join Date
    Jul 2002
    Posts
    2,524

    Re: help: C++ Writing a file from Text Box data

    StreamWriter^ pwriter = gcnew StreamWriter("memberFile.dat", true);

    See StreamWriter.StreamWriter(String, Boolean) Constructor:
    append
    Determines whether data is to be appended to the file. If the file exists and append is false, the file is overwritten. If the file exists and append is true, the data is appended to the file. Otherwise, a new file is created.

  3. #3
    Join Date
    Jul 2009
    Posts
    3

    Arrow Re: help: C++ Writing a file from Text Box data

    thanks AlexF,

    the code you gave me:
    "StreamWriter^ pwriter = gcnew StreamWriter("memberFile.dat", true);"
    appends the data to the end of the file which is exactly what i needed!

    my file now adds members sequencially and looks like this:

    1
    bob
    marly
    0183232323333
    12 chilcote close, croydon, surrey
    2 <--- line 6 in the memberFile.dat (start of member 2)
    danniella ..7
    jovis ..8
    075433322255 ..9
    72 chargrill road, kent <--- line 10 (end of member 2's details)
    3

    and i can append further records without any problem.


    The next problem i have now found is updating:
    Now i would like to be able to update record 2, i already have already set up a members array and memberList array which holds the members as objects when they are typed in. This allows me to search for member 2 and bring up (in this case "danniella's" details) and display them in the text boxes (btnFind Code shown below).

    I guess i could turn append to false and rewrite the whole file sequencially going through the array member by member and appending them to the file.. but the drawback is obviously that with this method the whole file will be re-written when only 1 record needs updating, this isnt a problem for small amounts of data, but when theres thousands of members this method (an update) could become time consuming.. there must be a better way..


    Im not sure if this is possible but i was thinking..
    possibly rewrite values at lines 6 to 12 in the text file (memberFile.dat) with the values brought up from the search for member "2" after clicking "btnFind" then "btnSave".

    **oh and i already have an "if", "else" function using the array to determine whether the record is already existing (an update) or a new record.





    here is the code from my btnFind incase anyone is interested:

    private: System::Void btnFindMemberByID_Click(System::Object^ sender, System::EventArgs^ e) {

    //MemberList* members;
    int findMemberNo = Convert::ToInt32(txtFindMemberNo->Text);

    //Display strings in text boxes
    txtMemberNo->Text = Convert::ToString(findMemberNo);
    txtTitle->Text = members->getMemberAt(findMemberNo)->getTitle();
    txtFirstName->Text = members->getMemberAt(findMemberNo)->getFirstName();
    txtLastName->Text = members->getMemberAt(findMemberNo)->getLastName();
    txtContactNo->Text = members->getMemberAt(findMemberNo)->getContactNo();
    txtAddress->Text = members->getMemberAt(findMemberNo)->getAddress();
    }
    Last edited by deanhavelock; July 26th, 2009 at 10:57 AM. Reason: (extra info!) oh and i already have an "if", "else" function using the array to determine whether the record is already exist

  4. #4
    Join Date
    Jul 2002
    Posts
    2,524

    Re: help: C++ Writing a file from Text Box data

    This is restriction of text files. Updating means rewriting the whole file. You may read all text file to a list of class instances, and edit them in memory. In the end, save the whole file.
    Or consider using some other format, like XML or database.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center