0

I want my code to receive an endless stream of input until the user (or text file) returns EOF. The problem is that in both input cases the first character registered is some additional one. Case in point - the input was Applepie, why is the first character registered 'p'?

enter image description here

Code:

string = (char *) malloc(allocated);    

while (c != EOF){

    c = getchar();
    
    if (counter >= allocated -1){
        allocated *= 2;
        new_buffer = realloc(string, allocated ); 
        if (! new_buffer){
            return (char *) string;
        }
        string = new_buffer;
    }
    
    /*store the char*/
    string[counter] = c;
    counter++;
    /*printf("current char: %c\n", string[counter]);*/
}

string[counter] = '\0';

return  (char*)string;

}

Sandra
  • 31
  • 4
  • 1
    In the first snippet `c` is uninitialized, but you compare it to EOF in the first iteration. – Eugene Sh. Mar 28 '22 at 15:17
  • 1
    I addition, since you check `c` *after* it was added to your string, it will give you this "extra character" – Eugene Sh. Mar 28 '22 at 15:19
  • 2
    Sandra, also do not use `char c;` to save the 257 different values from `getchar()`. Use `int c`. This also avoid UB with `isalnum(c)` when `c` is a _signed_ `char`. – chux - Reinstate Monica Mar 28 '22 at 15:19
  • Note: it's not the source of the main problem you're asking about, but you do need to read a (new) character *before* each test for EOF. This is related to [why `while (!feof(file))` is always wrong](https://stackoverflow.com/q/5431941/2402272). This mistake *is*, however, the reason you get a -1 at the end of your string. – John Bollinger Mar 28 '22 at 15:20
  • Note that if you run out of memory, you return a pointer to a byte array that is not null-terminated, so it is not a string. It would probably be better to release what was allocated and return a null pointer, but that's perhaps a different discussion. – Jonathan Leffler Mar 28 '22 at 15:35
  • I have change the while loop to be do-while and initiliazed the integer c (changed) to be 0. I am still getting the same error @EugeneSh. how else can I check it? – Sandra Mar 28 '22 at 15:36
  • 1
    @Sandra Please consider all the comments here. you have multiple issues. – Eugene Sh. Mar 28 '22 at 15:38

1 Answers1

1

Preliminary: why there is a -1 in your string

This has been discussed extensively in comments. It is because getString() writes the EOF indicating the end of the input into the string. It should avoid doing that by checking for EOF before putting the character just read in the string.

Unexpected first character

Case in point - the input was Applepie, why is the first character registered 'p'?

Because of this:

    int counter = 1; /*prints new line every 10 characters*/

[...]

    char c = string[counter];

printAndSum() then proceeds to print c, provided that its value is not -1, so if it prints anything at all then the first character it prints will be the one at index 1. After that, it updates c on each loop iteration with

        c = string[index++];                

, which has the effect of starting at the beginning of the string on the second iteration. There are various ways you could fix that, but one of the least disruptive would be to change the initialization of c to

    char c = string[0];  // or string[index]

, and to change the update of c to use preincrement instead of postincrement:

        c = string[++index];
John Bollinger
  • 160,171
  • 8
  • 81
  • 157