473,403 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

calling fopen using function

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) ;
}
Nov 14 '05 #1
9 1473
soumen <so*********@indiatimes.com> scribbled the following:
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") ;
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().

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"You will be given the plague."
- Montgomery Burns
Nov 14 '05 #2

"soumen" <so*********@indiatimes.com>
??????:a4*************************@posting.google. com...
/* 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.
Nov 14 '05 #3
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
so*********@indiatimes.com (soumen) wrote in message news:<a4*************************@posting.google.c om>...
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) ;
}

Nov 14 '05 #4
so*********@indiatimes.com (soumen) wrote in message news:<a4*************************@posting.google.c om>...
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); read_file(fptr) ;
close_file(fptr) ;

return(0) ;
}

Nov 14 '05 #5
so*********@indiatimes.com (soumen) wrote in message news:<a4*************************@posting.google.c om>...
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
Nov 14 '05 #6
so*********@indiatimes.com (soumen) wrote in message news:<a4*************************@posting.google.c om>...
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);
read_file(fptr) ;
close_file(fptr) ;

return(0) ;
}

Nov 14 '05 #7
Buzzard <Bu*****@126.com> spoke thus:
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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #8
> 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.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.


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.
Nov 14 '05 #9
Buzzard <Bu*****@126.com> scribbled the following:
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.


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.

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ------------- Finland --------\
\-- http://www.helsinki.fi/~palaste --------------------- rules! --------/
"Products like that make me wish I could menstruate."
- Andy Richter
Nov 14 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Daniel Hansen | last post by:
I'm sure I saw this somewhere but can't remember where and can't find it now... Is there a PHP function or global variable that will return name of the calling function? I want to do this for...
1
by: Martin Lucas-Smith | last post by:
I wrote the function below as part of a larger class. The fopen stage works, and, as according to the documentation at www.php.net/fopen that succesfully creates a new file. The fwrite stage...
5
by: Ben | last post by:
hi all, just wondering if it is possible to call a function that resides on external php file before any <html> tag?? i have a code that sets a cookie which needs to be called before <html> tag....
3
by: James | last post by:
Hi guys, I have been building a search engine here - not because I have plans of dethrowning Google but as a simple app upon which to develop a function set that I can use for other things. ...
5
by: Trying_Harder | last post by:
Generally we have function names briefly indicating their actions. In similar lines I expected `fopen' mean "File Open", fgets "File Get String" etc. I was told recently this wasn't the case...
28
by: Sathyaish | last post by:
If fopen fails, is there a way to know why?
5
by: Areric | last post by:
Ok all. I have a series of images stored in a db. Im trying to work on a script that will let me scale them based on user input. Ive hit a bit of a roadblock on this line. $orig =...
10
by: Julia | last post by:
Hi, there, I am trying to append a binary file by using: FILE *strean; stream = fopen( "c:\temp.dat", "ba+" )); Is there a way that I can check if the file exists or not before fopen,...
20
by: cscorley | last post by:
For some reason, I cannot use fopen() on the file in write mode. The file "time" is in the same directory as the .php file, with permissions set to 0766. PHP Version 5.2.5 Apache/2.2.8 code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.