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