#include
#include
#include
using namespace std;
string *getdate(char *a,int b){//这里把string a改成char *a,因下一句要求()里是char*
ifstream in(a);
string line;
string *temp=new string[b];
int count = 0;
if(in){
while(getline(in, line),count cout << line << endl;//这里输出有必要吗?尽量不要在函数数中搞输出
temp[count] = line;//这里把count-1改count,不然第一次是-1而非法
count++;
}
}
else
cout << "no such file" << endl;
return temp;
}
int main(void){
string *str;
int a=4;
//str = new string[a];这一行删除,不然有引起内存泄漏之嫌
str = getdate("test.txt",a);
for(int i = 0; i < a; i++){
cout << "第" << i+1 << "行:" << str[i] << endl;
}
delete []str;//不释放申请的空间能行吗?
return 0;
}