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) ;
}
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) ;
}