Very important c program!!! - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Very important c program!!!

axutaxut Posts: 17Member
[b][red]This message was edited by axut at 2004-8-15 3:27:57[/red][/b][hr]
I need to do a c program that deletes from a txt file all the lines containing a specific word. Like all the lines that contain the word "food" should be deleted. I'm not good in programming but i got to do this so please help me.

Thank you,
axut.


Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : [b][red]This message was edited by axut at 2004-8-15 3:27:57[/red][/b][hr]
    : I need to do a c program that deletes from a txt file all the lines containing a specific word. Like all the lines that contain the word "food" should be deleted. I'm not good in programming but i got to do this so please help me.
    :
    : Thank you,
    : axut.
    :
    :
    :
    [blue]Algorithm:
    [code]
    open_src_file_as_text ();
    create_temporary_file_as_text ();
    // Use function [b]fopen()[/b] here ^^^

    while (fgets (...)) { // Get a line from src file
    if (strstr (...) == NULL) { // Check if line has word
    // No word.
    fputs (...); // save the line into temporary file
    }
    }

    close_both_file (); // Use [b]fclose(...)[/b]
    delete_src_file (); // Use [b]unlink()[/b]
    rename_temp_file_as_src_file ();
    [/code]
    You need to find a help information (tutorial) on C-library and find out how to use the functions I mentioned.[/blue]
  • adrasteaadrastea Posts: 1Member
    #include
    #include
    #include

    #define K 100 /* max letters in a line*/

    // this program removes the line which contains the given string.! case sensitive
    // if you mean to remove the lines which contain exact string you modify the string " string "

    main(){

    FILE *p1,*p2;
    char path[100],line[K],word[12],tempname[]="C:\temp.txt";
    int z;

    clrscr();
    printf("Enter the file name ");
    gets(path);

    p1=fopen(path,"r");

    if(!p1)
    {
    printf("File could not be found");
    getch();
    return 0;
    }
    tempname[0]=path[0];
    p2=fopen(tempname,"w");
    printf("Please enter the word ");
    gets(word);

    fgets(line,K,p1);

    while(!feof(p1))
    {
    if(!strstr(line,word))
    fprintf(p2,"%s",line);
    fgets(line,K,p1);
    }
    fcloseall;
    remove(path);
    z=rename(tempname,path);
    if(z)
    printf("
    the file is in C drive and named temp.txt
    couldnt be moved");
    else printf("Done");

    getch();

    return 0;

    }




Sign In or Register to comment.