Work with cainiao to go deep into the file programming of the National embedded experiment

Source: Internet
Author: User

1. File Creation

Code:

#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 

 

Running result:

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_re success! Eastmoon @ eastmoon-Virtual-machine :~ /Work/guoqian/1/1.5 $ ll have been interrupted? 40drwxr-xr-x 2 eastmoon 4096. /drwxr-XR-x 8 eastmoon 4096 .. /-RW-r -- 1 eastmoon 0 2013-01-29 file_cre-rwxr-xr-x 1 eastmoon 7232 file_create *-RW-r -- 1 eastmoon 571 file_create.c-rw-r -- r -- 1 eastmoon 1204 file_create.o-rw ------- 1 eastmoon 12288 .file.txt. SWP-RW-r -- 1 eastmoon 237 makefile

 

2. file copy

Code:

#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 

 

Running result:

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 have been interrupted? 44drwxr-xr-x 2 eastmoon 4096. /drwxr-XR-x 7 eastmoon 4096 .. /-rwxr-XR-x 1 eastmoon 7472 file_cp *-RW-r -- 1 eastmoon 1241 file_cp.c-rw-r -- r -- 1 eastmoon 1868 file_cp.o-rw-r -- r -- 1 eastmoon 1241 file_test.c-rw ------- 1 eastmoon 12288 .file.txt. SWP-RW-r -- 1 eastmoon 233 makefile

 

 

3. Obtain the local time

Code:

#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 

 

Running result:

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 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.