小编rap*_*102的帖子

元素大小和计数在 fread 调用中可以交换吗?

假设我有一个大小为 5000 字节的文件,我正在尝试读取该文件。

我有这个代码:

int main()
{
  char *file_path[] = "/path/to/my/file"
  FILE *fp= fopen(file_path,"rb"); //open the file
  fseek(fp, 0, SEEK_END); // seek to end of file
  unsigned long fullsize = ftell(fp); //get the file size (5000 for this example)
  fseek(fp, 0, SEEK_SET); //bring back the stream to the begging  
  char *buf = (char*)malloc(5000);
  fread(buf,5000,1,fp);
  free(buf);
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

我也可以将fread呼叫替换为

fread(buf,1000,5,fp);
Run Code Online (Sandbox Code Playgroud)

什么是更好的?为什么?在优化方面,我理解返回值是不同的。

c file function fread

3
推荐指数
1
解决办法
1396
查看次数

排序数组有三个不同的整数,只有一个传递

有没有办法在数组上只有一个传递对三个整数的数组进行排序?

你得到:

int array[]={1,2,2,2,3,1,1,2,2,1,3}
Run Code Online (Sandbox Code Playgroud)

而且你被告知只有1,2,3号

要得到:

int array[]={1,1,1,1,2,2,2,2,2,3,3}
Run Code Online (Sandbox Code Playgroud)

并且不允许额外的空间

TNX!

c c++

0
推荐指数
2
解决办法
384
查看次数

标签 统计

c ×2

c++ ×1

file ×1

fread ×1

function ×1