找回密码
 注册
搜索
查看: 791|回复: 0

[讨论] 嵌入式Linux系统编程常见问题解答(一)

[复制链接]
发表于 2013-12-5 11:07:05 | 显示全部楼层 |阅读模式


1:使用用基于文件指针的文件操作函数,实现把文本文件a.txt中的内容复制到b.txt中,b.txt中原来的内容被覆盖。
答案:
#include <stdio.h>
int main()
{
    FILE *fpa = fopen("a.txt", "rb");
    FILE *fpb = fopen("b.txt", "wb");
    char ch;
    while((ch = fgetc(fpa)) != EOF)
    {
        fputc(ch, fpb);
    }
    fclose(fpa);
    fclose(fpb);
    return 0;
}

2:用基于文件描述符的文件操作函数,实现自己的简单的cp命令:mycp, 命令格式如:./mycp a.txt b.txt,把a.txt复制为b.txt。 要能实现单个文件的复制,以及从指定路径复制文件到指定路径中。
答案:
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
int main(int argc, char *argv[])
{
    char buf[512] = {0};
    int fo1 = open(argv[1], O_RDONLY);
    int fo2 = open(argv[2], O_WRONLY | O_CREAT | O_EXCL, 0755);
    if(fo2 == -1)
    {
        printf("error! file exist!\n");
        exit(0);
    }
    int fr = 0;
    /*开始复制*/
    while( (fr = read(fo1, buf, sizeof(buf))) > 0 )
    {
        write(fo2, buf, fr);
    }
    close(fo1);
    close(fo2);
    return 0;
}

3:从命令行传入某个.c或.txt文件的文件名,实现以下功能。
1):对该文件的每个字符做如下变换:如果是大写字母就变为小写,如果是小写字母就变为大写,并写回原文件,其它字符不变。
2):输出该文件的大小,文件类型,链接数,最后修改时间。
3):测试当前用户对该文件的权限(读、写、执行)。
要求:模块化编程,用函数实现不同的功能模块,再通过函数指针调用相关函数。
主函数只做:打开文件,调用函数,关闭文件。
另外,对文件读写之前,要加锁,读写完毕后,再解锁。
答案:
/*源代码*/
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
/*大小写互相转换*/
void zhuanhuan(int fd)
{
        char c;
        struct flock lock = {F_WRLCK, SEEK_SET, 0, 0, getpid()};

        if(-1 == fcntl(fd, F_SETLK, &lock))
        {
                perror("lock failed!\n");
                exit(-1);
        }
        while((read(fd, &c, sizeof(char)) > 0))
        {
                if(c >= 'A'&& c <= 'Z')
                        c = c + 'a' - 'A';
                else if(c >= 'a' && c <= 'z')
                        c = c - 32;
                else
                        continue;
                lseek(fd, -1, SEEK_CUR);
                write(fd, &c, sizeof(char));
        }
        lock.l_type = F_UNLCK;
        if(-1 == fcntl(fd, F_SETLK, &lock))
        {
                perror("unlock failed!\n");
                exit(-1);
        }

}
/*检测权限*/
void quanxian(char *filename)
{
        if(!access(filename, F_OK))
        {
                if(!access(filename, R_OK))
                        printf("r");
                else
                        printf("-");
                if(!access(filename, W_OK))
                        printf("w");
                else
                        printf("-");
                if(!access(filename, X_OK))
                        printf("x");
                else
                        printf("-");
        }
        else
                printf("file not exist!\n");
}
/*获取文件信息*/
void xinxi(int fd)
{
        struct stat a;
        if(-1 == fstat(fd, &a))
        {
                perror("fstat error");
                exit(-1);
        }

        if(S_ISREG(a.st_mode))
                printf("-");
        else if(S_ISDIR(a.st_mode))
                printf("d");
        else if(S_ISLNK(a.st_mode))
                printf("l");
        else if(S_ISCHR(a.st_mode))
                printf("c");
        else if(S_ISBLK(a.st_mode))
                printf("b");
        else if(S_ISFIFO(a.st_mode))
                printf("p");
        else if(S_ISSOCK(a.st_mode))
                printf("s");
        printf(" %d", a.st_nlink);
        printf(" %d", a.st_size);

        struct tm *pTm;
        pTm = gmtime(&a.st_mtime);
        printf(" %04d-%02d-%02d",(1900 + pTm->tm_year), (1 + pTm->tm_mon), (pTm->tm_mday));
        printf(" %02d:%02d:%02d\n",(8 + pTm->tm_hour), (pTm->tm_min), (pTm->tm_sec));
}
/*该函数调用一系列函数实现各个功能*/
void diaoyong(int fd, char *filename)
{
        /*定义函数指针*/
        void (*pfun1)(int);       
        void (*pfun2)(char *);
        void (*pfun3)(int);

        pfun1 = zhuanhuan;
        pfun2 = quanxian;
        pfun3 = xinxi;

        pfun1(fd);
        pfun2(filename);
        pfun3(fd);
}

int main(int a, char *b[])
{
        int fd = open(b[1], O_RDWR);
        if(-1 == fd)
        {
                perror("open failed!\n");
                exit(-1);
        }

        diaoyong(fd, b[1]);

        close(fd);
        return 0;
}

4:先用fork函数创建一个子进程,然后父进程从键盘输入一个字符串,在子进程中输出这个字符串。编写程序过程中,注意避免造成孤儿进程和僵尸进程。要求用之前学的知识去实现。
答案:
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main()
{
    char buf[50] = {0};
    int pid = fork();
    if(pid > 0)
    {
        printf("父进程:Enter a string:");
        scanf("%s", buf);
        int fd = open("a.txt",O_WRONLY|O_CREAT);
        write(fd, buf, sizeof(buf));//父进程把字符串写入文件
                close(fd);
        waitpid(pid, NULL, 0);
    }       
    else
    {
        sleep(5);
        int fd = open("a.txt",O_RDONLY);
        read(fd, buf, sizeof(buf));//子进程可以从该文件中读出该字符串.
        close(fd);
        printf("子进程:The string is:%s\n", buf);
    }
    return 0;
}

5:写一个显示时间的程序,终端下动态显示时间,并且时间在走。
答案:
#include <stdio.h>
#include <time.h>
int main()
{
        time_t n;
        struct tm *p;
        while(1)
        {
                system("clear");
                time(&n);//获取日历时间
                p = localtime(&n);//转换为本地时间
                printf("%4d-%d-%d:%d:%d:%d\n", 1900+p->tm_year, 1+p->tm_mon, p->tm_mday, p->tm_hour, p->tm_min, p->tm_sec);
                sleep(1);
        }
        return 0;
}专业嵌入式技术实训,咨询郭老师QQ754634522
高级模式
B Color Image Link Quote Code Smilies

本版积分规则

Archiver|手机版|小黑屋|52RD我爱研发网 ( 沪ICP备2022007804号-2 )

GMT+8, 2024-5-20 20:56 , Processed in 0.049699 second(s), 17 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表