calling fopen using function

S

soumen

Hi,

I'm a newbie compiling the following program on Sun Solaris platform
for reading data from a file. The code commented as "/* WORKING */" is
working (!). But in the current form of open_file(), it is generating
coredump (no warnings while compiling):

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

/* WORKING
FILE *open_file(char *fname_tmp)
{
FILE *fptr_tmp ;
fptr_tmp = fopen(fname_tmp,"r") ;
if (fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
return fptr_tmp ;
}
}
*/

void open_file(FILE *fptr_tmp, char *fname_tmp)
{
fptr_tmp = fopen(fname_tmp,"r") ;
if (fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
}
}

void read_file(FILE *fptr_tmp)
{
int c ;
while ((c = fgetc(fptr_tmp)) != EOF) {
putchar(c) ;
}
}
void close_file(FILE *fptr_tmp)
{
if (fclose(fptr_tmp) != 0) {
printf("Error closing file") ;
}
else {
printf("Closed file") ;
}
}

int main()
{
FILE *fptr ;
char fname[50] ;

strcpy(fname, "a") ;

/* WORKING
fptr = open_file(fname) ;
*/
open_file(fptr, fname) ;
read_file(fptr) ;
close_file(fptr) ;

return(0) ;
}
 
J

Joona I Palaste

soumen said:
I'm a newbie compiling the following program on Sun Solaris platform
for reading data from a file. The code commented as "/* WORKING */" is
working (!). But in the current form of open_file(), it is generating
coredump (no warnings while compiling):

/* WORKING
FILE *open_file(char *fname_tmp)
{
FILE *fptr_tmp ;
fptr_tmp = fopen(fname_tmp,"r") ;
if (fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
return fptr_tmp ;
}
}
*/
void open_file(FILE *fptr_tmp, char *fname_tmp)
{
fptr_tmp = fopen(fname_tmp,"r") ;

The fptr_tmp that this function gets is a *copy* of the FILE* it was
passed in main(), not the same variable. They are two different
variables sharing the same value. Now when you assign to fptr_tmp in
this function, it has no effect on the original variable in main().
if (fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
}
}
void read_file(FILE *fptr_tmp)
{
int c ;
while ((c = fgetc(fptr_tmp)) != EOF) {
putchar(c) ;
}
}
void close_file(FILE *fptr_tmp)
{
if (fclose(fptr_tmp) != 0) {
printf("Error closing file") ;
}
else {
printf("Closed file") ;
}
}
int main()
{
FILE *fptr ;
char fname[50] ;
strcpy(fname, "a") ;
/* WORKING
fptr = open_file(fname) ;
*/
open_file(fptr, fname) ;
read_file(fptr) ;
close_file(fptr) ;
return(0) ;
}

Try instead passing a pointer: make open_file() accept a FILE**, and
assign to *fptr_tmp in open_file, and pass &fptr to it in main().
 
B

Buzzard

soumen said:
/* WORKING
FILE *open_file(char *fname_tmp)
{
FILE *fptr_tmp ;
fptr_tmp = fopen(fname_tmp,"r") ;
if (fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
return fptr_tmp ;
}
}
*/
The pointer fptr_tmp is a local variable,when the function return,it is
destroied.So the return value point a inexistence address.
 
O

Olaf

Hello there
Parameters in C are always by value, never by reference.

the error is in the
void open_file(FILE *fptr_tmp, char *fname_tmp)
fptr_tmp is a local var. its changed but the calling function don't
get it's new value, the pointer to the opened file.

use
void open_file(FILE **fptr_tmp, char *fname_tmp)
{
*fptr_tmp = fopen(fname_tmp,"r") ;
if (*fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
}
}

and call it like this
open_file(&fptr, fname) ;

now you have a working program again!

Greetings Olaf
 
P

Paul

Hi,

I'm a newbie compiling the following program on Sun Solaris platform
for reading data from a file. The code commented as "/* WORKING */" is

code commented as "//" will work when uncommented :)
working (!). But in the current form of open_file(), it is generating
coredump (no warnings while compiling):

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

/* WORKING
FILE *open_file(char *fname_tmp)
{
FILE *fptr_tmp ;
fptr_tmp = fopen(fname_tmp,"r") ;
if (fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
return fptr_tmp ;
}
}
*/

void open_file(FILE *fptr_tmp, char *fname_tmp)
//void open_file(FILE **fptr_tmp, char *fname_tmp)
{
fptr_tmp = fopen(fname_tmp,"r") ;
//fptr_tmp = fopen(fname_tmp,"r") ;
if (fptr_tmp == NULL) {
//if (*fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
}
}

void read_file(FILE *fptr_tmp)
{
int c ;
while ((c = fgetc(fptr_tmp)) != EOF) {
putchar(c) ;
}
}
void close_file(FILE *fptr_tmp)
{
if (fclose(fptr_tmp) != 0) {
printf("Error closing file") ;
}
else {
printf("Closed file") ;
}
}

