如何将文本追加到c++文本文件中?

时间:2022-11-05 13:57:24

How to append text to a text file in C++? And create new if does not exist and append if it does exist.

如何在c++中附加文本到文本文件?如果不存在,则创建新的if,如果它确实存在,则追加。

5 个解决方案

#1


192  

#include <fstream>

int main() {  
  std::ofstream outfile;

  outfile.open("test.txt", std::ios_base::app);
  outfile << "Data"; 
  return 0;
}

#2


8  

 #include <fstream>
 #include <iostream>

 FILE * pFileTXT;
 int counter

int main()
{
 pFileTXT = fopen ("aTextFile.txt","a");// use "a" for append, "w" to overwrite, previous content will be deleted

 for(counter=0;counter<9;counter++)
 fprintf (pFileTXT, "%c", characterarray[counter] );// character array to file

 fprintf(pFileTXT,"\n");// newline

 for(counter=0;counter<9;counter++)
 fprintf (pFileTXT, "%d", digitarray[counter] );    // numerical to file

 fprintf(pFileTXT,"A Sentence");                   // String to file

 fprintf (pFileXML,"%.2x",character);              // Printing hex value, 0x31 if character= 1

 fclose (pFileTXT); // must close after opening

 return 0;

}

#3


6  

I use this code. It makes sure that file gets created if it doesn't exist and also adds bit of error checks.

我用这段代码。它确保在文件不存在时创建该文件,并添加一些错误检查。

static void appendLineToFile(string filepath, string line)
{
    std::ofstream file;
    //can't enable exception now because of gcc bug that raises ios_base::failure with useless message
    //file.exceptions(file.exceptions() | std::ios::failbit);
    file.open(filepath, std::ios::out | std::ios::app);
    if (file.fail())
        throw std::ios_base::failure(std::strerror(errno));

    //make sure write fails with exception if something is wrong
    file.exceptions(file.exceptions() | std::ios::failbit | std::ifstream::badbit);

    file << line << std::endl;
}

#4


0  

You could use an fstream and open it with the std::ios::app flag. Have a look at the code below and it should clear your head.

你可以使用一个fstream和std::ios::app flag。看看下面的代码,你就会明白了。

... fstream f("filename.ext", f.out | f.app); f << "any"; f << "text"; f << "written"; f << "wll"; f << "be append"; ...

…fstream f(“文件名。ext”,f。出| f.app);f < <“任何”;f < <“文本”;f < <“写”;f < <“将”;f < <“追加”;…

You can find more information about the open modes here and about fstreams here.

您可以在这里找到更多关于开放模式和fstreams的信息。

#5


0  

I got my code for the answer from a book called "C++ Programming In Easy Steps". The could below should work.

我从一本名为《简单步骤中的c++编程》(c++ Programming In Easy Steps)的书中获得了答案的代码。下面的罐子应该可以工作。

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

using namespace std;

int main()
{
    ofstream writer("filename.file-extension" , ios::app);

    if (!writer)
    {
        cout << "Error Opening File" << endl;
        return -1;
    }

    string info = "insert text here";
    writer.append(info);

    writer << info << endl;
    writer.close;
    return 0;   
} 

I hope this helps you.

我希望这对你有帮助。

#1


192  

#include <fstream>

int main() {  
  std::ofstream outfile;

  outfile.open("test.txt", std::ios_base::app);
  outfile << "Data"; 
  return 0;
}

#2


8  

 #include <fstream>
 #include <iostream>

 FILE * pFileTXT;
 int counter

int main()
{
 pFileTXT = fopen ("aTextFile.txt","a");// use "a" for append, "w" to overwrite, previous content will be deleted

 for(counter=0;counter<9;counter++)
 fprintf (pFileTXT, "%c", characterarray[counter] );// character array to file

 fprintf(pFileTXT,"\n");// newline

 for(counter=0;counter<9;counter++)
 fprintf (pFileTXT, "%d", digitarray[counter] );    // numerical to file

 fprintf(pFileTXT,"A Sentence");                   // String to file

 fprintf (pFileXML,"%.2x",character);              // Printing hex value, 0x31 if character= 1

 fclose (pFileTXT); // must close after opening

 return 0;

}

#3


6  

I use this code. It makes sure that file gets created if it doesn't exist and also adds bit of error checks.

我用这段代码。它确保在文件不存在时创建该文件,并添加一些错误检查。

static void appendLineToFile(string filepath, string line)
{
    std::ofstream file;
    //can't enable exception now because of gcc bug that raises ios_base::failure with useless message
    //file.exceptions(file.exceptions() | std::ios::failbit);
    file.open(filepath, std::ios::out | std::ios::app);
    if (file.fail())
        throw std::ios_base::failure(std::strerror(errno));

    //make sure write fails with exception if something is wrong
    file.exceptions(file.exceptions() | std::ios::failbit | std::ifstream::badbit);

    file << line << std::endl;
}

#4


0  

You could use an fstream and open it with the std::ios::app flag. Have a look at the code below and it should clear your head.

你可以使用一个fstream和std::ios::app flag。看看下面的代码,你就会明白了。

... fstream f("filename.ext", f.out | f.app); f << "any"; f << "text"; f << "written"; f << "wll"; f << "be append"; ...

…fstream f(“文件名。ext”,f。出| f.app);f < <“任何”;f < <“文本”;f < <“写”;f < <“将”;f < <“追加”;…

You can find more information about the open modes here and about fstreams here.

您可以在这里找到更多关于开放模式和fstreams的信息。

#5


0  

I got my code for the answer from a book called "C++ Programming In Easy Steps". The could below should work.

我从一本名为《简单步骤中的c++编程》(c++ Programming In Easy Steps)的书中获得了答案的代码。下面的罐子应该可以工作。

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

using namespace std;

int main()
{
    ofstream writer("filename.file-extension" , ios::app);

    if (!writer)
    {
        cout << "Error Opening File" << endl;
        return -1;
    }

    string info = "insert text here";
    writer.append(info);

    writer << info << endl;
    writer.close;
    return 0;   
} 

I hope this helps you.

我希望这对你有帮助。