简体   繁体   English

如何将文件中的字符转换为字符串

[英]How to convert a character in a file to a string

The problem is that I don't know how to properly change the character in the file to a string.问题是我不知道如何正确地将文件中的字符更改为字符串。

I tried this but...我试过这个但是......

    char c = '"';
    fstream test_file("l.txt");
    while (test_file >> c) {
        if (c == '"') {
            test_file << " \" ";
        }
    }
    system("pause");

It doesn't work properly.它不能正常工作。 The example is:例子是:

...and then he said "I'm older than you!" end of quote.

The expected result for me is:我的预期结果是:

...and then he said  " I'm older than you! "  end of quote.

The actual result of the above code is:上面代码的实际结果是:

...and then he said " "  older than you!" " d of quote.

It seems to skip 2 characters and then replace it, but not only 1 character but also next to it.好像跳过了2个字符然后替换了它,但不仅是1个字符而且还有它旁边的。

Edit.编辑。 Ok, I got a better result now with:好的,我现在有了更好的结果:

    string str;
    string temp;
    string quote;
    int count = 0;
    fstream test_file;
    test_file.open("l.txt");
    while (test_file >> str) {
        count++;
        quote = f_Erase_from(1, str);
        if (quote == "\"") {
            temp = " \" " + f_Erase_from_to(0, 1, str);
            test_file.close();
            test_file.open("l.txt");
            for (int i = 0; i < count - 1 ; i++) {
                test_file >> str;
            }
            test_file << temp + " ";
        }
        quote = f_Erase_from_to(0, str.size() - 1, str);
        if (quote == "\"") {
            temp = f_Erase_from_to(str.size() - 1, str.size(), str) + " \" ";
            test_file.close();
            test_file.open("l.txt");
            for (int i = 0; i < count ; i++) {
                test_file >> str;
            }
            test_file << " " + temp;
        }
    }
    system("pause");

With the result:结果:

...and then he said " I'm lder than you! " nd of quote.

But now it's eat the first character of the next string...但现在它吃下一个字符串的第一个字符......

Consider how files are stored on disk.考虑文件是如何存储在磁盘上的。 A file is a series of bytes, one after another.文件是一系列字节,一个接一个。 If you wanted to insert a character, you'd have to move all following characters by 1 byte (let's consider ASCII for simplicity).如果您想插入一个字符,则必须将所有后续字符移动 1 个字节(为简单起见,让我们考虑 ASCII)。

Now, neither your hard disk nor the operating system will do that for you.现在,您的硬盘和操作系统都不会为您做这些。 Why?为什么? Given a file with 1 MB of size and you need to insert a backslash every 100 bytes, it would move 999.9 kB, then 999.8 kB, ... then 200 bytes, then 100 bytes.给定一个大小为 1 MB 的文件,您需要每 100 个字节插入一个反斜杠,它将移动 999.9 kB,然后是 999.8 kB,...然后是 200 个字节,然后是 100 个字节。 In total, that's 4.998 GB of data being moved, just because you wanted to insert 10000 bytes.总共移动了 4.998 GB 的数据,只是因为您想插入 10000 个字节。 This has such a performance impact that it isn't done.这具有如此大的性能影响,因此尚未完成。

Then, how does one replace characters in a file?那么,如何替换文件中的字符呢?

  1. Read the whole file into memory, do the replacement, write the whole thing to the file.将整个文件读入内存,进行替换,将整个文件写入文件。 This approach is ok for smaller files, maybe up to 50% of the RAM of the machine.这种方法适用于较小的文件,可能高达机器 RAM 的 50%。

  2. For larger files, use streams and read in chunks.对于较大的文件,使用流并分块读取。 Read the first n bytes from disk, do the replacement and write it into a temporary file.从磁盘读取前 n 个字节,进行替换并将其写入临时文件。 Loop this until you reach the end of the file.循环这个直到你到达文件的末尾。 Then delete the original and move the temporary to the original location.然后删除原来的,把临时的移到原来的位置。 Note that you need double + something space on disk.请注意,您需要 double + 一些磁盘空间。

How do you replace in a string?你如何替换字符串? Well, unfortunately, there isn't simply a s.replace(string find, string replacement) method like in every other language I know.好吧,不幸的是,并没有像我所知道的所有其他语言那样简单的s.replace(string find, string replacement)方法。 But it has been solved .但是已经解决了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM