文件输入输出
file input output
在下面的代码中,我限制了写入文件的字符数必须小于15,&当我读回文件时,写入的字符正好是15个(如期望的那样)。但是第一个WHILE循环没有按预期工作,它应该被跳过&当计数器变量的值为15时,停止接收来自用户的输入;但它还在接收用户的输入,直到他/她没有按回车
#include<iostream>
#include<conio.h>
#include<string.h>
#include<fstream>
using namespace std;
int main()
{
int i=0;
ofstream out("my_file",ios::out|ios::binary); //'out' ofstream object
char ch1;
while(i<15) //receiving input even when i>15,till 'enter' IS pressed
{
cin>>ch1;
out.put(ch1);
i++;
}
out.close();
char ch;
ifstream in("my_file"); //'in' ifstream object
while(1)
{
in.get(ch);
if(in)cout<<ch;
}
in.close();
_getch();
return 0;
}
标准I/O功能需要按回车键才能工作。为了获得预期的效果,需要使用_getch,它会立即读取每个符号。注意_getch是不可移植的。
输入几乎总是行缓冲的,所以当程序从命令行读取时,它几乎总是阻塞,直到在输入处有一整行可用。
相关文章:
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++ 中文件输入输出中的长长 v/s int
- C++击球平均值程序使用输入/输出文件打印名称,平均值,最高/最低平均值时遇到麻烦
- 输入文件中的第一个字符是输出文件中的最后一个字符,反之亦然
- 使用 Octave GNU 访问输入/输出文件
- 使用 getline 从输入文件读取一行到输出文件
- TensorFlow模型:如何从原始爱好文件中识别输入/输出节点名称
- 为什么我的输出文件与输入文件不同
- 根据用户输入的字符串命名输出文件
- 我需要转换一些代码,以便它适用于输入和输出文件文本
- 输出文件从输入中提取零
- 如何在 c++ 中使用二进制文件输入/输出读取/写入结构的字符串类型成员
- 在C 中写入输入和输出文件
- 在CPP中,将输出文件作为输入文件作为输入文件
- C++文件输入/输出输出数字而不是字符
- 从.txt文件读取输入输出C++
- 家庭作业 输入/输出文件
- argv 和 argc 与输入/输出文件一起使用
- 输入/输出文件(一个字母的大写)
- 输入/输出文件(Sudoku Solver)