美文网首页
12 B 文件输入输出

12 B 文件输入输出

作者: LuckTime | 来源:发表于2016-06-18 09:57 被阅读18次

    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,中的换行符输出了出来)
    }

    相关文章

      网友评论

          本文标题:12 B 文件输入输出

          本文链接:https://www.haomeiwen.com/subject/xonvdttx.html