如何在C ++中使用fputs将用户输入写入文本文件 [英] How to write user input to a text file using fputs in C++
本文介绍了如何在C ++中使用fputs将用户输入写入文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我不是使用c ++的最佳程序员,但我正在尝试学习.我正在尝试接受用户输入并将其写入文本文件(而不覆盖旧文件),但是我不知道在哪里插入变量(int).到目前为止,这是我的代码...
I am not the best programmer with c++ but I am trying to learn. I am trying to take user input and write it into a text file (without overwriting the old one) but I can't figure out where to plugin the variable (int). Here is my code so far...
int main()
{
int i;
cout << "Please enter something: ";
cin >> i;
FILE * pFile;
pFile = fopen ("C:\\users\\grant\\desktop\\test.txt","a");
if (pFile!=NULL)
fputs ("C++ Rocks!",pFile);
fclose (pFile);
getch();
return 0;
}
此外,如果有更有效的方法,请告诉我!这就是我在互联网上有效的
Also, if there is a more effecient way of doing this, please let me know! This is just what I was able to find on the internet that worked.
推荐答案
我认为这可以做到:
{
string i; // dont forget to #include <string>
cout << "Please enter something: ";
cin >> i;
FILE * pFile;
pFile = fopen ("C:\\users\\grant\\desktop\\test.txt","a");
if (pFile!=NULL)
{
fputs("C++ Rocks!", pFile);
fputs(i.c_str(), pFile);
}
fclose (pFile);
getch();
return 0;
}
这篇关于如何在C ++中使用fputs将用户输入写入文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文