开发者

file input output

开发者 https://www.devze.com 2023-03-31 14:34 出处:网络
In the following code,i have restricted that number of characters to be written on the file must be LESS than 15,& the characters written are exactly 15(as desired),when i readback the file.But th

In the following code,i have restricted that number of characters to be written on the file must be LESS than 15,& the characters written are exactly 15(as desired),when i read back the file.But the first WHILE loop is not working as desired,it should have to be skipped & STOP receving input from the user,when the counter variable have a value 15, but it is yet receiving input from user,till he/she not pre开发者_如何转开发sses enter

#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;
    }


Standard I/O functions work only after pressing Enter. To get desired effect, you need to use _getch, which reads every symbol immediately. Notice that _getch is not portable.


input is almost always line buffered, so when a program reads from the command line, it almost always blocks until there is an entire line available at the input.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号