Hey, when I run this code:

else if(strcmp(buffer,"#itemSprite") == 0)
{
    char* sprite_filename = "";
    int trans_r;
    int trans_g;
    int trans_b;
    int width;
    int height;
    int numFrames;
    int numCols;
    int frameChangeDelay;
    fscanf(file,"%s",sprite_filename);
    fscanf(file,"%d",&trans_r);
    fscanf(file,"%d",&trans_g);
    fscanf(file,"%d",&trans_b);
    fscanf(file,"%d",&width);
    fscanf(file,"%d",&height);
    fscanf(file,"%d",&numFrames);
    fscanf(file,"%d",&numCols);
    fscanf(file,"%d",&frameChangeDelay);
    item->SetSprite(sprite_filename,D3DCOLOR_XRGB
             (trans_r,trans_g,trans_b),width,height,numFrames,numCols,frameChangeDelay);
}

I get an error on this line: fscanf(file,"%s",sprite_filename); that I don't have access to write to 0x0041ed5b. I'm guessing that's the address of sprite_filename, since the only other variable on that line is file, and we're not writing to it. Why am I getting this error? If I declare char* sprite_filename = ""; , shouldn't I have access to it within the same set of code brackets?

Recommended Answers

All 8 Replies

>> since the only other variable on that line is file, and we're not writing to it.

Oh but yes you are. file is supposted to be a pointer to an open FILE object.

>>sprite_filename
You have not allocated any memory for fscanf() to save the characters. Needs to be either allocated with new or statically allocated char *sprite_filename = new char[255]; // Or this char sprite_filename[255];

>>Oh but yes you are. file is supposted to be a pointer to an open FILE object.
I am writing to file? Aren't I just reading from it? file is declared as FILE* in another part of the code. And I double-check, the address with the issue is indeed sprite_filename.

I'll try the dynamic allocation with new. Thanks.

Why so much C code in a C++ program?

Besides, if you're using 'char', then a char array would be simpler. If you allocate it yourself, then you have to remember to deallocate it as well.

>>Oh but yes you are. file is supposted to be a pointer to an open FILE object.
I am writing to file? Aren't I just reading from it? file is declared as FILE* in another part of the code.

Yes, my mistake. as long as file was opened for either read or read+write.

Why so much C code in a C++ program?

FILE is used for C? What's the C++ way to do file I/O? fstream? If so, I guess I just don't like using the overloaded >> operator, but I suppose I could call the function directly if I wanted to.

FILE is used for C? What's the C++ way to do file I/O? fstream? If so, I guess I just don't like using the overloaded >> operator, but I suppose I could call the function directly if I wanted to.

when coming from C background fstream does take a bit getting used to, but your program could be reduced to this:

ifstream in("filename.txt");
   in >> sprite_filename
       >> trans_r
       >> trans_g
      >> trans_b
      >> width
      >> height
      >> numFrames
      >> numCols
      >> frameChangeDelay;

The above is certainly a lot less typing than all those calls to fscanf(), and less prone to coding errors.

>>when coming from C background
I actually don't come from a C background. C++ was actually the very first programming language I learned (took me a while to understand all of it though, i.e. classes, pointers). I just think the whole idea of using the shift operators to get input, because left and right shifting have nothing to do with getting input or putting output to a stream.

Also, is there a way to check if the end of a file has been reached with ifstream? Because I'm using feof() in my program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.