-2
    #include <stdio.h>

    int main()
    {
        FILE *fp;
        int i;
        int pos;

        fp=fopen("test.txt","r+");
        fseek(fp,0,SEEK_END);
        pos=ftell(fp);

        char ch[pos-1];
        fseek(fp,0,SEEK_SET);


        ch[0]=ch[0]-32;

        i=0;
        while(ch[i]=fgetc(fp)!=EOF){


            if(ch[i]!=' '){
                fseek(fp,1,SEEK_CUR);
                i++;
            }
            else{
                fseek(fp,1,SEEK_CUR);
                i++;
                ch[i]=fgetc(fp);
                ch[i]=ch[i]-32;
                fprintf(fp,"%c",ch[i]);
            }       
        }
        fclose(fp);








    }

I want to make C program that capitalizes the first characters of the words in the file. But when I run this code .txt file get wrong. Is usage of fgetc() wrong? where is my fault for this question ? And is fscanf moving cursor ?

cilvegozu
  • 3
  • 1
  • 4
  • `ch[i]=ch[i]-32;` will be quite wrong if it is not uppercase or a non-alphanumeric character. Use `if (isupper(ch[i]) ch[i]= tolower(ch[i]);` – Paul Ogilvie May 24 '18 at 12:27
  • Opening in mode `"r+"` (append mode) will not allow you to change an existing position in the file. Use `"rw"`. – Paul Ogilvie May 24 '18 at 12:29
  • Why use such a big buffer `char ch[pos-1];` if you are using only one character of it at a time? Just `char ch;` would be enough. – Paul Ogilvie May 24 '18 at 12:30
  • In the while condition you get a char with `fgetc`, Then in the loop with `fseek` you _skip_ a character and then you _get_ another character. This is quite out of sync and you have consumed 3 characters but you think you have processed a single character. – Paul Ogilvie May 24 '18 at 12:35
  • @PaulOgilvie The `if` statement in `if (isupper(ch[i]) ch[i]= tolower(ch[i]);` is extraneous. `ch[i] = tolower[ch[i]);` works as `tolower()` will only modify upper-case `char` values. Per [the C standard](https://port70.net/~nsz/c/c11/n1570.html#7.4.2.1p3): "If the argument is a character for which isupper is true and there are one or more corresponding characters, as specified by the current locale, for which islower is true, the tolower function returns one of the corresponding characters (always the same one for any given locale); otherwise, the argument is returned unchanged." – Andrew Henle May 24 '18 at 12:57
  • It is difficult to offer solutions when the problem statement is simply, ["it doesn't work"](http://idownvotedbecau.se/itsnotworking/). Please [edit] your question to give a more complete description of what you expected to happen and how that differs from the actual results. See [ask] for hints on what makes a good explanation. – Toby Speight May 24 '18 at 14:48

1 Answers1

2

In the condition for your while loop, you have

ch[i] = fgetc(fp) != EOF

Since != has a high precedence than =, this is equivalent to

ch[i] = (fgetc(fp) != EOF)

Which doesn't evaluate to the character, but rather the 0 or non-zero value from the comparison.

In my opinion, a better way to do this would be to read in the entire string, modify it, then open the file again in writing mode and write back, if you're going to allocate an array for the contents anyways.

Thomas Jager
  • 4,836
  • 2
  • 16
  • 30