简体   繁体   English

C++ Pipe 分隔文本文件以逗号分隔

[英]C++ Pipe delimited text file to comma delimited

How can you convert a pipe delimited text file into comma delimited?如何将 pipe 分隔的文本文件转换为逗号分隔的文件? The pipe text file needs to be converted into comma delimited and stored in an output file. pipe 文本文件需要转换为逗号分隔并存储在 output 文件中。

For example if I had a pipe.txt file like this:例如,如果我有一个像这样的 pipe.txt 文件:

Available tickets|Seat numbers|Ticket Prices|Total Balance|Tax increase|Payment Method|Ticket Fees|Seats        
1234567|5778|Jonas, Jack|1652|5,873.90|Balance|230|3,567.98|Free soda refills       
1876543|2468|Johnson,George|9059|2,187.53|deposit|904|2,457.90|Price for street parking               
130240|3490|Angel, Mike|4237|1,045.50|Balance|560|3,509.87| Ending Balance

In order to add quotes only where needed, you can store each section between pipes in a temporary string.为了只在需要的地方添加引号,您可以将管道之间的每个部分存储在一个临时字符串中。

#include <iostream>
#include <fstream>
using namespace std;

int main() {
    ifstream input;
    input.open("theFile");

    ofstream output;
    input.open("otherFile");

    string temp;
    while (getline(input, temp, '|')) {
        if (temp.find(",") != string::npos)
            output << '"' << temp << "\",";
        else
            output << temp << ',';
    }
}

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

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