0

Let's say I have some .csv file as follows:

1,2,5,5,
0,5,6,9,
3,2,5,7,
1,2,3,6,

How can I delete ',' sign at the end of each line?

p.s. For example I know how to clean a space at the end of the line - file.readLine().trimmed();, but how to do the same for other signs, I have no idea.

4

4 回答 4

1

假设您已经知道如何逐行阅读,这种粗略的方法有效:

QString testStr = QString("1,2,5,5,");

QStringList testList = testStr.split(",");
qDebug() << "testList" << testList;

testList.removeLast();
qDebug() << "testList" << testList;

testStr = testList.join(",");
qDebug() << "testStr" << testStr;

// 输出

testList ("1", "2", "5", "5", "") 
testList ("1", "2", "5", "5") 
testStr "1,2,5,5" 
于 2013-09-05T00:53:06.300 回答
1

这对我有用:

#include <QCoreApplication>
#include <QFile>
#include <QIODevice>
#include <QTextStream>

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QFile inFile("input.csv");
    if(!inFile.open(QIODevice::ReadOnly))
        exit(1);

    QString newFileData;
    QTextStream in(&inFile);

    //Read file line by line until it reaches the end
    while(!in.atEnd())
    {
        QString line = in.readLine();
        newFileData.append(line);

        int positionOfLastChar = newFileData.length()-1;
        char lastChar = newFileData.at(positionOfLastChar).toLatin1(); //Returns the character bevore the last character
        if(lastChar == ',')
            newFileData.remove(positionOfLastChar, //Remove at the position of the previous character 
                1                                  //one character
                );                      
        newFileData.append('\n');                  //Append the newline again, because in.readLine() ignored it
    }

    inFile.close();

    QFile outFile("output.csv");
    if(!outFile.open(QIODevice::WriteOnly | QIODevice::Truncate))
        exit(1);

    //Write the data to the output file
    QTextStream out(&outFile);                    
    out << newFileData;
    outFile.close();

    return a.exec();
}

我希望这有帮助。

于 2013-09-04T15:49:58.857 回答
0

这是另一种方式,无需将整个文件存储在内存中。它还显示了控制台应用程序如何处理错误。

#include <QCoreApplication>
#include <QStringList>
#include <QFile>
#include <QTextStream>
#include <cstdio>

QTextStream err(stderr);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    if (a.arguments().length() != 3) {
        err << "Usage: " << a.arguments()[0] << " infile outfile" << endl;
        return 1;
    }
    QFile fin(a.arguments()[1]), fout(a.arguments()[2]);
    if (! fin.open(QIODevice::ReadOnly)) {
        err << "Can't open input file: " << fin.fileName() << endl;
        return 2;
    }
    if (! fout.open(QIODevice::WriteOnly)) {
        err << "Can't open output file: " << fout.fileName() << endl;
        return 3;
    }
    QTextStream in(&fin), out(&fout);
    while (!in.atEnd()) {
        QString line = in.readLine().trimmed();
        if (line.endsWith(",")) line.truncate(line.length()-1);
        out << line << "\n";
        if (in.status() != QTextStream::Ok) {
            err << "Error while reading." << endl;
            return 4;
        }
        if (out.status() != QTextStream::Ok) {
            err << "Error while writing." << endl;
            return 5;
        }
    }
    return 0;
}
于 2013-09-04T18:05:56.440 回答
0

你也可以用另一种方式制作。您可以阅读此文本line by linepush.back() to vector. 然后你可以循环调用你的所有向量,你可以删除last items of vectors. 然后,如果您愿意,可以将矢量写入 txt 文件。之后,您的“,”将被删除。

于 2013-09-04T16:02:41.180 回答