《嵌入式系统开发实践》实践一 Linux 文件I/O操作

一、 实验目的

  1. 掌握函数stat中文件属性的应用;
  2. 掌握系统函数system、opendir等的使用;
  3. 掌握struct dirent的结构体变量的使用方法;
  4. 掌握文件属性的判断;
  5. 掌握系统函数open、read、write、close的使用方法。
  6. 掌握设备操作的系统函数使用方法。

二、 实验任务与要求

  1. 测试文件S_IRUSR、S_IWUSR、S_IRGRP、S_IROTH属性;
  2. 应用readdir函数显示文件和子目录;
  3. 文件属性的判断;
  4. 应用read和write实现对文件的操作。

三、 实验工具和环境
PC机、Linux Ubuntu操作系统。

四、 实验内容与结果

  1. 设计一个程序,要求判断“/etc/passwd”的文件类型,提示:使用st_mode属性,可以使用几个宏来判断:

S_ISLNK(st_mode) 是否是一个链接文件;
S_ISREG (st_mode) 是否是一个普通文件;
S_ISDIR (st_mode) 是否是一个目录;
S_ISCHR (st_mode) 是否是一个字符设备;
S_ISBLK (st_mode) 是否是一个块设备;
S_ISFIFO (st_mode) 是否是一个管道文件;
S_ISSOCK (st_mode) 是否是一个SOCKET文件。

#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>

int main() {
    struct stat st;
    stat("/etc/passwd", &st);
    if (S_ISDIR(st.st_mode)) {
        printf("is dir!\n");
    } else if (S_ISREG(st.st_mode)) {
        printf("is file!\n");
    } else {
        printf("other! \n");
    }
    // ... 剩下的可以自行拓展
}
  1. 应用文件属性,编写程序,请将程序代码拷贝在本题下,并将结果截图插入,要求实现命令ls –l的功能,要求在格式上必须与系统的命令一致,即:
    -rw-r–r-- 1 tarena tarena 8445 8月 20 2012 exam.desktop
    prw-rw-r-- 1 tarena tarena 0 3月 20 20:17 fifo
    drwxr-xr-x 2 tarena tarena 4096 8月 20 2012 Music
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <sys/stat.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
#include <time.h>

void print_mode(mode_t st_mode) {
    putchar((S_ISDIR(st_mode)) ? 'd' : '-');
    putchar((st_mode & S_IRUSR) ? 'r' : '-');
    putchar((st_mode & S_IWUSR) ? 'w' : '-');
    putchar((st_mode & S_IXUSR) ? 'x' : '-');
    putchar((st_mode & S_IRGRP) ? 'r' : '-');
    putchar((st_mode & S_IWGRP) ? 'w' : '-');
    putchar((st_mode & S_IXGRP) ? 'x' : '-');
    putchar((st_mode & S_IROTH) ? 'r' : '-');
    putchar((st_mode & S_IWOTH) ? 'w' : '-');
    putchar((st_mode & S_IXOTH) ? 'x' : '-');
}

void print_time(time_t time) {
    char buf[80];
    struct tm *tm = localtime(&time);
    strftime(buf, sizeof(buf), "%b %e %H:%M", tm);
    printf("%s", buf);
}

