1

Here My text file which includes ips :

168.897.61.23
168.32.74.11
127.0.0.1
127.0.0.1

here my bad code (sorry i'm newbie) :

#include <stdio.h>
int main(){
    FILE *fptr1;
    char file1[] ="test.txt";
    char curr;
    int del, line_number = 0;
    filet = fopen(file1,"r+");
    curr = getc(filet);
    if(curr!=EOF) {line_number =1;}
    while(1){
      if(del != line_number)
        putc(curr, filet);
        curr = getc(filet);
        if(curr =='\n') line_number++;
        if(curr == EOF) break;
    }
    fclose(filet);
}

I want working code to output my file like this :

168.897.61.23
168.32.74.11
127.0.0.1

Thanks.

Halano Siblee
  • 29
  • 1
  • 3
  • Seek to the end of the file. Read backwards one character at a time, ignoring any "leading" white-space. Once you hit a newline you know its position. Then either use a system-call to set the end of the file at that position (if available) or read the rest of the file (from the beginning) to the position of the last newline and save it to a new temporary file, and rename the temporary file as the original file. – Some programmer dude Sep 17 '21 at 15:53
  • 1
    where do you assign a value to `del`? – Chris Turner Sep 17 '21 at 15:53
  • Did you noticed that ```del``` is uninitialized ? – IvanDi Sep 17 '21 at 16:00
  • 1
    `if(curr == EOF)` a `char` cannot hold `EOF` You should use proper type to store return value of `getc`. – Gerhardh Sep 17 '21 at 16:06
  • The indentation is weird. After the undefined behavior at `if(del != line_number)`, do you expect all 4 indented lines to be part of the `if` branch? – William Pursell Sep 17 '21 at 16:13
  • Do you want to delete the last line, or a particular line? The code seems to indicate the latter. – William Pursell Sep 17 '21 at 16:22
  • Sorry for the errors @WilliamPursell i want to delete the last line and the output in the same text file . – Halano Siblee Sep 17 '21 at 16:23
  • Do you mean the last '\n'? Because the lack of newline turns the '\n' into a *delimitor* instead of a *terminator*, which is not POSIX standard and can cause problems. (Newer versions of gcc will warn you about that.) See [this question](https://stackoverflow.com/questions/729692/why-should-text-files-end-with-a-newline). – Neil Sep 17 '21 at 16:48
  • You don't actually want to do any writing at all. Since you're deleting the last line, you just want to call `truncate`. The only hard part is reading the file to determine the correct offset. – William Pursell Sep 17 '21 at 17:09
  • eg: https://github.com/wrp/examples/blob/main/c/truncate.c – William Pursell Sep 17 '21 at 18:16
  • @WilliamPursell finally William bravo man that is amazing work fast into the point and thanks for the help. – Halano Siblee Sep 17 '21 at 18:43
  • @WilliamPursell i fixed fseeko and ftello ??? but the problem is **truncate keyword** GCC : `warning: implicit declaration of function 'truncate' [-Wimplicit-function-declaration] if( (offset = ftell(in)) == -1 || truncate(path, offset) != 0 ){` – Halano Siblee Sep 17 '21 at 18:59
  • @HalanoSiblee You need to include `unistd.h` – William Pursell Sep 17 '21 at 19:12
  • @WilliamPursell oh truncate.c work on linux Can you make truncate.c example for Windows too ! – Halano Siblee Oct 08 '21 at 11:46

1 Answers1

1

Open two distinct files: one for input, another for output

FILE *inputfile = fopen("text.txt", "r");
FILE *outputfile = fopen("text-out.txt", "w");
char line1[1000], line2[1000]; // 1000 chars is enough
fgets(line1, sizeof line1, inputfile); // read 1st line and hold on to it

while (fgets(line2, sizeof line2, inputfile)) { // read 2nd (3rd, 4th, ...) line
    fprintf(outputfile, "%s", line1); // print 1st (2nd, 3rd, ...) line
    strcpy(line1, line2); // copy line
}
// last line disappears
fclose(outputfile);
fclose(inputfile);

You may now delete the input file and rename the output file if you want to.

remove("text.txt");
rename("text-out.txt", "text.txt");
pmg
  • 106,608
  • 13
  • 126
  • 198