Structs, linked list

Pages: 12
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
struct PNGinfo
{
    std::string filename;
    int width, height;
}

PNGinfo readPNG(std::string file_name)
{
    //open file, check if it is an png (maybe throw an exeption if it isn't)
    //Get width and height
    PNGinfo temp;
    temp.filename = file_name;
    temp. width = //png width
    temp height = //png height
    return temp;
}

int main()
{
    std::list<PNGinfo> files;
    //loop for all files in text.txt
        files.emplace_back( readPNG(filename) )
    //... 

}
I did this. Can anyone take a look at lines 16. I don't know what is wrong?
http://pastebin.com/EtVRZ5C0
Last edited on
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.
Oh I forgot to mention I am working in standard C.
Would strcpy work?
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.
I don't know why but I can't seem to get it how to use strncpy. Can someone please edit that line for me. Thank you.
Hmm... O.o? This isn't working? pNode->ime = strncpy(besede[i], pNode->ime, 500);
look at the example:
a) strncpy is a void function.
b) sintax is strcpy( target, source, limit ). It copies characters from source to target.
Can you please write that line.
strncpy(pNode->ime, besede[i], strlen(besede[i]) );
Do not forget to allocate memory for pNode->ime beforehand!
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)'
1) Made ima be of type char*, not char

did you do that?
I have it like this now. http://pastebin.com/hWxDwQUD
Last edited on
Sorry, I misspelled variable name.
Line 6: char ime; char* ime;
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 


You might also consider png++
http://www.nongnu.org/pngpp/
Last edited on
Now I changed the char ime to char* ime and it still gives me null. http://pastebin.com/RfsQEaLj char ime[500] gives me an error.
Last edited on
closed account (zb0S216C)
MiiNiPaa wrote:
"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.

MiiNiPaa wrote:
"You might notice that this is C++ forum"

Again, C++ is C.

Wazzak
Pages: 12