I have a program that reads a file text.txt and contains filenames. When it reads a filename it opens it and makes sure if it is a png file. If it is I write its height and width with printf. How could I transform that, so I would have a function, that would have a parameter as filename which would do the same and then add a structure of all the files into the linked list.
it expects char, and you trying to assign char* . I can give you an advice: use std::string. It is error-prone, safe and intuitive. No worries about overflowing, forgetting to add trailing zero, manual memory management. And do not use malloc(). Provide custom constructor for your node and do things in C++ way.
You might notice that this is C++ forum, and C++ isn't just an extencion to C, many C programs will not work in C++ because of language and behavior changes.
1) Made ima be of type char*, not char
2) Probably better to use strncopy to be sure that there is no overflow.
Strange couse now I get:
main.cpp:16: error: invalid conversion from `char' to `char*'
main.cpp:16: error: initializing argument 1 of `char* strncpy(char*, const char*, size_t)'
If you make ime a char*, then you need to dynamically allocate space (and free it later). It's a good idea to learn how to do that, but you can also do:char ime[100]; until you get your program working.
Are you using libpng? I would very much recommend this instead of trying make your own png reader. If this is the case then you have something like this:
1 2 3 4 5 6
FILE *fp = fopen(file_name, "rb");
if (!fp)
abort_("[read_png_file] File %s could not be opened for reading", file_name);
fread(header, 1, 8, fp);
if (png_sig_cmp(header, 0, 8))
abort_("[read_png_file] File %s is not recognized as a PNG file", file_name);
file_name is a c-string, but there is no reason you can't use std::string
1 2
std::string myName = "data.png";
fopen(myName.cstr(), "rb"); // I think that is the method to make a string a c-string
"many C programs will not work in C++ because of language and behavior changes."
No. C++ is C that has evolved into a more flexible language; C++ is designed to support C calling conventions. Even so, C++ has the same syntax than C, so a C++ compiler will understand the source code, regardless of language. Besides, C and C++ pretty much share the same standard.