int main(int argc, char *argv[]) {
    DIR *dp;
    struct dirent *dirp;
    struct stat statbuf;
    struct passwd *pwd;
    struct group *grp;
    
    if ((dp = opendir(".")) == NULL) {
        perror("opendir");
        exit(1);
    }

    while ((dirp = readdir(dp)) != NULL) {
        if (lstat(dirp->d_name, &statbuf) == -1) {
            perror("lstat");
            continue;
        }

        print_mode(statbuf.st_mode);
        printf(" %lu ", statbuf.st_nlink);

        if ((pwd = getpwuid(statbuf.st_uid)) != NULL)
            printf("%s ", pwd->pw_name);
        else
            printf("%d ", statbuf.st_uid);

        if ((grp = getgrgid(statbuf.st_gid)) != NULL)
            printf("%s ", grp->gr_name);
        else
            printf("%d ", statbuf.st_gid);

        printf("%5lld ", statbuf.st_size);
        print_time(statbuf.st_mtime);
        printf(" %s\n", dirp->d_name);
    }

    closedir(dp);

    return 0;
}
  1. 在当前目录中打开一个文件,利用open、read、write、close实现文件的复制。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    int sourceFile, destinationFile; //定义源文件和目标文件的文件描述符
    ssize_t readBytes, writeBytes; //定义读取和写入的字节数
    char buffer[1024]; //定义缓冲

    sourceFile = open("sum.c", O_RDONLY); //打开源文件
    if (sourceFile == -1) { //检查是否打开成功
        perror("Error opening source file");
        exit(EXIT_FAILURE);
    }

    destinationFile = open("tsum.c", O_WRONLY | O_CREAT | O_TRUNC, 0644); //打开目标文件
    if (destinationFile == -1) { //检查是否打开成功
        perror("Error opening destination file");
        close(sourceFile); //关闭源文件
        exit(EXIT_FAILURE);
    }

    while ((readBytes = read(sourceFile, buffer, sizeof(buffer))) > 0) { //循环读取源文件内容
        writeBytes = write(destinationFile, buffer, readBytes); //将读取到的内容写入目标文件
        if (writeBytes != readBytes) { //检查写入是否成功
            perror("Error writing to destination file");
            close(sourceFile); //关闭源文件
            close(destinationFile); //关闭目标文件
            exit(EXIT_FAILURE);
        }
    }

    if (readBytes == -1) { //检查读取是否成功
        perror("Error reading from source file");
        close(sourceFile); //关闭源文件
        close(destinationFile); //关闭目标文件
        exit(EXIT_FAILURE);
    }

    close(sourceFile); //关闭源文件
    close(destinationFile); //关闭目标文件

    return 0;
}
  1. 利用标准I/O操作中的fopen、fread、fwrite、fseek、fclose等函数实现文件显示命令cat和cp的功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 1024

void cat(char *filename)
{
    // 先读取并显示源文件
    FILE *file = fopen(filename, "r"); // 打开文件
    if (file == NULL)
    { // 检查是否打开成功
        perror("Error opening file");
        exit(EXIT_FAILURE);
    }
    char buffer[BUFFER_SIZE];
    size_t readBytes;
    while ((readBytes = fread(buffer, 1, BUFFER_SIZE, file)) > 0)
    {                                         // 循环读取文件内容
        fwrite(buffer, 1, readBytes, stdout); // 将读取到的内容输出到标准输出
    }
    fclose(file); // 关闭文件
}

void cp(char *sourceFileName, char *destinationFileName)
{

    // 然后cp命令
    FILE *sourceFile = fopen(sourceFileName, "r"); // 打开源文件
    if (sourceFile == NULL)
    { // 检查是否打开成功
        perror("Error opening source file");
        exit(EXIT_FAILURE);
    }

    FILE *destinationFile = fopen(destinationFileName, "w"); // 打开目标文件
    if (destinationFile == NULL)
    { // 检查是否打开成功
        perror("Error opening destination file");
        fclose(sourceFile); // 关闭源文件
        exit(EXIT_FAILURE);
    }

    char buffer[BUFFER_SIZE];
    size_t readBytes;

    while ((readBytes = fread(buffer, 1, BUFFER_SIZE, sourceFile)) > 0)
    {                                                  // 循环读取源文件内容
        fwrite(buffer, 1, readBytes, destinationFile); // 将读取到的内容写入目标文件
    }

    fclose(sourceFile);      // 关闭源文件
    fclose(destinationFile); // 关闭目标文件
}

int main(int argc, char *argv[])
{

    char *sourceFileName = "source.txt";
    char *destinationFileName = "dest.txt";

    cat(sourceFileName);
    cp(sourceFileName, destinationFileName);

    return 0;
}
  1. 读取wav文件的文件头,wav可以从网上自行下载。
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
typedef struct WAV
{
    char riff[4];
    long len;
    char type[4];
    char fmt[4];
    char tmp[4];
    short pcm;
    short channel;
    long sample;
    long rate;
    short framesize;
    short bit;
    char data[4];
    long dblen;
} wav_t;

int main(int argc, char *argv[])
{

    int fd = open("bird_free.wav", O_RDONLY); // 以二进制模式打开文件
    if (fd == -1)
    { // 检查是否打开成功
        printf("----- open err = %s \n", strerror(errno));
        return -1;
    }
    char buf[4096];
    wav_t *p = (wav_t *)buf;
    int size = read(fd, p, sizeof(wav_t));
    close(fd);

    printf("---%s,%d,%d,%d\n", p->type, p->channel, p->sample, p->bit);
    printf("---%d,%d\n", p->len, p->dblen);

    return 0;
}

五、 实验总结

  • 6
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

okfang616

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值