是的,我有这种问题,因为我总是很难遵循指针操作。所以,我有这个简单的代码:
struct myfile {
char* name;
char* content;
long size;
};
myfile this_file;
int main() {
read();
return 0;
}
void read() {
output("Please, specify file name: ");
cin >> (this_file.name = new char);
FILE *stream;
stream = fopen(code.name, "r");
if (stream != NULL) {
fseek(stream , 0, SEEK_END);
myfile.size = ftell(codefile);
myfile.content = new char[myfile.size];
fseek(myfile, 0, SEEK_SET);
if ((fread(myfile.content, 1, myfile.size, stream)) == 0) {
fclose(codefile);
cout << "File is empty!\n");
}
}
}
它正确地获取了文件的名称,它获取了内容的大小,但是当尝试为content
成员分配空间时,程序崩溃了,我知道这是一些指针问题,但是像往常一样,不记得/弄清楚什么是吗。到达此行时崩溃:myfile.content = new char[myfile.size];
我再次需要你的帮助。谢谢!