在每个特定字符串后插入下一行

时间:2018-03-09 04:27:02

标签: c++ string file

我在文本文件中有一长串字符串。 E.g:

asdasdas**Start**sadasdsadasdasd**End**45646546466516asdsadasd**Start**asdsfsdfsdfdsfdsfds**End**

如何在每个\n之前插入下一行(Start)?

std::ifstream in("example.txt");
    std::ofstream out("example2.txt");
    std::string line;

也许我可以将结果输出到example2.txt

3 个答案:

答案 0 :(得分:1)

我写了这段代码来解决你的问题

std::ifstream in("example.txt");
    std::ofstream out("example2.txt");
    string line;
    if (in)
    {

        int searchedindex = 0;
        in >> line;
        std::size_t found;
        while (true)
        {
            found = line.find("Start", searchedindex);
            if (found == std::string::npos)
            {
                break;
            }
            else
            {
                line.insert(line.begin() + found, '\n');
                searchedindex = found + 2;

            }
        }           
        in.close();
    }
    if (out)
    {
        out << line;
        out.close();
    }

答案 1 :(得分:0)

嗯,我以一种相当奇怪的方式做到了这一点。我认为最好先阅读整个文件然后处理整个字符串。我认为它比这短,这是相当冗长的。

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

const char* testString = "StartmumbojumboandfillerEnd  Start  fillerfillerfiller  End"
" StartrandomrandomrandomEndStartstuffstuffstuffEnd  Start more more more End";

int main() 
{
    ifstream infile("file.txt", ios::ate | ios::binary);
    if (!infile) return 0;
    size_t size = infile.tellg();
    infile.seekg(0);
    string fullFile(size, '\0');
    infile.read(&fullFile[0], size);

    fullFile = testString; // Use test string instead of file.

    size_t start = 0;
    size_t end = 0;
    string substr;
    int count = 0;

    do
    {
        start = fullFile.find("Start", end); // Will start reading only from first "Start"
        if (start != string::npos) // If Start found
        {
            start += strlen("Start"); // Skip "Start"
            end = fullFile.find("End", start); // Find "End"
            if (end == string::npos) throw("Failed to find end token");
            substr = fullFile.substr(start, end - start);
            substr += '\n';
            start = end + strlen("End"); // Move "start" pos past "End" to continue


            count++;
            ofstream outFile("file" + to_string(count) + ".txt", ios::binary);
            outFile.write(substr.data(), substr.size());
            cout << substr << '\n';
        }
    } while (start != string::npos);

    cout << count << "Files written\n";
    cin.ignore();

    return 0;
}

如果有更短或更好的方式,请告诉我。

答案 2 :(得分:0)

我认为这是解决问题的更好方法

#include <iostream>
#include <fstream>
#include <cstring>
#include <algorithm>
#include <array>
#include <cstring>


int main(int argc, char* argv[])
{
    const char *pTarget = "Start";
    const size_t targetLen = std::strlen("Start");
    const size_t maxReserveNum = targetLen - 1;

    std::array<char, 64 * 1024> readBuffer;
    std::ifstream inFile("F:\\example.txt");
    std::ofstream outFile("F:\\example2.txt");

    size_t reserveNum = 0;
    while (inFile.read(readBuffer.data() + reserveNum, readBuffer.size() - reserveNum), inFile.gcount() != 0)
    {
        const char *nextWritePos = readBuffer.data();
        const char *dataEndPos = nextWritePos + static_cast<size_t>(inFile.gcount()) + reserveNum;

        for (const char *targetPos = std::strstr(nextWritePos, pTarget);
            targetPos != nullptr;
            targetPos = std::strstr(nextWritePos, pTarget))
        {
            outFile.write(nextWritePos, targetPos - nextWritePos);
            outFile.write("\n", 1);
            outFile.write(pTarget, targetLen);
            nextWritePos = targetPos + targetLen;
        }

        const size_t remainderSize = dataEndPos - nextWritePos;
        reserveNum = std::min<size_t>(remainderSize, maxReserveNum);
        outFile.write(nextWritePos, remainderSize - reserveNum);

        std::memcpy(readBuffer.data(), dataEndPos - reserveNum, reserveNum);
    }

    outFile.write(readBuffer.data(), reserveNum);

    return 0;
}