-1

I'm trying a very simple programm where whatever is written in test.txt gets copied in up.txt but in capital letters. I'm using dev c++ on windows 11 and after running the programm the up.txt file is created but it is empty and i can't figure out why.

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>

int main(){
    FILE *fpin, *fpout; 
    char x;
    fpin=fopen("test.txt","r");
    if(fpin==NULL){
        fprintf(stderr,"read error\n");
        exit(666);
    }
    fpout=fopen("up.txt","w");
    if(fpout=NULL){
        fprintf(stderr,"write error/n");
        exit(667);
    }
    while((x=fgetc(fpin))!=EOF){
        fputc(toupper(x),fpout);
    }
    fclose(fpin);
    fclose(fpout);
    
    return 0;
}

I tried the same programm on linux succesfully but i'm not sure why it doesn't work on windows

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
sartttt
  • 11
  • 1
  • 1
    Your code is wrong: change `if (fpout = NULL)` to `if (fpout == NULL)`. I don't see how your code can work on any platform. – Jabberwocky Jan 18 '23 at 12:39
  • 1
    use `int x` instead of `char` because `EOF` is outside the range of 256 valid `char` values – Bodo Jan 18 '23 at 12:39
  • @Bodo that's not the main problem though. – Jabberwocky Jan 18 '23 at 12:42
  • ```exit(666)``` ---> What? – Harith Jan 18 '23 at 12:49
  • 2
    You might exchange those `fprintf(stderr,"some error")` with `perror("some error")`, which will print `"some error: "` in future programs -- so you get an idea *what went wrong*. – DevSolar Jan 18 '23 at 12:58
  • @DevSolar yes, but in this particular code the `fprintf(stderr, ...` is never executed because of the `if(fpout=NULL)` – Jabberwocky Jan 18 '23 at 13:09
  • @Jabberwocky That's why it's a comment on his coding style, not an answer to his problem. ;-) – DevSolar Jan 18 '23 at 13:18
  • 2
    @sartttt As for your question, it will likely be closed as "caused by a typo". Don't let that discourage you: Your question was valid, and actually *very good* as far as first SO questions go. You included a [mcve], which most first-timers don't, and described the problem. The close votes are basically just technical (the answer helped you, but is unlikely to help future readers, so it will not be listed). +1 to you. – DevSolar Jan 18 '23 at 13:22

1 Answers1

2

There is no way this code can work on any platform. Are you sure the code you ran on Linux is actually this exact same code you show in the question?

The main problem is here:

   fpout=fopen("up.txt","w");
                         // up.txt is now created

   if (fpout = NULL) {   // you're using an assignment here (=) instead 
                         // of the == operator
      fprintf(stderr,"write error/n");
      exit(667);
   }
   // fpout is NULL now

if (fpout = NULL) is not a comparision, but it simply assigns NULL to fout. As the result is false (which is more or less the same thing as NULL), fprintf(stderr,"write error/n");exit(667) will not be executed and the program just continues.

Then in the while loop that follows, you're writing into a NULL FILE pointer, which is undefined behaviour, most likely the program will crashes or it might just do nothing or just quit, or maybe something else). Look up "C undefined behaviour" on your favorite search engine.


There is another subtle bug: char x; should be int x; because fgetc returns an int, not a char.

Read following articles for more information:

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • Just wondering: why did it work on Linux? Because it was not quite the same code you posted? – Jabberwocky Jan 18 '23 at 15:33
  • 1
    There's a 2nd more subtle bug too: `(x=fgetc(fpin))!=EOF` Since `x` is `char` it can never be equal to `EOF`. Feel free to add that to your answer as well, for completeness. – Lundin Jan 18 '23 at 15:37
  • @Lundin correct, this has been mentioned in one of the comments. But yes, I'll add it to the questio, thnks. – Jabberwocky Jan 18 '23 at 15:39