linux读取u盘并拷贝文件c语言
时间: 2023-07-26 19:17:56 浏览: 76
你可以使用C语言中的系统调用来读取U盘并拷贝文件,以下是一个简单的示例代码:
```c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main() {
char source[100], destination[100];
printf("Enter source file path: ");
scanf("%s", source);
printf("Enter destination file path: ");
scanf("%s", destination);
// 打开源文件和目标文件
FILE *sourceFile = fopen(source, "rb");
FILE *destFile = fopen(destination, "wb");
if (sourceFile == NULL || destFile == NULL) {
printf("Error: Could not open file.\n");
exit(1);
}
// 从源文件中读取数据并写入到目标文件中
int bufferSize = 1024;
char buffer[bufferSize];
int bytesRead = 0;
while ((bytesRead = fread(buffer, 1, bufferSize, sourceFile))) {
fwrite(buffer, 1, bytesRead, destFile);
}
// 关闭文件
fclose(sourceFile);
fclose(destFile);
printf("File copied successfully.\n");
return 0;
}
```
在Linux中,U盘通常会挂载到`/media`目录下,你可以使用以下命令来查看U盘的挂载路径:
```
$ ls /media
```
然后,你就可以将U盘的路径作为源文件路径传递给上面的代码,再将目标文件路径设置为你想要拷贝到的本地文件路径即可。