将用户输入到字符串中并将其写入文本文件-不工作
Taking users input into a string and writing it into a text file - Not working
我的程序显示一条消息给用户输入,可以是没有长度限制的任何东西。
using namespace std;
void child() {
string i;
cout<<endl;
cout<<"Enter your text:"<<endl;
getline(cin, i);
cout<<endl;
ofstream out;
out.open("pro1.txt");
out<<i;
sleep(2);
out.close();
}
main()
{
int r = fork();
if (r == 0) // child
{
child();
exit(0);
} else if (r < 0) // failed to fork
{
cout << "Failed to fork" << endl;
// Throw exception
} else // parent
{
// Code only executed by parent process
cout<<"im in parent";
}
// Code executed by both parent and child.
}
这个输入将保存在一个字符串中,这个字符串将保存在文本文件中。我无法弄清楚我的代码有什么问题,因为它只需要第一个字母并将其保存在文本文件中。
请记住这是在子进程中应用的!
编辑:如您所见,我创建了一个新进程来完成这项工作
将cin >> i;
改为getline(cin, i);
。cin
一旦发现空白,它将停止读取
相关文章:
- 文本文件中的单词链表
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 如何将内容数组写入文本文件?
- 无法通过空白将文本文件行分隔为矢量
- 我正在使用嵌套的while循环来解析具有多行的文本文件,但由于某种原因,它只通过第一行,我不知道为什么
- C++将文本文件中的数据读取到结构数组中
- 在指针的帮助下,文本文件中单词的频率
- 将字符指针十六进制转换为字符串并保存在文本文件C++中
- 将值从二维数组输出到文本文件
- 如何在c++中从文本文件中逐行读取整数
- 从文本文件中读取时钟时间和事件时间并进行处理
- 如何从文本文件中读取值和数组
- 如何在C++中确定文本文件中的元素是字符还是数字
- C++试图读取一个文件并输出到另一个文本文件
- 如何通过套接字将文本文件的内容从服务器发送到客户端
- 已修改的LinkedList未在文本文件本身中更新
- C++文本文件的获取线
- 为什么C++的文件 I/O 在读取文本文件时忽略初始空行?我怎样才能让它不这样做?
- 打印逐行存储的文本文件
- C++文本文件输入