zoukankan      html  css  js  c++  java
  • C++输入输出知识

    1.strtok将字符串中的单词用' '分割出来

    #include<iostream>
    #include<cstdio>
    #include<cstdlib>
    #include<cstring>
    #include<fstream>
    using namespace std;
    int main()
    {
        /*
            fstream fin("input.txt",ios::in); fin>>value
        */
        fstream fin("output.txt",ios::out);
        char p[]="i am a student, you are a student too.";
        char *s=strtok(p," ");
        while(s!=NULL)
        {
            fin<<s;
            fin<<'
    ';
            s=strtok(NULL," ");
        }
        
        return 0;
    }

    输出结果:
    i
    am
    a
    student,
    you
    are
    a
    student
    too.

    具有同样效果的代码:
    #include<iostream>
    #include<sstream>
    #include<string>
    using namespace std;int main()
    {
        string line;
        getline(cin,line);//将回车也输入;
        stringstream ss;
        ss<<line;
        while(ss)
        {
            string word;
            ss>>word;
            cout<<word<<endl;//最后一个word为"
    "
        }
        return 0;
    }

     c语言中 gets()也能将空格输入

    #include"cstdio"
    #include"cstring"
    using namespace std;
    int main()
    {
        char s[100];
        gets(s);//回车做结束,且回车不会输入到字符串中,gets()合适做迷宫输入
        puts(s);
        return 0;
    }
  • 相关阅读:
    getopt for windows
    开源代码学习之Tinyhttpd
    GCC 中的编译器堆栈保护技术
    读《程序员的思维修炼》有感
    main之前初始化流程
    平均速度
    显示图案
    圆的面积和周长
    C#(Winform) Http 发送数据
    Android BaseAdapter的使用
  • 原文地址:https://www.cnblogs.com/program-ccc/p/4782108.html
Copyright © 2011-2022 走看看