一、 實(shí)驗(yàn)?zāi)康?/p>
- 掌握函數(shù)stat中文件屬性的應(yīng)用;
- 掌握系統(tǒng)函數(shù)system、opendir等的使用;
- 掌握struct dirent的結(jié)構(gòu)體變量的使用方法;
- 掌握文件屬性的判斷;
- 掌握系統(tǒng)函數(shù)open、read、write、close的使用方法。
- 掌握設(shè)備操作的系統(tǒng)函數(shù)使用方法。
二、 實(shí)驗(yàn)任務(wù)與要求
- 測(cè)試文件S_IRUSR、S_IWUSR、S_IRGRP、S_IROTH屬性;
- 應(yīng)用readdir函數(shù)顯示文件和子目錄;
- 文件屬性的判斷;
- 應(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é)果
- 設(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文件。文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-412840.html
#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");
}
// ... 剩下的可以自行拓展
}
- 應(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;
}
- 在當(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;
}
- 利用標(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;
}
- 讀取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)!