[英]Writing to a file in C++
I'm trying to make a program that will get the user input of a new file name, create the file, and write to it. 我正在尝试制作一个程序,该程序将获取用户输入的新文件名,创建文件并将其写入。 It works but it will only write the first word of the string to the file.
它可以工作,但是只会将字符串的第一个单词写入文件。 How can i get it to write the full string?
我怎样才能写出完整的字符串? thanks.
谢谢。
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
for (;;)
{
char *myFile = " ";
string f = " ";
string w = " ";
cout <<"What is the name of the file you would like to write to? " <<endl;
cin >>f;
ofstream myStream(f,ios_base::ate|ios_base::out);
cout <<"What would you like to write to " <<f <<" ? ";
cin >>w;
myStream <<w;
if (myStream.bad())
{
myStream <<"A serious error has occured.";
myStream.close();
break;
}
}
}
According to this post , you should consult this reference to use a method like getline(). 根据这篇文章 ,您应该查阅此参考资料以使用类似getline()的方法。
Also, when you are writing out I recommend that you flush the output (cout.flush()) before ending the program, especially in this case, since I presume you are ending the program with a ctrl-C break. 另外,在进行写操作时,我建议您在结束程序之前刷新输出(cout.flush()),尤其是在这种情况下,因为我认为您正在以ctrl-C中断结束程序。
In formulating a suggestion, I will read data into char*, and convert them to "string" in case you will use them elsewhere in your program. 在提出建议时,我将数据读入char *,并将它们转换为“字符串”,以防您在程序中的其他地方使用它们。
I tested this code in MS Visual C++ Express. 我在MS Visual C ++ Express中测试了此代码。
#include "stdafx.h"
#include <fstream>
#include <iostream>
#include <string>
using namespace std;
int main()
{
for (;;)
{
char *myFile = new char[200]; // modified this line
//added this line
char *myInput = new char[200];
string f = " ";
string w = " ";
cout << "What is the name of the file you would like to write to? " << endl;
cin.getline(myFile, 200);//modified this line
f = (myFile);//added this line
cin.clear(); //added this line
ofstream myStream(f, ios_base::ate | ios_base::out);
cout << "What would you like to write to " << f << " ? ";
cin.getline(myInput, 200);//edited this line
w = string(myInput);//added this line
myStream << w;
myStream.flush();//added this line
if (myStream.bad())
{
myStream << "A serious error has occured.";
myStream.close();
break;
}
delete myFile;
delete myInput;
}
}
You have to use std::getline()
to read the entire line: 您必须使用
std::getline()
读取整行:
std::getline(std::cin >> std::ws, w);
The >> std::ws
part gets rid of leading whitespace which is needed because the newline left in the stream from the previous extraction will prevent std::getline()
from fully consuming the input. >> std::ws
部分摆脱了前导空格,这是必需的,因为前一次提取在流中留下的换行符将阻止std::getline()
完全占用输入。
When inserting data into the stream you need to make sure it gets flushed (because, as the other answer said, you're probably using Ctrl+C to terminate the program and you may not see output during the program run). 在将数据插入流中时,需要确保将其刷新(因为正如另一个答案所说,您可能正在使用Ctrl + C终止程序,并且在程序运行期间可能看不到输出)。 You can use the
std::flush
manipulator to flush the output: 您可以使用
std::flush
操纵器刷新输出:
myStream << w << std::flush;
cin<<w;
cin would stop consuming input character when it encounter whitespace tab and other unseeable characters. 如果cin遇到空白标签和其他看不见的字符,它将停止使用输入字符。
you should probably use std::getline()
instead. 您可能应该改用
std::getline()
。 take a look at this page for ref. 看看这个页面的参考。 http://en.cppreference.com/w/cpp/string/basic_string/getline
http://en.cppreference.com/w/cpp/string/basic_string/getline
Or you can use manipulator to not skip whitespace. 或者,您可以使用操纵器不跳过空格。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.