赞
踩
设置一个文件夹(目录),使得两个操作系统(windows和linux)能同时访问到,可以方便操作。
我们一般习惯在Windows系统写代码,当我们需要在Linux系统编译文件时,需要将Windows的文件传到Linux系统,设置一个共享文件夹的话,可以使得两个系统可以同时访问到,大大地方便了操作。
设置过程:
打开虚拟机->菜单->管理->虚拟机设置->选项->共享文件夹->总是启用。
点击添加按钮->弹出添加向导->点击浏览按钮,从windows中选择一个文件夹,确定即可。
验证:
pwd 打印当前的工作路径
cd 用于切换路径(文件夹/目录)
格式:cd 路径名
ls 列出指定目录下的文件名
格式:ls 路径名(可省略不写)
clear 清屏
touch 文件名字 创建文件 vi/vim 编辑器 有两种模式: 第一种:命令模式:按下i,进入编辑模式! 第二种:编辑模式:按下esc这个键,进入命令模式 退出vi的方式有三种 语法:进入命令行模式:写入 " : " ,之后我们再进行下一步操作 暖男的方式:q 它会询问你,是否要保存 渣男的方式:x 不管系统,直接退出并且保存 正常人:wq 退出并且保存! ls 查看列表 可以将你当前目录的所有文件名打印出来 cd 切换目录的指令 使用方法: cd 目录名 cd /mnt/hgfs/NHDXshare cd . 表示进入当前目录 cd .. 表示进入上一级目录 pwd 查看你的当前目录的绝对路径 mkdir 创建一个目录 使用方法: mkdir 目录名 mv 移动一个文件 假设我要把data里面的1.txt移动到note里面去,该如何做?? mv ./1.txt ../code cp 复制一个文件 假设我要把code里面的1.txt复制一份到data里面去,该如何做? cp ./1.txt ../data
在linux中,有几个特别的符号,表示某种特定的路径:
~ 表示home目录,一般完整形式可能是: /home/china
典型用法: cd ~
. 表示当前路径
典型用法: ./xxx
.. 表示当前路径的上一级目录
典型用法:
cd ..
- 表示上一次所在的路径
典型用法:
cd -
#include <stdio.h>
int main()
{
printf("hello world\n");
return 0;
}
cd 共享文件夹路径
gcc xxx.c(文件名) -o xx(文件名)
cd /mnt/hgfs/code
gcc 00-hello.c -o hello
./hello
我们可以看到编译00-hello.c文件后,执行hello文件后打印 hello world,打开共享文件夹可以看到编译后的hello文件。
(右键此电脑->管理->系统工具->设备管理器->端口,即可查看serial所连接的端口号,我的电脑连接的时com5,所以选择com5)
mkdir -p /文件名
arm-linux-gcc xxx.c -o xxx.arm
3、把生成的xxx.arm传输到GEC6818开发板
选择发送Xmodem
选择要传输的文件
4、添加执行权限
5、执行
./hello.arm
编译 .c文件
使用linux操作系统的好处:它提供了丰富的人机交互功能,或者说是人机的交互界面!
Linux的应用开发:
操作文件的步骤:
C语言库给我们提供了很多函数!
const char *pathname: 不可在本函数内部进行更改的一个路径名
int flags: 打开文件的方式
返回值:
NAME
read - read from a file descriptor
从一个文件描述符中去读取数据
SYNOPSIS
#include <unistd.h>
ssize_t read(int fd, void *buf, size_t count);
int fd : fd 表示是文件描述符!!也就是open的返回值!
void *buf :用来保存你从这个文件(fd)当中读取到的内容,我们一般用字符数组来保存
size_t count : 你想要从文件中去读取多少个字节!!
返回值:
成功,返回读到的字节数
失败:返回-1
当文件读取完了,返回一个 0 ;
NAME
write - write to a file descriptor
向文件描述符中里去写数据
SYNOPSIS
#include <unistd.h>
ssize_t write(int fd, const void *buf, size_t count);
int fd : 文件描述符
const void *buf : buf 是保存你要写入到文件里面去的数据
size_t count : 你想写入文件数据的大小,或者说你想写多少字节到文件当中去!
返回值:
成功,返回实际上写入的字节数
失败,返回-1
NAME
close - close a file descriptor
关闭文件
SYNOPSIS
#include <unistd.h>
int close(int fd);
int fd : 其实就是我们前面open这个函数的返回值,也就是一个文件文件描述符!!
NAME lseek - reposition read/write file offset SYNOPSIS #include <sys/types.h> #include <unistd.h> off_t lseek(int fd, off_t offset, int whence); int fd : 你要操作的那个文件的文件描述符 off_t offset : 偏移量 >0 往后偏移 <0 往前偏移 =0 不动 int whence : 光标定位的方式 1、是文件的开头 SEEK_SET:基于文件的开头 2、是文件的结尾 SEEK_END:基于文件的结尾 3、是文件的当前位置 SEEK_CU:基于光标的当前位置 返回值: 失败,返回-1 成功,返回新光标到文件开头的位置 例如: 我想把光标定位在文件结尾处: lseek(fd,0,SEEK_END);
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { //打开文件 //打开文件成功的话,我们就返回一个整数,这个整数(文件描述符)是>0!! //打开文件失败就返回-1; int fd = open("./1.txt",O_RDWR); if(fd == -1) { printf("open error\n"); return -1; } //操作文件 char buf[10] = {0}; int ret = read(fd, buf, 5); if(ret == -1) { printf("read error\n"); return -1; } if(ret == 0) { printf("read over\n"); return 1; } printf("%s\n",buf); close(fd); return 1; }
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main(){ int fd = open("./2.txt",O_RDWR); if(fd == -1) { printf("open error\n"); return -1; } //写文件 char buf[10] = {'a','b','c','2','3'};//字符数组 //char *buf1 = "asdff12";//字符串 int ret = write(fd, buf, 5); if(ret == -1) { printf("write error\n"); return -1; } close(fd); return 0; }
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <unistd.h> #include <stdio.h> int main(){ //打开文件 int fd = open("./biji.md",O_RDWR); if(fd == -1) { printf("open error\n"); return -1; } //它的返回值是新光标到文件开头的位置 int num = lseek(fd, 0, SEEK_END); printf("num = %d\n", num); close(fd); return 0; }
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <stdlib.h> #include <unistd.h> int main() { int fd = open("./1.txt",O_RDWR); int fd1 = open("./2.txt",O_RDWR); if(fd == -1) { printf("open error\n"); return -1; } if(fd1 == -1) { printf("open error\n"); return -1; } //操作文件 char buf[2] = {0}; while (1) { int ret = read(fd, buf, 1); if (ret == -1) { printf("read error\n"); return -1; } if (ret == 0) { printf("read over\n"); break; } int ret1 = write(fd1, buf, 1); if(ret1 == -1) { printf("write error\n"); return -1; } } close(fd); close(fd1); return 1; }
粤嵌GEC6818-学习笔记2-屏幕相关及音频播放https://blog.csdn.net/weixin_45735391/article/details/125452935
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。