12 B 文件输入输出

cin.get();
cin.getline();
cin.putback();

cout.put(char);
<< endl ; 对应getline();

//技巧:cout.put(cin.get());输出输入的字符

include <iostream>

include <string>

include <fstream>

include <ctime>

using namespace std;

int main()
{
ofstream fout("a.txt");
char a,b,c,d;
a = cin.get();
cin.get(b); //输入一个字符
cin.get(c).get(d);

fout << a << endl;
fout << b << endl;
fout << c << endl;
fout << d << endl;
fout.close();
string s;
ifstream fin("a.txt");
if(!fin)
{
cout << "error" << endl;
}

while(getline(fin,s,'\n'))
{
for(int i = 0; i < 5; i++)
{
long t = time(NULL);
while(time(NULL) == t);
cout << s <<flush;
}
}
}
//==================================

include <iostream>

include <fstream>

include <string>

include <ctime>

using namespace std;

int main(int argc, char* argv[])
{

ifstream fin(argv[1]);
char ch;
/*for(;;)
{
fin.get(ch); //输入字符到 ch中
if(!fin)
break;
time_t t = time(NULL);
while(time(NULL) == t);
cout << ch << flush; //直接从缓存区中读取
}
fin.close();
*/

while(fin.get(ch)) //两种一样
{
if (!fin)
{
break;
}
time_t t = time(NULL);
while(time(NULL) == t);
cout << ch << flush;
}
fin.get();
}
//============================
//===========================
//==========================
//=========================
cin.getline(字符数组名,长度)//防止长度溢出,确保长度要比实际长度要长
char string1【256】
cin。getline(string1,257)//get whole line
cin >> string 1;//stop at the 1 blank space;

getline(cin,str)//从文件中读取
//===========================

include <iostream>

include <fstream>

using namespace std;

include <string>

int main()
{

string str;
char buf[20];
cout << "input a line" << endl;
getline(cin,str);
cout << "str = " << str << endl;
cout << "input a line(<20)" << endl;
cin.getline(buf,20);
cout << "buf = " << buf << endl;

cin.clear(); //但是如果存在cin.clear()。就可以清空缓存区。。buf没有字节的限制

//当读的元素大于20,输入错误状态。就不再执行任何输入
if (!cin)
{
cout << "read error state" << endl;
}

int n;
cout << "input an integer" << endl;
cin >> n;
cout << "n = " << n <<endl;
}
::有cin。clear()主看buf 存储

没cin.clear()

include <iostream>

include <fstream>

using namespace std;

include <string>

int main()
{
ifstream fin("a.txt");
string str;
getline(fin,str,':'); //读字符串,读到: 结束
cout << "username:"<< str<< endl;

getline(fin,str,':');
cout << "password:"<< str<< endl;

getline(fin,str,':');
cout << "user id:"<< str<< endl;

getline(fin,str,':');
cout << "group id:"<< str << endl;

getline(fin,str);
cout << "info:" << str << endl;
cout << "first ch (only one)" << endl;
char no;
char ch = cin.get();
fin.clear();
char name[20];
cout << ch<< endl;//只输出一个字符
cout << "switch ASCLL " << endl;
cout << cin.get()<< flush; //将输入的字符转换为ASCLL
//===================================
cin.clear();
cout <<" second ch "<<endl;
ch = cin.peek(); //peek 偷看功能(偷看下一个字符施舍么)
cout << ch <<endl;
if (ch >= '0' &&ch <= '9' )
{
cin >> no;
cout << "no = " << no <<endl;
}
else
{
cin.getline(name,20);
cout << "name = " << name<< endl;

}
//a.txt 内容:china:123456:477:147:please into
//从文件中依次读取读取

}
//getline(cin,str,'\n'); 到\n结束
//cin.getline(cin,str,'\n');
//peek TK 偷看输入流的字符,返回Ascll
//cin.putback 把取走的字符,退回到输入符中

//putback()

include <iostream>

include <fstream>

include <string>

using namespace std;
int main()
{
char ch;
int n;
cout << "input an integer "<< endl;
cin.get(ch);//get会读走一个字符
cin >> n;
cout << "ch = " << ch << endl;
cout << " n = " << n << endl;
cout << "input an integer "<< endl;
cin.get(ch);
cin.putback(ch);//读走一个字符,再读回去
cin >> n;
cout << "ch = " << ch << endl;
cout << " n = " << n << endl;
//putback(ch 现在是换行符,putback是将第一次输入的3869\n,中的换行符输出了出来)
}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • C/C++输入输出流总结 前两天写C++实习作业,突然发现I/O是那么的陌生,打了好长时间的文件都没有打开,今天终...
    LuckTime阅读 1,748评论 0 6
  • 1. 流 流:数据从一个对象到另一个对象的传输。 功能:标准输入输出+文件处理 分类含义文本流一串ASCII字符二...
    jdzhangxin阅读 1,420评论 0 4
  • 浅谈C++常用输入输出 在编写C++程序的时候,经常因为输入输出头疼,所以在这里做一个小结,记录一下常用的输入输出...
    MinoyJet阅读 3,771评论 0 6
  • 原文出自【比特网】,转载请保留原文链接:http://soft.chinabyte.com/database/46...
    petit_prince阅读 7,684评论 0 2
  • 输入 : I 从控制台读取(文件)输出 : o 从控制台写入(文件) cin 键盘 标准输入流cout 屏幕...
    LuckTime阅读 320评论 0 0