国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

《嵌入式系統(tǒng)開(kāi)發(fā)實(shí)踐》實(shí)踐一 Linux 文件I/O操作

這篇具有很好參考價(jià)值的文章主要介紹了《嵌入式系統(tǒng)開(kāi)發(fā)實(shí)踐》實(shí)踐一 Linux 文件I/O操作。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問(wèn)。

一、 實(shí)驗(yàn)?zāi)康?/p>

  1. 掌握函數(shù)stat中文件屬性的應(yīng)用;
  2. 掌握系統(tǒng)函數(shù)system、opendir等的使用;
  3. 掌握struct dirent的結(jié)構(gòu)體變量的使用方法;
  4. 掌握文件屬性的判斷;
  5. 掌握系統(tǒng)函數(shù)open、read、write、close的使用方法。
  6. 掌握設(shè)備操作的系統(tǒng)函數(shù)使用方法。

二、 實(shí)驗(yàn)任務(wù)與要求

  1. 測(cè)試文件S_IRUSR、S_IWUSR、S_IRGRP、S_IROTH屬性;
  2. 應(yīng)用readdir函數(shù)顯示文件和子目錄;
  3. 文件屬性的判斷;
  4. 應(yīng)用read和write實(shí)現(xiàn)對(duì)文件的操作。

三、 實(shí)驗(yàn)工具和環(huán)境
PC機(jī)、Linux Ubuntu操作系統(tǒng)。

四、 實(shí)驗(yàn)內(nèi)容與結(jié)果

  1. 設(shè)計(jì)一個(gè)程序,要求判斷“/etc/passwd”的文件類(lèi)型,提示:使用st_mode屬性,可以使用幾個(gè)宏來(lái)判斷:

S_ISLNK(st_mode) 是否是一個(gè)鏈接文件;
S_ISREG (st_mode) 是否是一個(gè)普通文件;
S_ISDIR (st_mode) 是否是一個(gè)目錄;
S_ISCHR (st_mode) 是否是一個(gè)字符設(shè)備;
S_ISBLK (st_mode) 是否是一個(gè)塊設(shè)備;
S_ISFIFO (st_mode) 是否是一個(gè)管道文件;
S_ISSOCK (st_mode) 是否是一個(gè)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. 應(yīng)用文件屬性,編寫(xiě)程序,請(qǐng)將程序代碼拷貝在本題下,并將結(jié)果截圖插入,要求實(shí)現(xiàn)命令ls –l的功能,要求在格式上必須與系統(tǒng)的命令一致,即:
    -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. 在當(dāng)前目錄中打開(kāi)一個(gè)文件,利用open、read、write、close實(shí)現(xiàn)文件的復(fù)制。
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    int sourceFile, destinationFile; //定義源文件和目標(biāo)文件的文件描述符
    ssize_t readBytes, writeBytes; //定義讀取和寫(xiě)入的字節(jié)數(shù)
    char buffer[1024]; //定義緩沖

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

    destinationFile = open("tsum.c", O_WRONLY | O_CREAT | O_TRUNC, 0644); //打開(kāi)目標(biāo)文件
    if (destinationFile == -1) { //檢查是否打開(kāi)成功
        perror("Error opening destination file");
        close(sourceFile); //關(guān)閉源文件
        exit(EXIT_FAILURE);
    }

    while ((readBytes = read(sourceFile, buffer, sizeof(buffer))) > 0) { //循環(huán)讀取源文件內(nèi)容
        writeBytes = write(destinationFile, buffer, readBytes); //將讀取到的內(nèi)容寫(xiě)入目標(biāo)文件
        if (writeBytes != readBytes) { //檢查寫(xiě)入是否成功
            perror("Error writing to destination file");
            close(sourceFile); //關(guān)閉源文件
            close(destinationFile); //關(guān)閉目標(biāo)文件
            exit(EXIT_FAILURE);
        }
    }

    if (readBytes == -1) { //檢查讀取是否成功
        perror("Error reading from source file");
        close(sourceFile); //關(guān)閉源文件
        close(destinationFile); //關(guān)閉目標(biāo)文件
        exit(EXIT_FAILURE);
    }

    close(sourceFile); //關(guān)閉源文件
    close(destinationFile); //關(guān)閉目標(biāo)文件

    return 0;
}
  1. 利用標(biāo)準(zhǔn)I/O操作中的fopen、fread、fwrite、fseek、fclose等函數(shù)實(shí)現(xiàn)文件顯示命令cat和cp的功能。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define BUFFER_SIZE 1024

