// c_datastructure.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
cout << "************" << endl;
char ch[20];
cout << "enter a sentence:" << endl;
cin >> ch;
/*
用cin>>从输入流中提取数据,遇到空格符就结束。因此只读出I存入ch[0],然后在ch[1]
存入'/',输出只有I
*/
cout << "The string read with cin is: " << ch << endl;
cin.getline(ch, 20, '/');
/*
会从输入流中读取19个字符(最后一位保留做结束符),然后输入流中第一次遇到'/'就结束,
把I like c++存入前十个数组元素中,输出I like c++
*/
cout << "The second part is: " << ch << endl;
cin.getline(ch, 20);
/*
由于未指定结束符,所以第一个和第二个'/'被当做字符被读取,一共读入19个字符,最后输出。
*/
cout << "The third part is: " << ch << endl;
// delete pt;
return 0;
}
如果把该程序中的两个cin.getline()函数都改为:cin.getline(ch,20,'/');
第一个'/'字符因为输入流读到时候自动填装到末位,那么字符指针就指向下一个,故此输出后面的I study c++.遇到第二个'/'字符就结束。
EOF函数:这是end of file缩写,表示文件结束。从输入流读取数据,如果达到文件末尾或者遇到文件结束符,eof函数值为非0,表示真;为0,表示假。下面使用该函数输出一行字符中非空的字符。
// c_datastructure.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
int main()
{
cout << "************" << endl;
char c;
// eof()为假则退出,表示文末遇到文件结束符。
while (!cin.eof()) {
// 检查输入的字符是否为空格符
if ((c = cin.get()) != ' ') {
cout.put(c);
}
}
return 0;
}