0

This function gives infinite loop. Any Help? And is it even possible to pass file stream to a function as argument.

#include<stdio.h>
#include<stdlib.h>
void fcopy(FILE *inFILE1){
FILE *inFILEcopy;
char a;
inFILEcopy=fopen("C:/Users/labuser.pcroot-PC.003/Desktop/empoleecopy.bak","w");
do{
        a=fgetc(inFILE1);
        fputc(a,inFILEcopy);
        if(feof(inFILE1))break;
    }while(1);
}
int main(){

FILE *inFILE;
inFILE=fopen("C:/Users/labuser.pcroot-PC.003/Desktop/empolee.dat","w");
fputs("My name is Anthony",inFILE);
fcopy(inFILE);  


}
Nagato
  • 87
  • 2
  • 8
  • 2
    You should `fclose(inFILE)` and re-open it with mode "`r`" before passing it to `fcopy`. – Phil Brubaker May 10 '17 at 12:21
  • 2
    It is possible to pass file stream as function argument. In the given code there are few things missing, (1) File need to be opened in read+write mode ([details](https://www.tutorialspoint.com/cprogramming/c_file_io.htm)). (2) Since you write to file, cursor is at the ending of file. This will result in no data from `a=fgetc(inFILE1);` line. Cursor have to be brought back in order to read data ([details](http://beej.us/guide/bgc/output/html/multipage/fseek.html)). – svtag May 10 '17 at 12:58
  • 1
    One more: `fgetc` returns an `int`, not a `char`, so `char a;` -> `int a;` – Jabberwocky May 10 '17 at 13:05
  • 2
    Also add fclose(inFILEcopy) after do-while loop into fcopy function – Simone Cifani May 10 '17 at 13:06

2 Answers2

1

To summarize mine and Phil Brubaker comments, modify your code in this way:

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

void fcopy(FILE *inFILE1) {
    FILE *inFILEcopy;
    char a;
    inFILEcopy = fopen("C:/Users/scifani/Desktop/empoleecopy.bak", "w");
    do{
        a = fgetc(inFILE1);
        fputc(a, inFILEcopy);
        if (feof(inFILE1))break;
    } while (1);
    fclose(inFILEcopy);
}

int main(){
    FILE *inFILE;
    inFILE = fopen("C:/Users/scifani/Desktop/empolee.dat", "w");
    fputs("My name is Anthony", inFILE);
    fclose(inFILE);
    inFILE = fopen("C:/Users/scifani/Desktop/empolee.dat", "r");
    fcopy(inFILE);
}
Community
  • 1
  • 1
Simone Cifani
  • 784
  • 4
  • 14
0
FILE* fcopy(char* yourFile) { // Or parse a FILE like you did but with a pointer

    FILE *inFILEcopy;
    inFILEcopy = fopen("C:/Users/labuser.pcroot PC.003/Desktop/empoleecopy.bak", "w");
    if (inFILEcopy == NULL)
        return NULL; // You'll have to check null to see if fcopy() failed
    FILE* inFILE1 = fopen(yourFile, "r");
    if (inFILE1 == NULL) {
        puts("File to be copied does not exist.\n");
        return NULL;
    }
    for (char a = fgetc(inFILE1); feof(inFILE1);)
    {
       fputc(a, inFILEcopy);
       if (ferror(inFILE1) || ferror(inFILEcopy)) { // If error in one of the two files
           if (fclose(inFILE1))
               puts("Couldn't close inFILE1\n");
           if (fclose(inFILEcopy));
               puts("Couldn't close inFILEcopy\n");
           puts("Error during copy.\n");
           return NULL;
       }
    }
    return inFILEcopy;
}

int main() {

   FILE *inFILE;
   inFILE= fopen("C:/Users/labuser.pcroot-PC.003/Desktop/empolee.dat","w");
   if (inFILE != NULL)
   {
       fputs("My name is Anthony", inFILE);
       if (!ferror(inFILE) || fclose(inFILE)) // If no error when writing and closing works, we can copy
       {
           inFILE = fcopy("C:/Users/labuser.pcroot-PC.003/Desktop/empolee.dat");
           if (inFILE != NULL)
               puts("Copy success\n");
       }
   }
}

I think this is the best way to do this. I am open to any improvement though. Check this link if you have any question regarding the error checkings, someone explains the best way to do so. This should work perfectly.

Community
  • 1
  • 1
Badda
  • 1,329
  • 2
  • 15
  • 40