0

I want to make a program that delete a String that the same as user input from a file below are contents in the file

G12
G13
G14

For example user input G13 , expected output is as following :

G12
G14

I got some idea that make a temporary file , but don't get any idea on how to get a string line by line because i print the file content using these code

if(file!=NULL)
{
    while ((c = getc(file)) != EOF)
    putchar(c);
    fclose(file);
}

so basically i reads all the file content only letter by letter (dont have any idea how to make it reads word by word

Thanks in advance

Gio
  • 25
  • 5
  • 2
    Use [`fgets`](http://www.cplusplus.com/reference/cstdio/fgets/) for reading ine line. – Jabberwocky Apr 25 '14 at 15:21
  • See also e.g. http://stackoverflow.com/questions/3501338/c-read-file-line-by-line –  Apr 25 '14 at 15:22
  • `fscanf` and `sscanf` may also be useful. –  Apr 25 '14 at 15:22
  • thanks for the string problem , but i still have no idea on delete part – Gio Apr 25 '14 at 15:24
  • 1
    @gio : you cannot delete a line from a file, you must copy the whole file line by line omitting the line(s) you want to delete. – Jabberwocky Apr 25 '14 at 15:27
  • 1
    @MichaelWalz yes thats what i meant , so it means make a new temporary file and rename it to the original file , isnt it ? – Gio Apr 25 '14 at 15:31
  • @MichaelWalz but i was wondering how to ommit a specific string by user input – Gio Apr 25 '14 at 15:32
  • @gio for each line read from the file use [strcmp](http://www.cplusplus.com/reference/cstring/strcmp/) to compare it to the string entered by the user and if they are equal dont write it to the temporary file, pretty basic. – Jabberwocky Apr 25 '14 at 15:35
  • @MichaelWalz after trying some codes , i still didnt get how to make the while loop read per string , i only can do read per char which is letter by letter , please give me the code how to read it by string and i will do the rest – Gio Apr 25 '14 at 15:48
  • @MichaelWalz `getline()` is even better suited for the task, it automatically `malloc()`s enough memory for the string. Part of the POSIX-2008 standard. – cmaster - reinstate monica Apr 25 '14 at 16:08
  • Try by yourself, the fgets documentation is pretty explicit. And if it doesn't work, edit your question and show your code. – Jabberwocky Apr 25 '14 at 16:09
  • @cmaster: sure getline is better, but it's not available on all platforms. – Jabberwocky Apr 25 '14 at 16:10
  • getline doesnt work on what is mine ... – Gio Apr 25 '14 at 17:08

2 Answers2

1

simple line by line sample

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

char *esc_cnv(char *str){
    char *in, *out;
    out = in = str;
    while(*in){
        if(*in == '\\' && in[1] == 'n'){
            *out++ = '\n';
            in += 2;
        } else 
            *out++ = *in++;
    }
    *out = '\0';
    return str;
}

int main(void){
    FILE *fin = stdin, *fout = stdout;
    char line[1024];
    char del_str[1024];
    char *p, *s;
    int len;
    int find=0;//this flag indicating whether or not there is a string that you specify 

    fin = fopen("input.txt", "r");
    fout = fopen("output.txt", "w");//temp file ->(delete input file) -> rename temp file.
    printf("input delete string :");
    scanf("%1023[^\n]", del_str);
    esc_cnv(del_str);//"\\n" -> "\n"
    len = strlen(del_str);
    while(fgets(line, sizeof(line), fin)){
        s = line;
        while(p = strstr(s, del_str)){
            find = 1;//find it!
            *p = '\0';
            fprintf(fout, "%s", s);
            s += len;
        }
        fprintf(fout, "%s", s);
    }
    fclose(fout);
    fclose(fin);
    if(find==0)
        fprintf(stderr, "%s is not in the file\n", del_str); 
    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70
  • could you please explain how does the 1st while and 2nd while work ? i cant get it at all , but ur code is working . Thanks before – Gio Apr 26 '14 at 02:58
  • and i am also wondering how to make an alert if the user input is not exist in the file ? – Gio Apr 26 '14 at 03:16
  • @Gio 1st while : Repeat to read line by line from the file. 2nd while : Is repeated between string you want to delete exists. – BLUEPIXY Apr 26 '14 at 03:22
  • @Gio File is simply copied if the string to be deleted does not exist. Is switched to the process of simply deleting the temporal file by checking that there was no change of the flag when it has finished reading the file by using a flag to record that it has entered the inner loop. – BLUEPIXY Apr 26 '14 at 03:28
  • i managed to make an Error message once the string that user input does not exist in the file , how do i get it ? – Gio Apr 26 '14 at 03:30
  • "@Gio File is simply copied if the string to be deleted does not exist. Is switched to the process of simply deleting the temporal file by checking that there was no change of the flag when it has finished reading the file by using a flag to record that it has entered the inner loop." i didnt get what you mean , i meant like if in the file there is only "GIO" (in 1 line) and the user input is "ALI" an error message "Ali is not in the file" comes out – Gio Apr 26 '14 at 03:32
0

Exactly as Micheal said. On a file you can do only write and read operation. So you must read the char and when you find the exact word that you don't want to write, just don't write it. Otherwise you write it :)

divivoma
  • 13
  • 4
  • read the char ? i wont work because michael said that i should compare the string , – Gio Apr 25 '14 at 17:08
  • A string is composed by multiple char. You should decide how to do the compare action. You can do it char by char or char[10] x char[10] using e.g. the strcmp function. – divivoma Apr 26 '14 at 17:22