Click here to Skip to main content
15,031,162 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi I have file that have numbers and names.in my dialog there are editBoxes that received numbers and names.now I want to when button Add clicked if the information in editBoxes are new so add it at the end of the file else(number has already exist in file) find the line and replace the name.
I know how to add new information but for editing file I need help.

I write a code to search in file line by line and save each word in a room of array but don't no how replace it
if u can please help me to complete this cod(if it is true) else please help me in new way

thank u in advance :)

m_status is my CStdioFile and m_Number is the first editBox and m_Name is second editBox

    CString str6;
CString str7;
CString strToken;
CString x[2];
int length=0;
int nTokenPos = 0;
int xcounter=0;

m_status.Open("E:\\3.txt",CStdioFile::modeReadWrite); //open file
while(m_status.ReadString(str6))
{
    length++;   //get the number of line in file
}
m_status.SeekToBegin();

for(int i=0;i<length;i++)
{
    m_status.ReadString(str6);
    str7 = str6;
    strToken = str7.Tokenize(_T(" "),nTokenPos); //each word between spaces save in
                                                         //strToken
    while (!strToken.IsEmpty())
    {
         x[xcounter] = strToken;  //save each word in array
         xcounter++;
         strToken = str7.Tokenize(_T(" "), nTokenPos);
    }
    if (x[1]==m_Number)
    {
        x[2]=m_Name;
        //here I want if number in file was same as number in editBox1 so change
                    //the name in file to name in editbox2
    }
}
m_status.Close();

UpdateData(FALSE);
Posted
Updated 2-Jan-14 6:27am
v3

1 solution

You cannot do it the way your code is written, because, if the new data is not exactly the same size as the original you will end up with a corrupt file. You should read all the data into a collection, then modify the data in there. When all updates are complete you rewrite the entire file.
   
Comments
CPallini 2-Jan-14 14:11pm
   
5.
Richard MacCutchan 3-Jan-14 3:24am
   
:thumbsup:
Diba.S 2-Jan-14 15:35pm
   
thank you Mr.Richard MacCutchan

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900