| |

VerySource

 Forgot password?
 Register
Search
View: 5689|Reply: 10

Strange question: I have a text file, there are many records in it, I want to replace a garbled with a blank code.

[Copy link]

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2021-4-22 18:30:02
| Show all posts |Read mode
I want to use TStringList * txtList = new tstringlist;
STRING SS;
   
      Tstringlist * txtlist = new tstringlist;
   
      TXTLIST-> LoadFormfile ("C:\1uO1Mytxt.txt");
      For (int i = 0; i <txtlist-> count; i ++)
            {
                   SS = txtList-> strings [i];
                   IF (SS.POS ("string")> 0)
                   SS = STRINGREPLACE (SS, "string", "", -----------);
                   TXTLIST-> STRINGS [I] = SS;
          }
      TXTLIST-> Savetofile ("C:\1uO1mytxt.txt");
   
      Delete txtList;
The question now is:
Mainly in this way, only part of the data is only in the first line.

I don't know what else can I realize?

Read with C, but how to replace it?

Many thanks!
Reply

Use magic Report

0

Threads

27

Posts

24.00

Credits

Newbie

Rank: 1

Credits
24.00

 China

Post time: 2021-4-22 21:00:01
| Show all posts
Changed, good.
Any "string" changes into spaces to store new name mytxt1.txt

Void __fastcall tform1 :: button1click (Tobject * Sender)
{
  STRING SS;
  Tstringlist * txtlist = new tstringlist;
  TXTLIST-> LoadFromfile ("mytxt.txt");
  For (int i = 0; i <txtlist-> count; i ++)
  {
    SS = txtList-> strings [i];
    Treplaceflags flags;
    Flags << RFREPLACEALL;
    IF (SS.POS ("string")> 0) SS = StringReplace (SS, "string", "" ", flags);
    TXTLIST-> STRINGS [I] = SS;
  }
  TXTLIST-> Savetofile ("Mytxt1.txt");
  Delete txtList;
}
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2021-4-22 21:30:02
| Show all posts
TstringList's LoadFromFile generally only handles text, if there is a text end of the text (0x1a) or string end value (0x00) in your file, it has a problem.
C / C ++ file processing capability is very, the simplest, with the C standard function to achieve your requirements
1. Use rename to rename mytxt.txt, such as Mytxt.bak
2. Open file mytxt.bak with fopen, and create a new file mytxt.txt
3. Read the file mytxt.bak by getC, and determine if it is a mess, if yes, change to space to write mytxt.txt, otherwise write file mytxt.txt
4. Repeat 3 until myTXT.BAK file end
5. If necessary, delete the file mytxt.bak
Reply

Use magic Report

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2021-4-22 22:00:01
| Show all posts
Is the above problem, getc is a character, I am now two characters, forming a garbled Chinese character.

Use the code of C to an example?
Reply

Use magic Report

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2021-4-22 22:30:01
| Show all posts
This Chinese character is transferred to the capital 16. is: 9D7C
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2021-4-23 09:00:01
| Show all posts
File * fp = fopen ("D:\\Temp\\t.txt", "RB");
   File * fp1 = fopen ("D:\\Temp\\t1.txt", "WB");
   INT C, C1;
   While ((c = fgetc (fp))! = EOF)
      IF (c! = 0x9d)
         FPUTC (C, FP1);
         Else
         {
             C1 = FGETC (FP);
             IF (C1! = 0x7c)
               FPUTC (C, FP1);
             Else
             {
               FPUTC ('', fp1); // instead of 0x9D7C in two spaces
               FPUTC ('', fp1);
             }
         }
   Fclose (fp);
   Fclose (fp1);
}
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2021-4-23 09:15:01
| Show all posts
Just finished, I found a small bug,
             IF (C1! = 0x7c)
               FPUTC (C1, FP1); // should be C1 here
Reply

Use magic Report

1

Threads

4

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2021-4-23 09:45:01
| Show all posts
Thank you!

I feel there is still a problem.

If the previous character is the second half byte is 0x9d, the latter character is 0x7c, so that, then it is replaced.
Reply

Use magic Report

0

Threads

8

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2021-4-23 10:00:01
| Show all posts
This is annoying. If your file is unicode or a pure Chinese character (double byte), it is also good to process (change FGETWC, read two bytes at a time). Otherwise, this secondary sense is difficult to handle (including Ultra-edit commercial software sometimes has this problem)
Reply

Use magic Report

0

Threads

3

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2021-4-23 10:15:02
| Show all posts
Go to WideString (Unicode) and then processed. After the processing is completed.

But if you have some special words can't be converted to Unicode, this conversion will lose that word.
100% is correct, we must look forward to the unity of Chinese standards and software.

Do you want you twice, Unicode mode once, C mode once. In contrast, smart selection one, do not have wrong efforts.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list