the linux fwrite () method using a linux fwrite () method using

turn

the linux fwrite () method using

fwrite function

1. Function Function

For reading and writing a block of data.

2. General calls form

fwrite(buffer,size,count,fp);

3. Description

(1) buffer: is a pointer to fread, it is read into the data storage address. For fwrite, the address data is to be output.

Number of bytes to read and write;: (2) size

(3) count: the number of read and write to size bytes of data items;

(4) fp: a pointer file type

   This is an example of working with fwrite function to write, not only to record their own learning, but also to share with our friends the fwrite function instance.

 

Fwrite this example is a program written text of the current time, the following method using function fwrite.
markfile int (void)
{
    the FILE * SP;
// char BUFF [512];
    char COUNT = 0;
    char * currentime = NULL;
    char * wday [] = { "the Sun", "Mon", "Tue", "Wed "," Thu "," Fri "," Sat "};
    time_t timep;
    struct (TM) * P;
    Time (& timep);
    P = localtime (& timep);
    currentime = the ctime (& timep);
// Memset (BUFF, 0, 512) ;
// sprintf (BUFF, "% S", currentime);
    the printf ( "% D / D% / D%", (p-1900 +> tm_year), (. 1 + p-> tm_mon), p-> tm_mday );
    the printf ( "% S% D:% D: D% \ n-", wday [p-> tm_wday], p-> tm_hour, p-> tm_min, p-> tm_sec);


    fwrite(currentime,size(currentime)-1,1,sp);
    fclose(sp);
    return 1;
}

Effect size (currentime) (= 26) is the number of stars string pointer currentime referred to (including '\ 0'), but the distortion is output (the number of the string should be too long), and therefore, I put a number to lose, you will be able to comfort the correct result.

用另外一种方法:
int markfile(void )
{
    FILE *sp ;
    char buff[512] ;
    char count = 0;
    char *currentime = NULL;
    char *wday[] = {"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
    time_t timep;
    struct tm *p;
    time(&timep);
    p = localtime(&timep);
    currentime = ctime(&timep);
//    memset(buff,0,512);
    sprintf(buff,"%s",currentime);
    printf("%d/%d/%d",(1900+p->tm_year),(1+p->tm_mon),p->tm_mday);
    printf(" %s %d:%d:%d\n",wday[p->tm_wday],p->tm_hour,p->tm_min,p->tm_sec);
    if((sp = fopen("/root/kay/mark.txt","a+")) == NULL)
       return 0;
    fwrite (currentime, (COUNT = strlen (BUFF)),. 1, SP);
    the printf ( "% D \ n-", COUNT);
    fclose (SP);
    return. 1;
}
COUNT = strlen (BUFF) to obtain characters The result is the number 25,

Comparison on the Comprehensive think fwrite () character output do not include terminator ( '\ 0'), or because of the number of characters more and cause garbled

    After reading friend, that you have to help the top one, where if you feel good, you can publish under opinion, learning together.

Guess you like

Origin blog.csdn.net/wu694128/article/details/91384328