C Program cant create Text files

Viewed 68

I know java already and decided to pick up C today. I am trying to do a beginner exercise where I take two text files and generate a 3rd text file with the content of the first 2 text files.

I created a folder on my desktop and the only files in it are file1.txt, file2.txt, merge.c, merge.exe.

This is the contents of merge.c below

#include <errno.h>
#include <stdio.h> 
#include <stdlib.h> 
  
int main() 
{ 
 
   FILE *fp1 = fopen("file1.txt", "r"); 
   FILE *fp2 = fopen("file2.txt", "r"); 
  

   FILE *fp3 = fopen("file3.txt", "w+"); 
   char c; 
  
   if (fp1 == NULL || fp2 == NULL) 
   { 
         puts("Could not find source files"); 
         exit(EXIT_FAILURE); 
   }

   if (fp3 == NULL){
       puts("DNE");
       exit(EXIT_FAILURE);
   }
  
   while ((c = fgetc(fp1)) != EOF) 
    fputc(c, fp3); 
  
   while ((c = fgetc(fp2)) != EOF) 
      fputc(c, fp3); 
  
   printf("Merged file1.txt and file2.txt into file3.txt"); 
  
   fclose(fp1); 
   fclose(fp2); 
   fclose(fp3); 
   return 0; 
} 

It exits on DNE every time since file 3 is never created. I have tried using both w and w+ to no avail.

When using Errno, I get error no:13 permission denied. Even when running my vscode on administrator mode. Does anyone know why this is happening? I even removed my old code and copy pasted a generic answer from the internet but I still have these permission problems.

0 Answers
Related