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;
    }
  • 相关阅读:
    使用libgdx及其中的box2d 2.1的注意事项
    Android.mk file syntax specification(ndkr8)
    NDK Note
    Problems of Android NDK
    Android.mk of NDK
    Android IM Note
    Regular Expression
    Some Efficient Algorithms
    libgdx use TexturePacker
    The Conversion Of JNI
  • 原文地址:https://www.cnblogs.com/program-ccc/p/4782108.html
Copyright © 2011-2022 走看看