Thread: Replace a word of a file using cmd

  1. #1
    Registered User
    Join Date
    May 2018
    Posts
    1

    Question Replace a word of a file using cmd

    Hello people i've done a program that replace a letter for another one using cmd. But i'm having issues trying to replace a whole word. i'm new using files, so i've tried to use fgets and fputs, but i had no results. :/


    This is my code:
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    //Function's prototypes
    void atributos(int argc,char *argv[]);
    void SustChar(char *argv[]);
    
    
    //MAIN
    int main(int argc, char *argv[]){
        
        
    FILE *ptrf;
    ptrf=fopen(argv[1],"r");
    if(ptrf==NULL){
        printf("No se pudo abrir el archivo\n");
        exit(1);
    }
    else{
        fclose(ptrf);
        atributos(argc,argv);
    }
        
        return 0;
    }
    
    //attributes
    
    
    void atributos(int argc,char *argv[]){
        if(strcmp(argv[2],"S")==0)
        SustChar(argv);
    }
    
    //Replace words
    
    
    void SustChar(char *argv[]){
        char c[80];
        
    FILE *ptrf;
    ptrf=fopen(argv[1],"r");
        FILE *ptrs;
        ptrs=fopen(argv[5],"w");
        
        while(!feof(ptrf)){    
        
        
        c=fgetc(ptrf);
        if(c==*argv[3])
            fputc(*argv[4],ptrs);
        else
            fputc(c,ptrs);    
    }
        fclose(ptrs);
        fclose(ptrf);
        
    }

    The sintaxis i'm using on cmd is: .exeName origin.txt S oldword newword destiny.txt

    Replace a word of a file using cmd-screenshot_20180512174404-png

  2. #2
    Registered User
    Join Date
    Apr 2017
    Location
    Quetzaltenango
    Posts
    63
    Code:
    fgets(c, sizeof c, ptrf);
    if (strcmp(c, argv[3]))
      fputs(argv[4], ptrs);
    else
      fputs(c, ptrs);
    Last edited by christophergray; 1 Week Ago at 05:58 PM.

  3. #3
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,531
    You shouldn't use feof() as the condition for the loop, not in that way at least. feof() returns true only after the end was reached, meaning that the fgetc() you use there returns EOF, and you mistakenly assume that EOF to be a character and write it to the other file with fputc().
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 28
    Last Post: 10-23-2011, 07:17 PM
  2. reading text-and-numbers file word by word
    By bored_guy in forum C Programming
    Replies: 22
    Last Post: 10-26-2009, 10:59 PM
  3. reading file word by word
    By 98holb in forum C Programming
    Replies: 2
    Last Post: 01-25-2006, 05:49 PM
  4. open file, search of word, replace word with another
    By Unregistered in forum C++ Programming
    Replies: 0
    Last Post: 06-05-2002, 01:16 PM
  5. Help reading text file word by word
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 05-25-2002, 05:13 PM

Tags for this Thread