zoukankan      html  css  js  c++  java
  • 和菜鸟一起深入学习国嵌实验之文件编程

    1、  文件创建

    代码:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <fcntl.h>
    #include <sys/types.h>
    #include <sys/stat.h>
     
    void create_file(char *filename)
    {
        //create a file
        if(creat(filename, 0666)< 0)
        {
            printf("createfile %s failure\n", filename);
            return;
        }
        else
        {
            printf("createfile %s success!\n", filename);
        }
    }
     
     
    int main(int argc, char *argv[])
    {
        if(argc < 2)
        {
            printf("youhave't input the filename, please try again\n");
            exit(1);
        }
     
        create_file(argv[1]);
     
        return 0;
    }
     

    Makefile:

    CC = gcc
     
    CURTDIR = $(shell pwd)
    TARGET = file_create
     
    %.o:%.c
    $(CC) -c $(EXTRAFLAGS) $<-o $@
    %.o:%.S
    $(CC) -c $(EXTRAFLAGS) $<-o $@
     
    .PHONY: all clean
     
    $(TARGET): $(TARGET).o
    $(CC)  -o $@ $^
     
    clean:
    rm -rf $(TARGET) $(TARGET).o
     

     

    运行结果:

    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$ make
    gcc -c  file_create.c -ofile_create.o
    gcc  -o file_createfile_create.o
     
    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$ ls
    file_create file_create.c  file_create.o  Makefile
     
    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$./file_create file_cre
    create file file_cre success!
     
    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$ ll
    鎬荤敤閲?40
    drwxr-xr-x 2 eastmoon eastmoon 4096 2013-01-29 19:39 ./
    drwxr-xr-x 8 eastmoon eastmoon 4096 2013-01-29 19:29 ../
    -rw-r--r-- 1 eastmoon eastmoon    0 2013-01-29 19:39 file_cre
    -rwxr-xr-x 1 eastmoon eastmoon 7232 2013-01-29 19:38 file_create*
    -rw-r--r-- 1 eastmoon eastmoon  571 2013-01-29 19:38 file_create.c
    -rw-r--r-- 1 eastmoon eastmoon 1204 2013-01-29 19:38 file_create.o
    -rw------- 1 eastmoon eastmoon 12288 2013-01-29 19:39 .file.txt.swp
    -rw-r--r-- 1 eastmoon eastmoon  237 2013-01-29 19:31 Makefile
     

     

    2、文件拷贝

    代码:

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
     
    #define BUFFER_SIZE 1024
     
    int main(int argc, char *argv[])
    {
        FILE *from_fd;
        FILE *to_fd;
        long file_len = 0;
        char buffer[BUFFER_SIZE];
        char *ptr;
     
        if(argc != 3)
        {
            printf("Usage: %s form file tofile\n", argv[0]);
            exit(1);
        }
       
        //open source file
        if((from_fd =fopen(argv[1], "rb")) == NULL)
        {
            printf("Open %sError\n", argv[1]);
            exit(1);
        }
     
        //create dest file
        if((to_fd = fopen(argv[2],"wb")) == NULL)
        {
            printf("Open %sError\n", argv[1]);
            exit(1);
        }
     
        //test the file size
        fseek(from_fd, 0L,SEEK_END);
        file_len = ftell(from_fd);
        fseek(from_fd, 0L,SEEK_SET);
       
        printf("from filesize is %ld\n", file_len);
     
        //copy the file
        while(!feof(from_fd))
        {
            fread(buffer,BUFFER_SIZE, 1, from_fd);
            if(BUFFER_SIZE >=file_len)
            {
                fwrite(buffer,file_len, 1, to_fd);
            }
            else
            {
                fwrite(buffer,BUFFER_SIZE, 1, to_fd);
                file_len =file_len - BUFFER_SIZE;
            }
            bzero(buffer,BUFFER_SIZE);
        }
        fclose(from_fd);
        fclose(to_fd);
     
        return 0;
    }

     

    Makefile

    CC = gcc
     
    CURTDIR = $(shell pwd)
    TARGET = file_cp
     
    %.o:%.c
    $(CC) -c $(EXTRAFLAGS) $<-o $@
    %.o:%.S
    $(CC) -c $(EXTRAFLAGS) $<-o $@
     
    .PHONY: all clean
     
    $(TARGET): $(TARGET).o
    $(CC)  -o $@ $^
     
    clean:
    rm -rf $(TARGET) $(TARGET).o
     

     

    运行结果:

    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$ make
    gcc -c  file_cp.c -o file_cp.o
    gcc  -o file_cp file_cp.o
     
    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$./file_cp  file_cp.c file_test.c
    from file size is 1241
     
    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$ ll
    鎬荤敤閲?44
    drwxr-xr-x 2 eastmoon eastmoon 4096 2013-01-29 19:28 ./
    drwxr-xr-x 7 eastmoon eastmoon 4096 2013-01-29 19:11 ../
    -rwxr-xr-x 1 eastmoon eastmoon 7472 2013-01-29 19:27 file_cp*
    -rw-r--r-- 1 eastmoon eastmoon 1241 2013-01-29 19:26 file_cp.c
    -rw-r--r-- 1 eastmoon eastmoon 1868 2013-01-29 19:27 file_cp.o
    -rw-r--r-- 1 eastmoon eastmoon 1241 2013-01-29 19:28 file_test.c
    -rw------- 1 eastmoon eastmoon 12288 2013-01-29 19:28 .file.txt.swp
    -rw-r--r-- 1 eastmoon eastmoon  233 2013-01-29 19:27 Makefile

     

     

    3、  获取本地时间

    代码:

    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    #include<fcntl.h>
    #include<sys/types.h>
    #include<sys/stat.h>
    #include<time.h>
     
    intmain(int argc, char *argv[])
    {
        struct tm *ptr;
        time_t lt;
     
        //get current time
        lt = time(NULL);
     
        //turn to the native time
        ptr = localtime(<);
     
        //print the time as native time
        printf("%s\n", asctime(ptr));
     
        printf("%s\n", ctime(<));
     
        return 0;
    }
     

    Makefile

    CC =gcc
     
    CURTDIR= $(shell pwd)
    TARGET= time
     
    %.o:%.c
           $(CC) -c $(EXTRAFLAGS) $< -o $@
    %.o:%.S
           $(CC) -c $(EXTRAFLAGS) $< -o $@
     
    .PHONY:all clean
     
    $(TARGET):$(TARGET).o
           $(CC) -o $@ $^
     
    clean:
           rm -rf $(TARGET) $(TARGET).o
     

     

    运行结果:

    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.7$make
    gcc-c  time.c -o time.o
    gcc  -o time time.o
     
    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.7$ls
    Makefile  time time.c  time.o
     
    eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.7$./time
    TueJan 29 19:45:46 2013
     
    TueJan 29 19:45:46 2013
     

     


  • 相关阅读:
    前端技术-PS切图
    Html5资料整理
    Html5知识体系
    Html知识体系
    C语言知识结构
    ASP.NET知识结构
    src和href的区别
    Ajax的简单使用
    学习理论
    求模运算法则
  • 原文地址:https://www.cnblogs.com/wuyida/p/6300023.html
Copyright © 2011-2022 走看看