[SOLVED] Write to a file without the CR at the end of line



  • hi,
    i have a file that contains "text" , and every line in it ends with ONLY LF.
    i'm reading some parts of it and writing to a file using :
    @
    QTextStream tmp_in(old_file);
    QTextStream stream(new_file);
    QString tmp_line = tmp_in.readLine();
    stream << tmp_line << endl;
    @
    but the new written data contains CR,LF at end of each line .
    how can i write without the CR ??

    i'm using windows .

    By LF and CR i mean carriage return and line feed



  • try in this way:

    @QTextStream tmp_in(old_file);
    QTextStream stream(new_file);
    QString tmp_line = tmp_in.readLine();
    tmp_line.simplified();
    stream << tmp_line << endl;@



  • [quote author="nologinma" date="1409666663"]try in this way:

    @QTextStream tmp_in(old_file);
    QTextStream stream(new_file);
    QString tmp_line = tmp_in.readLine();
    tmp_line.simplified();
    stream << tmp_line << endl;@[/quote]

    It didn't work :\

    i tried QDataStream , i does the work writing only the LF but also adds NUL between every character of the file .



  • Hi,
    On Win machines you get the CR/LF all the time and it is good practice on Linux machines to do the same. Then text files are able to be transported from Win to Linux and back.
    Is there a reason you don't want the CR?



  • solved part of the problem it i just removed the
    [quote]
    | QIODevice::Text
    [/quote]
    from the open , now my open for writing is :
    @
    new_file->open(QIODevice::WriteOnly);@

    so my writing now is like this :
    @
    QFile* new_file= new QFile("XXXX");
    new_file->open(QIODevice::WriteOnly);

    QTextStream tmp_in(old_file);
    QTextStream tmp_out(new_file);
    
    QString tmp_line = tmp_in.readLine();
    while(tmp_line != ")"){
        tmp_out << tmp_line + "\n";
        tmp_line = tmp_in.readLine();
    }
    tmp_out << ")" << endl;
    

    @

    the only problem now is that the endl , writes "\n" in last line which i dont want , can i modify the endl so it doesn't do so ?

    [quote]
    Is there a reason you don’t want the CR?
    [/quote]
    i have to , i need to output my file according to a format .



  • Ok, then please put [SOLVED] as prefix to Post name.



  • [quote author="nologinma" date="1409730610"]Ok, then please put [SOLVED] as prefix to Post name.[/quote]

    sorry i thought i solved but then , i got the last line problem see my last post above .



  • Now solved instead of using endl;
    i just used flush .
    thank you all .


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.