开发者

Replace line in text-file using C

开发者 https://www.devze.com 2023-04-12 15:34 出处:网络
I want to change lines which contain the # symbol in a text file with heet using C. I have tried it this way, but it did not work thoroughly, it just replaces the characters & overwrites not the

I want to change lines which contain the # symbol in a text file with heet using C.

I have tried it this way, but it did not work thoroughly, it just replaces the characters & overwrites not the whole string, like I want.

Is there any other trick to remove or delete a whole line from the file? So, we can easily replace it.

myfile.txt: (before execution)

Joy
#Smith
Lee
Sara#
Priyanka
#Addy

Code:

#include <stdio.h>
#include <string.h>

int main() {
    FILE *pFile;
    fpos_t pos1, pos2;
    int line = 0;
    char buf[68]
    char *p;
    char temp[10] = "heet";

    pFile = fopen("myfile.txt", "r+");

    printf("changes are made in this lines:\t");    
    while (!feof(pFile)) {
        ++line;
        fgetpos(pFile, &pos1);          

        if (fgets(buf, 68, pFile) == NULL)  
            break;

        fgetpos(pFile, &pos2);

        p = strchr(buf, '#');

        if (p != NULL) {
            printf("%d, " , line);
            fsetpos(pFile, 开发者_C百科&pos1);  
            fputs(temp, pFile);
        }
        fsetpos(pFile, &pos2);
    }

    fclose(pFile);
    return 0;
}

myfile.txt: (after execution)

Joy  
heetth  
Lee  
heet#  
Priyanka  
heety  

Output:

changes are made in this lines: 2, 4, 6,  

myfile.txt: (I want to get)

Joy  
heet  
Lee  
heet  
Priyanka  
heet  


The best way of doing what you want is to use a utility like sed. It is faster and uses less memory than anything you (or I) would write.

That aside, let's assume you want to go ahead and write it yourself anyway.

A file is just like a long array of bytes. If you want to increase or decrease the length of one line, it affects the position of every byte in the rest of the file. The result can be shorter (or longer) than the original. As the result can be shorter, modifying the file in place is a bad idea.

The following pseudo-code illustrates a simple approach:

open original file
open output file
allocate a line buffer that is large enough
read a line from the original file
do
  return an error if the buffer is too small
  manipulate the line
  write the manipulated line to the output file
  read a line from the original file
loop until read returns nothing

sed does it much smarter. I once saw an explanation on how sed works, but my google karma can't seem to find it.

Edit: How to do it using sed:

 sed -e 's/.*\#.*/heet/g' myfile.txt

The s, or substitute, command of sed can replace one string, or regular expression, with another string.

The above command is interpreted as:

replace any line that has a # somewhere in it with heet. The final g tells sed to do this globally, i.e. in the entire file.

Edit2: By default, sed writes to standard output. To rewrite the file you should redirect the output to a file and then rename it. In linux, do the following (you can run command line stuff from C with system):

sed -e 's/.*\#.*/heet/g' myfile.txt > temp_file123.txt
rm myfile.txt
mv temp_file123.txt myfile.txt

From C:

system("sed -e 's/.*\#.*/heet/g' myfile.txt > temp_file123.txt");
system("rm myfile.txt");
system("mv temp_file123.txt myfile.txt");

If you want to do it with just one call to system, put all the command line stuff in a shell script.


You should probably treat input/output like a UNIX utility and replace the line by reading in the whole input and writing the whole output like sed would or something. It's going to be a pain to edit the line in place as you need to shift the following text 'down' in order to make it work.


You cannot achieve your goal by overwriting the file in place like you do in the code because heet is 3 bytes longer than # and there is no standard function to insert bytes in the middle of a file.

Note also these important issues:

  • you do not test if fopen() succeeds at opening the file. You have undefined behavior if the file does not exist or cannot be open for read+update mode.
  • while (!feof(pFile)) does not stop exactly at the end of file because the end of file indicator returned by feof() is only set when a read operation fails, not before. You should instead write:

        while (fgets(buf, 68, pFile) != NULL) {
    
  • if the file has lines longer than 66 characters, the line numbers will be computed incorrectly.

There are 2 ways to replace the text in the file:

  • you can create a temporary file and write the modified contents to it. Once the contents have all been converted, delete the original file with remove() and rename the temporary file to the original name with rename(). This method uses extra space on the storage device, and requires that you can create a new file and determine a file name that does not conflict with existing file names.
  • alternately, you can read the complete contents of the original file and overwrite it with the modified contents from the start. This works because the modified contents is longer than the original contents. This method may fail if the file is very large and does not fit in memory, which is rather rare today for regular text files.

Here is a modified version using the second approach:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

int main() {
    FILE *pFile;
    int c, line, changes;
    unsigned char *buf;
    size_t pos, length, size;
    char replacement[] = "heet";

    /* open the file */
    pFile = fopen("myfile.txt", "r+");
    if (pFile == NULL) {
        printf("cannot open myfile.txt\n");
        return 1;
    }

    /* read the file */
    buf = NULL;
    length = size = 0;
    while ((c = getc(pFile)) != EOF) {
        if (length == size) {
            size = size + size / 2 + 128;
            buf = realloc(buf, size);
            if (buf == NULL) {
                printf("not enough memory to read myfile.txt\n");
                fclose(pFile);
                return 1;
            }
        }
        buf[length++] = c;
    }
    /* write the modified contents */
    rewind(pFile);
    line = 1;
    changes = 0;
    for (pos = 0; pos < length; pos++) {
        c = buf[pos];
        if (c == '\n')
            line++;
        if (c == '#') {
            if (changes++ == 0)
                printf("changes are made in this lines:\t");
            else
                printf(", ");
            printf("%d", line);
            fputs(replacement, pFile);
        } else {
            putc(c, pFile);
        }
    }
    free(buf);
    fclose(pFile);
    if (changes == 0)
        printf("no changes were made\n");
    else
        printf("\n");
    return 0;
}


To rewrite a word in file using fwrite or any file writing function, use fgetpos and fsetpos. Otherwise seeking file pointer alone will not work. Still this work, if the file pointer is end of the file, it means append is possible.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号