Linux fwrite () use method, the current time to write the program of the text

Source: Internet
Author: User
Tags sprintf

fwrite function

1. function function

Used to read and write a data block.

2. General Invocation Form

Fwrite (BUFFER,SIZE,COUNT,FP);

3. Description

(1) Buffer: is a pointer to Fread, which is the storage address of the read-in data. For Fwrite, the address of the data to be output.

(2) Size: The number of bytes to read and write;

(3) Count: The number of bytes of data to read and write;

(4) FP: file-Type pointer

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

This fwrite instance is the program that writes the text at the current time, and this is how the Fwrite function is used.
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,size (currentime) -1,1,sp);
Fclose (SP);
return 1;
}

The function of size (currentime) (= 26) is to derive the number of strings that the pointer currentime refers to (including ' m '), but the output is garbled (it should be the number of strings is too long), so I can lose one of the numbers, and I will be able to comfortably correct the result.

In a different way:
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);
printf ("%d\n", count);
Fclose (SP);
return 1;
}
Count = strlen (buff) to get the number of characters result is 25,

In contrast, the fwrite () output character does not include The Terminator (' s '), otherwise it will be garbled because of the number of characters

Read the friend, feel that you have help on the top one, if you feel bad, you can publish the views, learn together.

Linux fwrite () use method, the current time to write the program of the text

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.