和菜鳥一起深入學習國嵌實驗之檔案編程

來源:互聯網
上載者:User

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$ makegcc -c  file_create.c -ofile_create.ogcc  -o file_createfile_create.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$ lsfile_create file_create.c  file_create.o  Makefile eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$./file_create file_crecreate file file_cre success! eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$ ll鎬葷敤閲?40drwxr-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$ makegcc -c  file_cp.c -o file_cp.ogcc  -o file_cp file_cp.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$./file_cp  file_cp.c file_test.cfrom file size is 1241 eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.5$ ll鎬葷敤閲?44drwxr-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$makegcc-c  time.c -o time.ogcc  -o time time.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.7$lsMakefile  time time.c  time.o eastmoon@eastmoon-virtual-machine:~/work/guoqian/1/1.7$./timeTueJan 29 19:45:46 2013 TueJan 29 19:45:46 2013 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.