【发布时间】:2011-05-21 08:35:48
【问题描述】:
我试图将整个 file.txt 读入一个字符数组。但是有一些问题,请提出建议=]
ifstream infile;
infile.open("file.txt");
char getdata[10000]
while (!infile.eof()){
infile.getline(getdata,sizeof(infile));
// if i cout here it looks fine
//cout << getdata << endl;
}
//but this outputs the last half of the file + trash
for (int i=0; i<10000; i++){
cout << getdata[i]
}
【问题讨论】:
-
或者也许有人可以建议一种将文本文件存储到 char 数组中的更好方法。
-
如果您在除玩具应用程序之外的任何其他应用程序中执行此操作,请确保您设置了防止无限内存分配的保护措施。
-
您似乎遗漏了一些分号。