0

Im doing a project to change the letter 'e' to 'a' and I still didnt have it right at all. My input is a file abc.txt: '''

Im enne end
my ded is frenk
My mom is elycie Lou

''' and my output is "My mom is alycie Lou" and in another line "My mom is alicya Lou". Heres my code. Can anyone helps me?

'''

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LINE 3
#define MAX_STRING_SIZE 50


int main()
{
   char line[MAX_LINE][MAX_STRING_SIZE];
   int i=0;
   FILE *arch;
   arch = fopen("abc.txt", "r");

   if (arch==NULL){
       printf("ERROR");
    }
    else{
        while (!feof(arch)){
            fgets(line[i], MAX_STRING_SIZE , arch);
            i++;
        }
    }
    fclose(arch);

    for ( i=0; line[i][MAX_STRING_SIZE]; ++i )
    {
        if ( line[i][MAX_STRING_SIZE] == 'e' )
        {
            line[i][MAX_STRING_SIZE] = 'a';

        }
    }printf("%s", line);

    return 0;
}

'''

Ognum
  • 19
  • 7
  • You're reading all the lines into the same buffer, you end up with only the last line (you overwrite the previous one each time you read a new one). Try dealing with each line in the while loop, right after reading it, instead of at the end. – Dmitri Dec 26 '19 at 17:59
  • Also, the `for` loop doesn't make sense... you're iterating over the characters in the string to do the substitutions, but printing the string every time you replace a character instead of just once after all the replacements are done. – Dmitri Dec 26 '19 at 18:03

3 Answers3

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

int main()
{
   int i;
   char line[50];
   FILE *arch;
   arch = fopen("abc.txt", "r");

   if (arch==NULL){
       printf("ERROR");

better: perror("abc.txt"); exit(EXIT_FAILURE);

    }
    else{
        while (!feof(arch)){

see while(!feof()) is always wrong

            fgets(line, 50 , arch);
            //store all strings from the file
        }

Nope! This does not store all strings from the file. This reads all strings fro the file, keeping the last one read in line. In your case, when the loop finishes (ignoring errors from while(!feof())), line will have the last line from the file.

    }
    fclose(arch);

    for ( i=0; line[i]; ++i )
    {
        if ( line[i] == 'e' )
        {
            line[i] = 'a';
            printf("%s", line);

Do you want to print the line as many times as there are 'e's there? If you just want to print it once after all 'e's have been converted move the printf line to after the end of the for loop

        }
    }

    return 0;
}

Have fun!

pmg
  • 106,608
  • 13
  • 126
  • 198
  • thanks for the touch! But if I do something like: fgets(line[i], 50 , arch) and i++ to store all the strings; its also wrong, right? – Ognum Dec 26 '19 at 18:47
  • yes, that is wrong. You need an array of array (an array of strings) as in `char line[MAX_LINES][MAX_LENGTH];` and then you can read into `line[i]` inside the loop (and update `i++`) – pmg Dec 26 '19 at 18:55
  • I changed my code above and now it just reads the first line. Can you tell what is wrong, please? :) – Ognum Dec 26 '19 at 19:37
  • @Mungo You are not processing the line immediately after reading it. To preserve PMG's answer I have included my own down below. Never use `feof()` but instead check to see if `fgets()` returns `NULL` – Joel Trauger Dec 26 '19 at 20:14
  • It worked! thank you so much! you guys helped me a lot. Im new on this thing and my professor told me to not use functions that we still havent seen on class. Thats why I used 'feof' because he only taught me that to read files. But the the way you put it its allowed to me to use and I didn't have thought of that. so thank you so much again. – Ognum Dec 26 '19 at 20:32
0

Here are a couple tips I'd consider implementing

First change your line declaration and also declare a counter that isn't i. We'll use both i and counter:

char line[MAX_STRING_SIZE];
int counter = 0;

Then I would do this to the while loop that reads your file. Note that if counter ever equals or exceeds MAX_LINE then the loop will exit:


while (((fgets(line, MAX_STRING_SIZE , arch)) != NULL) && counter < MAX_LINE)
{
    for ( i=0; line[i]; ++i )
    {
        if ( line[i] == 'e' )
        {
            line[i] = 'a';
        }
    }
    printf("%s", line);
    counter++;
}

Try these and see if it produces the desired behavior.

Hope it helps!

Joel Trauger
  • 720
  • 1
  • 4
  • 24
0

When you want to do something with the contents of a whole file (change all 'e's to 'a's, delete all occurrences of the word "very", ...) the first thing that you should think about is: I'll read the file line-by-line, process each line until there are no more lines. The most straight forward implementation of that thought is

char line[MAX_LINE_LENGTH];
// use FILE *f = fopen(filename, "r"); instead of stdin
while (fgets(line, sizeof line, stdin)) {
    process(line);
}
// remember to fclose(f) if not using stdin

Remember this 4 (or 6) lines; you'll use them often.

In your specific case (you want to print the line with 'a's instead of 'e's) the process() function could be something like:

void process(const char *line) {
    for (int i = 0; i < strlen(line); i++) {
        if (line[i] == 'e') putchar('a');
        else putchar(line[i]);
    }
}

And that's it. Most of your program is done.

Add the proper #includes, error checking, main() and call it a day.

pmg
  • 106,608
  • 13
  • 126
  • 198