Strategies for modifying text files in Linux 0.11

Source: Internet
Author: User
Tags final strlen

Now, let's say that Hello.txt is a file already on your hard disk, and it's "Hello, World," and after the current pointer is set, we'll introduce Sys_read, Sys_write, sys_lseek How to use it together to insert data into the hell In O.txt.

They can be combined and applied in the following ways, and the application code is as follows:

#include <fcntl.h>  
#include <stdio.h>  
#include <string.h>  
      
#define LOCATION 6  
      
int Main (char argc, char **argv)  
{  
    char str1[] = "Linux";  
    Char str2[1024];  
    int fd, size;  
          
    memset (str2, 0, sizeof (STR2));  
    FD = open ("Hello.txt", O_rdwr, 0644);  
    Lseek (FD, LOCATION, seek_set);  
    strcpy (str2, str1);  
    size = Read (FD, str2+5, 6);  
          
    Lseek (FD, LOCATION, seek_set);  
    Size = Write (FD, STR2, strlen (str2));  
          
    Close (FD);  
    return (0);  
}

This article URL address: http://www.bianceng.cn/OS/Linux/201410/45411.htm

This is a user process program, through such a piece of code can be "Linux" this string into the Hello.txt file, the final Hello.txt file content should be: "Hello,linuxworld."

This code uses almost all of the system calls that manipulate text files, and below we analyze the role of the Code.

FD = open ("Hello.txt", O_rdwr, 0644);

The Open function corresponds to the Sys_open function, and it is obvious that you have to decide which file to manipulate before you do it.

Lseek (FD, LOCATION, Seek_set);

The Lseek function corresponds to the Sys_lseek function, which indicates that the current action pointer of the file is offset from the beginning of the file to 6 bytes from the end of the file, because Seek_set is selected in the parameter.

strcpy (str2, str1);

This line is where you copy the "Linux" string to the beginning of the array str2[1024.

size = Read (FD, str2+5, 6);

This line to achieve the stitching, stitching the result is: LinuxWorld

Lseek (FD, LOCATION, Seek_set);

The effect of this line is the same as the previous call, which is to tell the file's current operation pointer, which is the starting position of the file, 6 bytes to the end of the file, which determines the exact write location of the following file.

Size = Write (FD, STR2, strlen (str2));

The Write function corresponds to the Sys_write function, which is now written to the Hello.txt file with the "LinuxWorld" string in the str2 array, and the write location is just determined to offset the six byte position from the beginning of the file to the tail end. So the final writing result is: "Hello,linuxworld"

The above mentioned, is read, write, lseek combination of applications, so as to achieve the whole process of file modification.

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.