简体   繁体   中英

Append text to file with new line c++

I want to append a text from file. I already done to append a text to a new file but the text can not enter the new line. This is my code :

ofstream data_ip_list;
data_ip_list.open("data_ip_list.txt", std::ios_base::app);

    ifstream fileinput("ip_a.txt");
    if(fileinput.is_open()){
        for(i = 0; i < count; ++i)
            {
            fileinput >> str1[i];
            data_ip_list << str1[i] ;

the result :

192.168.0.10010.10.10.2

how to make this to :

192.168.0.100
10.10.10.2

sorry for my bad english

Simply use a newline character '\\n' .

ofstream data_ip_list;
data_ip_list.open("data_ip_list.txt", std::ios_base::app);

ifstream fileinput("ip_a.txt");
if(fileinput.is_open()){
    for(i = 0; i < count; ++i)
    {
        fileinput >> str1[i];
        data_ip_list << str1[i] << '\n';

You can use either std::endl or '\\n' to add the newline

data_ip_list << std::endl;
// or
data_ip_list << '\n';

But std::endl flush the output buffer, which may be a little slower than '\\n' .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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