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

13
  • 2
    Use fgets for reading ine line. Commented Apr 25, 2014 at 15:21
  • See also e.g. stackoverflow.com/questions/3501338/c-read-file-line-by-line Commented Apr 25, 2014 at 15:22
  • fscanf and sscanf may also be useful. Commented Apr 25, 2014 at 15:22
  • 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. Commented Apr 25, 2014 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 ? Commented Apr 25, 2014 at 15:31

2 Answers 2

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;
}
Sign up to request clarification or add additional context in comments.

6 Comments

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
and i am also wondering how to make an alert if the user input is not exist in the file ?
@Gio 1st while : Repeat to read line by line from the file. 2nd while : Is repeated between string you want to delete exists.
@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 managed to make an Error message once the string that user input does not exist in the file , how do i get it ?
|
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 :)

2 Comments

read the char ? i wont work because michael said that i should compare the string ,
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.