Thread: How to insert data to the top of exist text file?

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    68

    How to insert data to the top of exist text file?

    I want to insert "{" to the top of exist text file and insert "}" to the bottom of text file

    Example:

    If in "1.txt" has this data in it.

    1234afd
    daf845
    dfdfa

    and I want to insert "{" to the top of text file and insert "}" to the bottom of text file. So I think it should be like this

    {
    1234afd
    daf845
    dfdfa
    }

    I write program to do that. But my program replace "{" to the first line data of "1.txt"

    So my program write this data in "1.txt"

    {
    daf845
    dfdfa
    }

    Below is my program.

    Code:
    CStdioFile OutputFile;
    CString outp_filename, m_string; 
    CFileException FileExc;
    BOOL bEnd;
    
    	outp_filename = "1.txt";
    
    if (!OutputFile.Open(outp_filename, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate, &FileExc)) 
    {
    FileExc.ReportError();
    return;
    }
    OutputFile.SeekToBegin();
    
    m_string = "{";
    
    OutputFile.WriteString(m_string+'\n'); 
    
    OutputFile.SeekToEnd();
    
    m_string = "}";
    
    OutputFile.WriteString(m_string+'\n');
    
    OutputFile.Close();
    
    }
    Do you know how to solve my problem?
    Thank you very much.

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You can append at the end of the file, but you can't do it first in a file. You could try to write "{\n" to a new file, then read the contents from the original find and append it to the new file. Then add "\n}" to the end of it.
    Finally, overwrite the old file with the new one.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    There are several solutions.

    One experient is to append data at the beginning and end of the file using write(). Make sure you open the file with the append flag (ios::app). Another solution is to map the file's contents into virtual memory using file-mapping. That will allow you to modify its contents at any given point.

    Kuphryn

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    170
    You can do it, but you have to move all the other data.

    Starting at the end of the file move the eof to new spot (oldpos + strlen(newdata)). Then do that with every character after your insert. Then you have blank space for your insert.

    It is not pretty but this is how I always do it. I have some code if you need it.
    Best Regards,

    Bonkey

  5. #5
    Registered User
    Join Date
    Aug 2005
    Posts
    1
    You can try the following method it works

    First read the first string and store it
    append it to the end of the string you want to insert at top
    Insert the combined string at top

    this new string will replace the old first string at the top of the file but anyway the inserted new string contains the old string in it
    Code:
    void CMyClass::AddTextAt TheTopOf TheFile(LPCTSTR szFile, LPCTSTR szText)
    {
    	if ((szText == NULL) || (szText == "")) return ;
    	CString str;
    
    
    	char s[81];
    	CFile file;   
      
    	if (!file.Open(szFile, CFile::modeRead)) return ;
    	file.SeekToBegin();
    	int bytesRead = file.Read(s, 80);
                    s[bytesRead] = 0;
                    CString message = s;
    	file.Close();
    
    	if (!file.Open(szFile, CFile::modeWrite)) return ;
    	file.SeekToBegin();
    	str.Format("%s\n%s\n",szText,message);
    	file.Write(str,str.GetLength());
    	file.Close();
    
    }
    Only problem is you have to open the file twice once in Read mode once in Write mode. hope this helps
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  4. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  5. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 11:47 AM