For example, the file 1.txt contains the information "123456789 * *" How to remove asterisks? At the end should be "123456789".

How to edit a 1.txt file correctly without creating another file?

    4 answers 4

    Open two streams (one read, another write (on update)) to the same file. Overwrite characters not equal to '*', then trim the file.

    #include <stdio.h> #include <stdlib.h> #include <errno.h> main (int ac, char *av[]) { if (ac < 2) { printf ("No filename\n"); exit(1); } int i = 0, c; FILE *in = fopen(av[1],"r"), *out = fopen(av[1],"r+"); if (in == NULL || out == NULL) { perror (av[1]); exit(2); } rewind(out); while ((c = fgetc(in)) != EOF) { if (c != '*') { i++; fputc(c,out); } } fclose(in); ftruncate(fileno(out),i); fclose(out); } 
    • Dear avp, please write for a particularly slow-witted program immediately ready for assembly. otherwise I'm confused in FILE * in = fopen (av [1], "r"), * out = fopen (av [1], "r +"); and nevier how ftruncate works ((( - alexl1720p
    • The program was compiled by gcc in MinGW and Interix. FILE * in = fopen ("file name", "r") opens binds the in variable to the stream for reading. FILE * out = (..., "r +") is similar for writing to the same file. "r +" actually opens the file for both reading and writing without cutting it open. In some implementations, seek () (rewind ()) is required for writing operations. The variable i counts the number of bytes that are written to the file (not *). ftruncate () (see man 2 ftruncate) cuts the file to the desired size. It requires a file descriptor, we get it by fileno (out). Everything ! - avp

    To edit a text file that requires changing its size, there is no other way to create a new text file. Specifically for the case with asterisks, you can open the file in read-write mode, read each character and if it is an asterisk, rewrite it with some unreadable character.

    • "To edit a text file that requires changing its size, there is no other way how to create a new text file" ... Khrenova ((( - alexl1720p
    • "To edit a text file that requires changing its size, there is no other way to create a new text file" —Well, or read the file into the buffer = volume of the original file, bang the old file and create a new one or overwrite the old buffer completely. - gecube
    • one
      Well, in this case, you only need to delete the bytes. Therefore, the write buffer m. small and how to slide through the file after the read buffer. - avp

    like this:

     char s[1111]; //исходная строка char ans[1111]; // полученная строка int c = 0; // количество символов в полученной строке //Вставьте код: открытие 1.txt на чтение scanf("%s", &s); for (int i = 0; i < strlen(s); i++) if (s[i] != '*') ans[c++] = s[i]; //Вставьте код: закрытие файла 1.txt //Вставьте код: открытие 1.txt на запись printf("%s", ans); //Вставьте код: закрытие файла 1.txt. 
    • Need a program to work with all txt files, different sizes ... For example, with a size of 1 GB or more. This code will not work for you - alexl1720p
     #include <iostream> #include <fstream> using namespace std; int main() { ifstream f("file.txt"); ofstream f0("file.txt"); char s[10]; f >> s; f0 << s; return EXIT_SUCCESS; }