mikel bubi
mikel bubi

Reputation: 123

Return FILE pointer from function and work on it on main

I've tried to return a FILE pointer from some function to main(). After it, I've tried to do some fprintf on the pointer but it wasn't working. Here is my code:

My function:

FILE *create_file(void){

    FILE *regularTxt = NULL;
    regularTxt = fopen("MyLogExamplekkggggggk.txt", "wt");
    if (!regularTxt){
        printf("error with regtxt");
        getchar();
        return 1;
    }
    char first_part_string[] = "kkkkik";
    fprintf(regularTxt, "%s\n\n\n%s", "ttttg", "lklf");
    return regularTxt;
}

The main function:

int main(void)
{
    p_txt = create_file();
    fprintf(p_txt, "%s\n\n\n%s", "gggg", "lklf");
    return 0;
}

The error:

Error 92 error C4703: potentially uninitialized local pointer variable 'p_txt' used

Upvotes: 5

Views: 14632

Answers (5)

hfunes.com
hfunes.com

Reputation: 178

I had the same problem. Solved that adding a CAST in the function's call. I.E.: In this example, replaced this:

p_txt = create_file();

With this:

p_txt = (FILE *) create_file();

Hope It helps!

Upvotes: 0

mikel bubi
mikel bubi

Reputation: 123

the problem was becase the compailer feared when i didnt initialized the value of the pointer to NULL...thanks guys :)

Upvotes: 0

Brad
Brad

Reputation: 3530

Without all the code I can't explain the warning, but when you "return 1" from the function in the error case you didn't initialize the pointer correctly.

Change to this:

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

FILE *create_file()
{
    FILE *regularTxt = NULL;
    regularTxt = fopen("MyLogExamplekkggggggk.txt", "wt");
    if (regularTxt) {
        char first_part_string[] = "kkkkik";
        fprintf(regularTxt, "%s\n\n\n%s", "ttttg", "lklf");
        return regularTxt;
    }
    return NULL; // error
}

int main(void)
{
    FILE* p_txt = create_file();
    if (p_txt == NULL)
    {
        printf("error with file");
        getchar();
        exit(1); // quit
    }
    fprintf(p_txt, "%s\n\n\n%s", "gggg", "lklf");
    return 0;
}

Upvotes: 2

Fire Crow
Fire Crow

Reputation: 7739

The below works, replace 1 with NULL to remove the compiler warning shown below.

#include <stdio.h>

FILE *get(){
  FILE *myfile = fopen("fio.txt", "wt");
  if(1 == 2){
    return 1;// this causes a warning, setting this to NULL removes the warning
  }
  return myfile;
}

int main(int argc, char **argv){
  FILE *fp = get();
  fprintf(fp, "hi there C\n");
}

fio.c:6:12: warning: incompatible integer to pointer conversion returning 'int' from a function with result type 'FILE *' (aka 'struct __sFILE *') [-Wint-conversion] return 1; ^ 1 warning generated.

Upvotes: 0

ForceBru
ForceBru

Reputation: 44886

You seem to be treating warnings as errors here. I think you should first check the returned value for NULL to silence this error:

FILE *p_txt = create_file();

if (! p_txt) {
    fprintf(stderr, "[!] Failed to open the file!\n");
    return 1;
} else {
    fprintf(p_txt, "%s\n\n\n%s", "gggg", "lklf");
}

Also, when you're entering into if (!regularTxt), you're returning one from a function returning a pointer. Better return NULL instead.

Upvotes: 1

Related Questions