假设我有一个大小为 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)
什么是更好的?为什么?在优化方面,我理解返回值是不同的。
有没有办法在数组上只有一个传递对三个整数的数组进行排序?
你得到:
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!