S
SP
I am writing a function to read data into a structure, but I seem to be
messing up the character string in the fopen() call.
When the program runs it does not open the file and fopen() returns a
NULL value.
I put in a few fprintf() calls to verify that the file name & path is
being set properly.
?? am I referencing the structure element correctly in the freadpng()
function ??
Thanks
Sal Polifemo
file png.h
=======
struct png_file{
char *name; /* name of image file */
FILE *filep; /* file pointer */
char *type; /* image type identifier */
int max_row; /* total rows of image */
int max_col; /* total columns of image */
int max_val; /* max pixel value of image */
int **pixel; /* pointer to memory space to store
image */
};
file freadpng.c
==========
#include <stdio.h>
#include "png.h"
int freadpng( struct png_file * image)
{
FILE *file_ptr;
fprintf(stderr,"\n#### file name: %s\n", &image->name);
file_ptr = fopen(image->name, "r");
if( !file_ptr )
{
fprintf(stderr,"\n#### error opening file \n");
return -1;
}
fclose(file_ptr);
return 1;
}
file main.c
=======
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fgetdata.h"
#include "png.h"
int freadpng( struct png_file *image);
int main(int argc, char * argv[])
{
struct png_file image_in;
int result;
image_in.name = malloc( ( strlen(argv[1]) + 1 ) * sizeof
*image_in.name );
if(image_in.name)
{
strcpy(&image_in.name, argv[1]);
}
else
{
fprintf(stderr,"Failed to copy filename to variable");
}
fprintf(stderr," \n#### image.name_p: %s\n", &image_in.name);
result = freadpng( &image_in);
if( result == 1)
{
fprintf(stderr,"\n#### back in main.c ####\n");
}
else
{
fprintf(stderr," \n#### function call error\n");
}
return EXIT_SUCCESS;
}
messing up the character string in the fopen() call.
When the program runs it does not open the file and fopen() returns a
NULL value.
I put in a few fprintf() calls to verify that the file name & path is
being set properly.
?? am I referencing the structure element correctly in the freadpng()
function ??
Thanks
Sal Polifemo
file png.h
=======
struct png_file{
char *name; /* name of image file */
FILE *filep; /* file pointer */
char *type; /* image type identifier */
int max_row; /* total rows of image */
int max_col; /* total columns of image */
int max_val; /* max pixel value of image */
int **pixel; /* pointer to memory space to store
image */
};
file freadpng.c
==========
#include <stdio.h>
#include "png.h"
int freadpng( struct png_file * image)
{
FILE *file_ptr;
fprintf(stderr,"\n#### file name: %s\n", &image->name);
file_ptr = fopen(image->name, "r");
if( !file_ptr )
{
fprintf(stderr,"\n#### error opening file \n");
return -1;
}
fclose(file_ptr);
return 1;
}
file main.c
=======
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "fgetdata.h"
#include "png.h"
int freadpng( struct png_file *image);
int main(int argc, char * argv[])
{
struct png_file image_in;
int result;
image_in.name = malloc( ( strlen(argv[1]) + 1 ) * sizeof
*image_in.name );
if(image_in.name)
{
strcpy(&image_in.name, argv[1]);
}
else
{
fprintf(stderr,"Failed to copy filename to variable");
}
fprintf(stderr," \n#### image.name_p: %s\n", &image_in.name);
result = freadpng( &image_in);
if( result == 1)
{
fprintf(stderr,"\n#### back in main.c ####\n");
}
else
{
fprintf(stderr," \n#### function call error\n");
}
return EXIT_SUCCESS;
}