void cat(char *filename)
{
    // 先讀取并顯示源文件
    FILE *file = fopen(filename, "r"); // 打開(kāi)文件
    if (file == NULL)
    { // 檢查是否打開(kāi)成功
        perror("Error opening file");
        exit(EXIT_FAILURE);
    }
    char buffer[BUFFER_SIZE];
    size_t readBytes;
    while ((readBytes = fread(buffer, 1, BUFFER_SIZE, file)) > 0)
    {                                         // 循環(huán)讀取文件內(nèi)容
        fwrite(buffer, 1, readBytes, stdout); // 將讀取到的內(nèi)容輸出到標(biāo)準(zhǔn)輸出
    }
    fclose(file); // 關(guān)閉文件
}

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

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

    FILE *destinationFile = fopen(destinationFileName, "w"); // 打開(kāi)目標(biāo)文件
    if (destinationFile == NULL)
    { // 檢查是否打開(kāi)成功
        perror("Error opening destination file");
        fclose(sourceFile); // 關(guān)閉源文件
        exit(EXIT_FAILURE);
    }

    char buffer[BUFFER_SIZE];
    size_t readBytes;

    while ((readBytes = fread(buffer, 1, BUFFER_SIZE, sourceFile)) > 0)
    {                                                  // 循環(huán)讀取源文件內(nèi)容
        fwrite(buffer, 1, readBytes, destinationFile); // 將讀取到的內(nèi)容寫(xiě)入目標(biāo)文件
    }

    fclose(sourceFile);      // 關(guān)閉源文件
    fclose(destinationFile); // 關(guān)閉目標(biāo)文件
}

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

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

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

    return 0;
}
  1. 讀取wav文件的文件頭,wav可以從網(wǎng)上自行下載。
#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); // 以二進(jìn)制模式打開(kāi)文件
    if (fd == -1)
    { // 檢查是否打開(kāi)成功
        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;
}

五、 實(shí)驗(yàn)總結(jié)文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-412840.html

