【发布时间】:2018-11-10 18:08:32
【问题描述】:
我正在尝试打开一个文件并将其中的信息保存在一个字符数组中,但是我没有得到它。要保存在字符串中,请使用:
int main(){
string line1;
ifstream myfile;
myfile.open("example.txt");
if(!myfile){
cout<<"Unable to open the file."<<endl;
exit(0);
}
while(getline(myfile,line1)){
ReadFile(myfile);
}
}
而且它有效。 当我使用一个字符数组时,我的代码是这样的:
int main(){
int size=100;
char line1[size];
ifstream myfile;
myfile.open("example.txt");
if(!myfile){
cout<<"Unable to open the file."<<endl;
exit(0);
}
while(myfile.peek()!EOF){
line1[size]->ReadFile();
}
}
ReadFile 函数是这样的:
void ReadFile(ifstream &is){
char aux[100];
is.getline(aux,100);
}
【问题讨论】:
-
myfile.peek()!EOF似乎不对。line1[size]->myfile;也很奇怪。 -
那么不要使用字符数组 - 在这里使用字符串是正确的。此外,
char line1[size];不是有效的 C++ 代码。 -
我在其他程序中使用过它并且它有效。你有什么建议吗? @Quimby
-
@NeilButterworth 我需要使用 chars 来实现另一个计算文件中字符数的函数
-
使用字符串流读取整个文件。 stackoverflow.com/questions/2602013/…
标签: c++ arrays char readfile ifstream