Home > front end >  How to return a FILE* fp from a function to my main in c
How to return a FILE* fp from a function to my main in c

Time:11-25

Hello I practice with file handling in C and I try to make a code about removing comments. I make it with main.c, functions.c and functions.h .

main.h

#include <stdio.h>
#include <stdlib.h>
#include "functions.c"

int main(void) {

    FILE *in ,*out;
    char c;

    if ((in = fopen("input_file.c", "r")) == NULL ) {
        printf("Error opening \"file1.txt\" for reading\n");
        exit(1);
    }
    if ((out = fopen("input_file.nocmments.c","w")) == NULL ) {
        printf("Error opening \"file2.txt\" for writing\n");
        exit(2);
    }
    while((c = getc(in)) != EOF) {
        if(c == '/') comment(c, in, out);
        else if (c == '\'' || c == '\"') quotes(c, in, out);
        else print(c, out);
    }
    fclose(in);
    fclose(out);
    return 0;
}

function.c (I will show only a part because there is the same problem to other functions too

int one_line(char ch, FILE* fpin, FILE* fpout) {
    while ((ch = getc(fpin)) != '\n');
    fputc(ch, fpout);
    return ch, fpin, fpout; <-- error here
}

function.h

int comment(char ch, FILE* fpin, FILE* fpout);
int one_line(char ch, FILE* fpin, FILE* fpout);
int multiline(char ch, FILE* fpin, FILE* fpout);
int quotes(char ch, FILE* fpin, FILE* fpout);
int print(char ch, FILE* fpout);

So when I compile the code I get the following warning

returning ‘FILE *’ {aka ‘struct _IO_FILE *’} from a function with return typeintmakes integer from pointer without a cast [-Wint-conversion]

I understand that I can't return typedef and that's why I get the following problem, but how can I return the file pointer to my main function? (Sorry if my question is stupid)

CodePudding user response:

int one_line(char ch, FILE* fpin, FILE* fpout) {
    while ((ch = getc(fpin)) != '\n');
    fputc(ch, fpout);
    return ch, fpin, fpout; <-- error here
}

C has a few tricky or confusing details. The comma operator is one of them.

    return ch, fpin, fpout;

This is valid C code. The comma operator is defined in C11 section 6.5.17, which says:

The left operand of a comma operator is evaluated as a void expression; there is a sequence point between its evaluation and that of the right operand. Then the right operand is evaluated; the result has its type and value.

In your example, this means:

  • First, the expression ch is evaluated, and since it doesn't have any side effects, it is ignored.
  • Second, fpin is evaluated and ignored as well.
  • Third, fpout is evaluated and returned from the function.

The type of fpout is FILE *, the return type of the function is int, these types don't match, and this is where the compiler warning comes from.

You should change your code to simply return ch.

You do not need to return the files from the function, even though you modified them. The point here is that you didn't pass the actual files to the function, but pointers to the files.

CodePudding user response:

In C, you can only use return with 1 thing. To return more, you use pointers, so when you use fputc in fpout, you are already "returning". Basically, when you pass &variable you are actually passing the memory address to the called function, which means that the called function will be reading and writing in that memory space, altering it (if you write something).

In main, you can use fprintf to print to a file.

Note: when passing pointers you don't need to use the "&", you can just write the variable name.

  • Related