到了這里,關(guān)于《嵌入式系統(tǒng)開(kāi)發(fā)實(shí)踐》實(shí)踐一 Linux 文件I/O操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶(hù)投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

  • 嵌入式實(shí)時(shí)操作系統(tǒng)的設(shè)計(jì)與開(kāi)發(fā)

    嵌入式實(shí)時(shí)操作系統(tǒng)的設(shè)計(jì)與開(kāi)發(fā)

    在RTOS中,時(shí)鐘具有非常重要的作用,通過(guò)時(shí)鐘可實(shí)現(xiàn)延時(shí)任務(wù)、周期性觸發(fā)任務(wù)執(zhí)行、任務(wù)有限等待的計(jì)時(shí)。 大多數(shù)嵌入式系統(tǒng)有兩種時(shí)鐘源,分別為實(shí)時(shí)時(shí)鐘RTC(Real-Time Clock)和定時(shí)器/計(jì)數(shù)器。 實(shí)時(shí)時(shí)鐘一般是靠電池供電,即使系統(tǒng)斷電,也可以維持日期和時(shí)間。由于實(shí)

    2024年02月11日
    瀏覽(23)
  • 嵌入式實(shí)時(shí)操作系統(tǒng)的設(shè)計(jì)與開(kāi)發(fā)(十)

    RTOS的引導(dǎo)是指將操作系統(tǒng)裝入內(nèi)存并開(kāi)始執(zhí)行的過(guò)程。在嵌入式系統(tǒng)的實(shí)際應(yīng)用中,針對(duì)不同應(yīng)用環(huán)境,對(duì)時(shí)間效率和空間效率有不同的要求。因此,操作系統(tǒng)啟動(dòng)時(shí)應(yīng)充分考慮這兩種限制。 時(shí)間限制主要包括兩種情況:系統(tǒng)要求快速啟動(dòng)和系統(tǒng)啟動(dòng)后要求程序能實(shí)時(shí)運(yùn)行

    2024年02月07日
    瀏覽(27)
  • 嵌入式實(shí)時(shí)操作系統(tǒng)的設(shè)計(jì)與開(kāi)發(fā)(一)

    嵌入式實(shí)時(shí)操作系統(tǒng)的設(shè)計(jì)與開(kāi)發(fā)(一)

    以一款簡(jiǎn)單、易學(xué)的嵌入式開(kāi)發(fā)平臺(tái)ARM Mini2440(CPU是三星ARM 9系列的ARM S3C2440)為例,通過(guò)具體代碼實(shí)現(xiàn),介紹如何從裸板入手設(shè)計(jì)簡(jiǎn)單的輪詢(xún)系統(tǒng)、前后臺(tái)系統(tǒng),以及如何一步一步在ARM Mini2440上編寫(xiě)RTOS內(nèi)核,到如何讓RTOS內(nèi)核支持多核嵌入式處理器。 aCoral是2009年創(chuàng)建的開(kāi)源

    2024年02月12日
    瀏覽(24)
  • 【嵌入式系統(tǒng)應(yīng)用開(kāi)發(fā)】FPGA——HLS入門(mén)實(shí)踐之led燈閃爍

    【嵌入式系統(tǒng)應(yīng)用開(kāi)發(fā)】FPGA——HLS入門(mén)實(shí)踐之led燈閃爍

    HLS(High Level Synthesis) :一款高層次綜合工具。 能夠?qū)?C/C++ 或者 system C 等高級(jí)語(yǔ)言轉(zhuǎn)化為 RTL (底層硬件描述語(yǔ)言)電路,降低開(kāi)發(fā)時(shí)間。 提供了常見(jiàn)的庫(kù)(例如圖像處理相關(guān)的 OpenCv 庫(kù)和其 它的數(shù)學(xué)庫(kù))。 可以創(chuàng)建IP并通過(guò)例化或者使用 BlockDesign 的方式應(yīng)用到項(xiàng)目中。 轉(zhuǎn)化原

    2024年02月05日
    瀏覽(29)
  • 嵌入式開(kāi)發(fā)——文件系統(tǒng)部署rz、sz命令

    嵌入式開(kāi)發(fā)——文件系統(tǒng)部署rz、sz命令

    下載網(wǎng)址:https://ohse.de/uwe/software/lrzsz.html (1)報(bào)錯(cuò)顯示在編譯sz、rz命令時(shí)還依賴(lài)libnsl.so庫(kù),當(dāng)前編譯時(shí)找不到libnsl庫(kù); (2)libnsl是Unix/Linux系統(tǒng)下的一個(gè)開(kāi)源C語(yǔ)言庫(kù),全稱(chēng)為“network services library”,提供了對(duì)許多網(wǎng)絡(luò)服務(wù)程序的網(wǎng)絡(luò)接口調(diào)用。其主要功能包括獲取主機(jī)名、網(wǎng)絡(luò)

    2024年02月07日
    瀏覽(21)
  • 嵌入式實(shí)時(shí)操作系統(tǒng)的設(shè)計(jì)與開(kāi)發(fā)(信號(hào)量學(xué)習(xí))

    除了臨界點(diǎn)機(jī)制、互斥量機(jī)制可實(shí)現(xiàn)臨界資源的互斥訪問(wèn)外,信號(hào)量(Semaphore)是另一選擇。 信號(hào)量與互斥量的區(qū)別 對(duì)于互斥量來(lái)說(shuō),主要應(yīng)用于臨界資源的互斥訪問(wèn),并且能夠有效地避免優(yōu)先級(jí)反轉(zhuǎn)問(wèn)題。 對(duì)于信號(hào)量而言,它雖然也能用于臨界資源的互斥訪問(wèn),但是不能

    2024年02月08日
    瀏覽(30)
  • 嵌入式Linux驅(qū)動(dòng)開(kāi)發(fā)系列五:Linux系統(tǒng)和HelloWorld

    嵌入式Linux驅(qū)動(dòng)開(kāi)發(fā)系列五:Linux系統(tǒng)和HelloWorld

    三個(gè)問(wèn)題 了解Hello World程序的執(zhí)行過(guò)程有什么用? 編譯和執(zhí)行:Hello World程序的執(zhí)行分為兩個(gè)主要步驟:編譯和執(zhí)行。編譯器將源代碼轉(zhuǎn)換為可執(zhí)行文件,然后計(jì)算機(jī)執(zhí)行該文件并輸出相應(yīng)的結(jié)果。了解這個(gè)過(guò)程可以幫助我們理解如何將代碼轉(zhuǎn)化為可運(yùn)行的程序。 語(yǔ)法和語(yǔ)義

    2024年02月13日
    瀏覽(26)
  • lv3 嵌入式開(kāi)發(fā)-3 linux shell命令(文件搜索、文件處理、壓縮)

    lv3 嵌入式開(kāi)發(fā)-3 linux shell命令(文件搜索、文件處理、壓縮)

    目錄 1 查看文件相關(guān)命令 1.1 常用命令 1.2 硬鏈接和軟鏈接 2 文件搜索相關(guān)命令 2.1?查找文件命令 2.2 查找文件內(nèi)容命令 2.3 其他相關(guān)命令 3 文件處理相關(guān)命令 3.1?cut? 3.2 sed 過(guò)濾 3.3 awk 匹配 4 解壓縮相關(guān)命令 4.1?解壓縮文件的意義 4.2?解壓縮相關(guān)命令 cat - concatenate files and pri

    2024年02月10日
    瀏覽(24)
  • 修改嵌入式 ARM Linux 內(nèi)核映像中的文件系統(tǒng)

    修改嵌入式 ARM Linux 內(nèi)核映像中的文件系統(tǒng)

    zImage 是編譯內(nèi)核后在 arch/arm/boot 目錄下生成的一個(gè)已經(jīng)壓縮過(guò)的內(nèi)核映像。通常我們不會(huì)使用編譯生成的原始內(nèi)核映像 vmlinux ,因其體積很大。因此, zImage 是我們最常見(jiàn)的內(nèi)核二進(jìn)制,可以直接嵌入到固件,也可以直接使用 qemu 進(jìn)行調(diào)試。當(dāng)然,在 32 位嵌入式領(lǐng)域還能見(jiàn)到

    2024年02月10日
    瀏覽(36)
  • 【正點(diǎn)原子FPGA連載】第二章 安裝Ubuntu操作系統(tǒng) 摘自【正點(diǎn)原子】DFZU2EG_4EV MPSoC之嵌入式Linux開(kāi)發(fā)指南

    【正點(diǎn)原子FPGA連載】第二章 安裝Ubuntu操作系統(tǒng) 摘自【正點(diǎn)原子】DFZU2EG_4EV MPSoC之嵌入式Linux開(kāi)發(fā)指南

    1)實(shí)驗(yàn)平臺(tái):正點(diǎn)原子RV1126 Linux開(kāi)發(fā)板 2)平臺(tái)購(gòu)買(mǎi)地址:https://detail.tmall.com/item.htm?id=692176265749 3)全套實(shí)驗(yàn)源碼+手冊(cè)+視頻下載地址: http://www.openedv.com/thread-340252-1-1.html 前面虛擬機(jī)已經(jīng)創(chuàng)建成功了,相當(dāng)于硬件已經(jīng)準(zhǔn)備好了,接下來(lái)就是要在虛擬機(jī)中安裝Ubuntu系統(tǒng)了,首先

    2023年04月26日
    瀏覽(28)

覺(jué)得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包