-1

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:

#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

Sintaxis' example

CarmesiD
  • 1
  • 1
  • Please add the line of code to the original question instead of attaching a screenshot – yash May 13 '18 at 01:02
  • You don't check fgetc for EOF return value. Maybe see [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). – KamilCuk May 13 '18 at 01:10

1 Answers1

0

To compare the whole line, one can use fgets to read a line and use strcmp to compare against third argument:

void SustChar(char *argv[]){
    char linebuf[80];
    FILE *ptrf;
    ptrf = fopen(argv[1],"r");
    FILE *ptrs;
    ptrs = fopen(argv[5],"w");
    // read line into a linebuf
    while(fgets(linebuf, 80, ptrf) != NULL){    
        linebuf[strlen(linebuf)-2] = '\0'; // remove newline character
        // if the line matches the third argument
        if (!strcmp(linebuf, argv[3])) {
            fputs(argv[4], ptrs);
        } else {
            fputs(linebuf, ptrs);    
        }
    }
    fclose(ptrs);
    fclose(ptrf);
}    

If you are interested in a specific word in that line, assuming that words are delimeted by a single space chracter, one can use strchr function to extract words and then compare them to the third argument.

void SustChar(char *argv[]){
    char linebuf[80];
    FILE *ptrf;
    ptrf = fopen(argv[1],"r");
    FILE *ptrs;
    ptrs = fopen(argv[5],"w");
    // read one line into a linebuf
    while(fgets(linebuf, 80, ptrf) != NULL) {
        // remove newline character
        linebuf[strlen(linebuf)-2] = '\0';
        // get a pointer to the second word in the line
        char *secondword = strchr(strchr(linebuf, ' ') + 1, ' ');
        assert(secondword != NULL);
        // get length of the second word
        char *thirdword = strchr(secondword , ' ');
        size_t secondwordlen = thirdword == NULL ? strlen(secondword) : thirdword - secondword;
        // if the second word matches the third argument
        if (!memcmp(secondword , argv[3], secondwordlen)) {
            secondword[0] = '\0'; // remove second word from linebuf
            fputs(linebuf, ptrs); // print first word
            fputs(argv[3], ptrs); // print second word substituted by argv[3]
            fputs(thirdword, ptrs); // print the rest of the line
        } else {
            fputs(linebuf, ptrs);    
        }
    }
    fclose(ptrs);
    fclose(ptrf);
}
KamilCuk
  • 120,984
  • 8
  • 59
  • 111