int main()
{
FILE *fptr ;
char fname[50] ;

strcpy(fname, "a") ;

/* WORKING
fptr = open_file(fname) ;
*/
open_file(fptr, fname) ;
//open_file(&fptr, fname);
 
P

Paul

Hi,

I'm a newbie compiling the following program on Sun Solaris platform
for reading data from a file. The code commented as "/* WORKING */" is
working (!). But in the current form of open_file(), it is generating
coredump (no warnings while compiling):

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

/* WORKING
FILE *open_file(char *fname_tmp)
{
FILE *fptr_tmp ;
fptr_tmp = fopen(fname_tmp,"r") ;
if (fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
return fptr_tmp ;
}
}
*/

void open_file(FILE *fptr_tmp, char *fname_tmp)
{
fptr_tmp = fopen(fname_tmp,"r") ;
if (fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
}
}

void read_file(FILE *fptr_tmp)
{
int c ;
while ((c = fgetc(fptr_tmp)) != EOF) {
putchar(c) ;
}
}
void close_file(FILE *fptr_tmp)
{
if (fclose(fptr_tmp) != 0) {
printf("Error closing file") ;
}
else {
printf("Closed file") ;
}
}

int main()
{
FILE *fptr ;
char fname[50] ;

strcpy(fname, "a") ;

/* WORKING
fptr = open_file(fname) ;
*/
open_file(fptr, fname) ;
read_file(fptr) ;
close_file(fptr) ;

return(0) ;
}

missed a '*'
*fptr_tmp = fopen(fname_tmp,"r") ;

-Paul
 
J

John Bode

Hi,

I'm a newbie compiling the following program on Sun Solaris platform
for reading data from a file. The code commented as "/* WORKING */" is
working (!). But in the current form of open_file(), it is generating
coredump (no warnings while compiling):

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

/* WORKING
FILE *open_file(char *fname_tmp)
{
FILE *fptr_tmp ;
fptr_tmp = fopen(fname_tmp,"r") ;
if (fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
return fptr_tmp ;
}
}
*/

void open_file(FILE *fptr_tmp, char *fname_tmp)

Remember that C passes all parameters by value; if you want to write
to an object of type T in the calling function, you must pass an
object of type T*. In this case, you want to write to fptr (type FILE
*) in your main() function, so you need to pass a pointer to it
(&fptr, type FILE **):

void open_file (FILE **fptr_tmp, char *fname_tmp)
{
fptr_tmp = fopen(fname_tmp,"r") ;

*fptr_tmp = fopen(fname_tmp, "r");
if (fptr_tmp == NULL) {

if (*fptr_tmp == NULL) {
printf("Error opening file : %s\n", fname_tmp) ;
exit(2) ;
}
else {
printf("Opened file : %s\n", fname_tmp) ;
}
}

void read_file(FILE *fptr_tmp)
{
int c ;
while ((c = fgetc(fptr_tmp)) != EOF) {
putchar(c) ;
}
}
void close_file(FILE *fptr_tmp)
{
if (fclose(fptr_tmp) != 0) {
printf("Error closing file") ;
}
else {
printf("Closed file") ;
}
}

int main()
{
FILE *fptr ;
char fname[50] ;

strcpy(fname, "a") ;

/* WORKING
fptr = open_file(fname) ;
*/
open_file(fptr, fname) ;

open_file(&fptr, fname);
 
C

Christopher Benson-Manica

Buzzard said:
The pointer fptr_tmp is a local variable,when the function return,it is
destroied.So the return value point a inexistence address.

No. The local variable is gone, but its value (which is what is being
returned) is still perfectly valid. Consider:

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

int foo()
{
int bar=3;
return bar;
}

char *baz()
{
char *quux=malloc( 4 );
if( quux ) {
sprintf( quux, "foo" );
}
return quux;
}

Both are perfectly legal. The memory quux points to (assuming malloc
succeeded) remains allocated and valid after baz() returns.
 
B

Buzzard

No. The local variable is gone, but its value (which is what is being
returned) is still perfectly valid. Consider:

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

int foo()
{
int bar=3;
return bar;
}

char *baz()
{
char *quux=malloc( 4 );
if( quux ) {
sprintf( quux, "foo" );
}
return quux;
}

Both are perfectly legal. The memory quux points to (assuming malloc
succeeded) remains allocated and valid after baz() returns.

quux is assign in heap,so when function return,it remains untill call
free().but fptr_tmp is assign in stack,when function return,it is
destroied.
 
J

Joona I Palaste

quux is assign in heap,so when function return,it remains untill call
free().but fptr_tmp is assign in stack,when function return,it is
destroied.

Please read a C textbook. The C language doesn't define concepts such as
"heap" or "stack". All automatic variables behave the same way, no
matter what their types are.
The situation with quux above and fptr_tmp in the original code is the
exact same. They are both assigned in automatic storage. When the value
of quux (char*) or fptr_tmp (FILE*) is returned from a function, it is
perfectly valid, even when the original variable quux or fptr_tmp is no
longer usable.
You have been shown a simpler example with int. Pointers behave exactly
like int in this matter.
All you're doing here is confusing newbies by giving incorrect
information.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top