File Content replacement-depressing results

Source: Internet
Author: User

Today, I want to write a small program that replaces a row of files in batches,

The initial idea is to operate directly in the file (not generate new files), so write the following code (operate on a single file)

  1. /*
  2. String m_strrepsrc; // The identifier string in the replaced line vc98/include/
    String m_strrepdes; // string F:/program files/software/Microsoft Visual Studio 8/VC/include/
  3. Required results:
  4. "E:/program files/Microsoft Visual Studio/vc98/include/basetsd. H "/
  5. Change to ==>
  6. "F:/program files/software/Microsoft Visual Studio 8/VC/include/basetsd. H "/
  7. The basetsd. H "/requirements remain unchanged.
  8. */
  9. Bool creplace: Deal ()
  10. {
  11. // Open the file
  12. Cstdiofile hfile (m_strfilepath.c_str (), cfile: modereadwrite | cfile: sharedenywrite );
  13. // Search
  14. Cstring strtemp;
  15. Cstring newstr;
  16. Long iidx = 0;
  17. While (hfile. readstring (strtemp ))
  18. {
  19. Iidx = strtemp. Find (m_strrepsrc.c_str (); // If the row contains an identifier string, replace
  20. If (iidx! =-1)
  21. {
  22. // Obtain the replacement part;
  23. NewStr = strTemp. Right (strTemp. GetLength ()-iIdx-1-m_strRepSrc.length (); // basetsd. h "/
  24. // String to be replaced
  25. NewStr = CString (_ T ("/" ") + CString (m_strRepDes.c_str () + newStr + CString (_ T ("/n "));
  26. // Write back the file
  27. HFile. Seek (-2-strTemp.GetLength (), CFile: current); // locate the start position of the string to be replaced
  28. HFile. WriteString (newStr. GetBuffer ());
  29. NewStr. ReleaseBuffer ();
  30. }
  31. }
  32. HFile. Close ();
  33. Return true;
  34. }

Result 1 is not satisfactory because

"F:/Program Files/software/Microsoft Visual Studio 8/VC/include/basetsd. h"/length ratio

"E:/program files/microsoft visual studio/vc98/include/basetsd. h"/long

As a result, the content following "e:/program files/microsoft visual studio/vc98/include/basetsd. h"/in the file is overwritten.

Since

HFile. Seek (-2-strTemp.GetLength (), CFile: current );

HFile. WriteString (newStr. GetBuffer ());

Cause: hFile. ReadString is read back to the previous content, leading to an endless loop.

And the target file is increased several times (the previous content is written into)... Khan ====> I don't understand why the previous content is written into the file...

 

Conclusion: The content of a file cannot be directly replaced by a common method, unless it is the same as the length of the string to be replaced.

The same length can be achieved as follows

  1. LPos = hFile. GetPosition ();
  2. HFile. Seek (-2-strTemp.GetLength (), CFile: current );
  3. HFile. WriteString (newStr. GetBuffer ());
  4. Newstr. releasebuffer ();
  5. Hfile. Seek (LPOS, cfile: Begin );

Finally, in another way, read the file content into the memory, replace the file content in the memory, and write it back to the file...

The implementation is as follows:

  1. // Replace all strsrc in strbig with strdst
  2. Void creplace: string_replace (string & strbig, const string & strsrc, const string & strdst)
  3. {
  4. String: size_type Pos = 0;
  5. String: size_type srclen = strsrc. Size ();
  6. String: size_type dstlen = strdst. Size ();
  7. While (Pos = strbig. Find (strsrc, POS ))! = String: NPOs)
  8. {
  9. StrBig. replace (pos, srclen, strdst); // replace the srclen characters starting from the index pos with the strdst
  10. Pos + = dstlen;
  11. }
  12. }
  13. Bool CReplace: deal ()
  14. {
  15. Ifstream in (m_strFilePath.c_str ());
  16. Stringstream ss;
  17. Ss <in. rdbuf ();
  18. String s = ss. str ();
  19. // Size_type is of the unsigned type, which depends on the specific machine.
  20. String: size_type iIdx = 0;
  21. String: size_type iBegin, iEnd;
  22. IIdx = s. find (m_strRepSrc );
  23. If (iIdx! = String: npos)
  24. {
  25. IBegin = s. rfind ("/" ", iIdx );
  26. IEnd = iIdx + m_strRepSrc.size ();
  27. String sRep = s. substr (iBegin, iEnd-iBegin );
  28. // S. replace (sRep, m_strRepDes );
  29. String_replace (s, sRep, m_strRepDes );
  30. // Write back
  31. Ofstream out (m_strFilePath.c_str ());
  32. Out <s;
  33. // Out. close () is not required, and the out structure of the scope end will automatically close ()
  34. }
  35. In. close ();
  36. Return true;
  37. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.