64,637
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
ifstream in;
in.open("f:\\PhoneBook.txt",ios::in);
if(!in)
{
cout<<"Can't open file!"<<endl;
return -1;
}
char s[30];
string str1;
string str2;
int count = 0;
while(!in.eof())
{
in.getline(s,sizeof(s));
//cout<<s<<endl; //这里输出s = name:w
str1.assign(s,1,3); //我将s中 nme存在str1中,却显示错误 问题在这里 in.getline(s,sizeof(s));
str2.assign(s,7); //这个将s中前7个字符存于str2中
//str2 = s.substr(7,5);
//str2.assign(s,7,11);
cout<<str1<<endl;
cout<<str2<<endl;
count++;
/*这里是小测试
string str3;
char str4[] = "War and Peace";
str3.assign(str4,4,3); //这里却可以将str4中第4个开始数3个的字符and存于str3
cout<<str3<<endl;
*/
}
in.close();
cout<<"号码数共:"<<count<<"个。"<<endl;
return 0;
}
if (s[0] == '\0') continue;