[英]Using the function fread() to read blocks of data in a file
如果我用
size_t fread ( void * ptr, size_t size, size_t count, FILE * stream ),
如何取消引用ptr指向的数据? 就像我有
int main()
{
void *ptr
fread(ptr,1,100,file);
printf("%s",ptr);
}
您在fread
声明中误解了void * ptr
的目的。
从http://en.cppreference.com/w/c/io/fread
buffer-指向存储读取对象的数组的指针
首先,参数必须是可以存储对象的有效指针。 使用
void *ptr;
fread(ptr,1,100,file);
由于ptr
不会指向任何有效内容,因此将导致未定义的行为。
其次,参数类型的原因为void*
以允许您从流中读取所有类型的数据。 例如
// Read an integer
int i;
fread(&i, sizeof(int), 1, file);
// Read 10 integers
int a[10];
fread(a, sizeof(int), 10, file);
// Read a double
double d;
fread(&d, sizeof(double), 1, file);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.