Home  >  Article  >  Operation and Maintenance  >  How to write files in linux

How to write files in linux

尚
Original
2019-12-30 10:31:384559browse

How to write files in linux

How to write files in Linux:

1. Use fwrite function

Function function

Use to read and write a data block.

General calling form

fwrite(buffer,size,count,fp);

Explanation

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

(2) size: the number of bytes to be read and written;

(3) count: how many size bytes of data items to be read and written;

( 4) fp: file pointer

Example:

A program that writes the current time into text.

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;
}

2. Use the echo command

The command format is as follows:

[root@localhost ~]# echo [选项] [输出内容]

Options:

-e: Supports character conversion controlled by backslash (see Table 1 for details)

-n: Cancel the newline symbol at the end of the line after output (no line break after the content is output)

Example:

echo 'i love u' >a.txt  *在a.txt这个文件中输入i love u,如果没有这个文件则创建。如果有这个文件,那么
新内容代替原来的内容。

Recommended learning: linux tutorial

The above is the detailed content of How to write files in linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn