trouble with opening and loading a file

Started by
28 comments, last by 00dave 18 years, 1 month ago
oh... That would make a lot of sense, let me check.

Edit: Yeah, in the middle of those blanks spaces is a number telling how many bytes the string is, Thanks man.
Advertisement
How exactly would I read a single byte? like 0C.. I have the stream right before it, but I can't figure out what to use to read it, int is too large, char is a char.. I need something 1 byte that I can use in a for loop :/
use an unsigned char. Then you can just treat it like a number from 0-255 (which it is).
Will it let me use that to allocate memory in a char *? and use it in a for loop?

for example...
unsigned char g;fread(&g,1,1,file);char name[g];for(int x = 0; x < g; x++)


[Edited by - 00dave on March 15, 2006 10:01:45 PM]
Are you trying to read in the size of the string? Just so you know, that's an unsigned int, not a byte:
unsigned int sizeOfName;fread(&sizeOfName, sizeof(unsigned int), 1, file);char* nameBuffer = char[sizeOfName];std::string name;fread(nameBuffer, sizeOfName, 1, file);name = nameBuffer; //now we have the name in a string.delete[] nameBuffer;


Is this what you're trying to do? I'm not sure what the for loop's for.
Oh, I thought you said unsigned char :X

And when I try to create the char array using the unsigned int, I keep getting compiling errors "type char expected"
Quote:Original post by 00dave
Oh, I thought you said unsigned char :X

And when I try to create the char array using the unsigned int, I keep getting compiling errors "type char expected"


Well I did say unsigned char, but that was before I knew what you were doing.

Also, you can't create an auto array with a non-const variable. You have to either use the "new" keyword, or the malloc function. And don't forget to delete/free()!

You can't do this:

char someArray[numberOfElements];

You have to do this:

char* someArray = new char[numberOfElements];

and delete when you're done:

delete[] someArray;
Yeah, that worked.. one more question :P

Will it let me overwrite the name using fwrite once I edit the characters in the array? Just to replace them?
Yes, as long as you have specified input and output as well.

Note though, not only will you have to change the "length" string, but if the length is too long, you may have to shift the whole file downward in order for it to fit. They may have left blank space in the header though.

Oh, and remember to seek back to the beginning of the size string.

Edit: going to bed
I'm not sure... but I think there might be something about these mp3 files that is not allowing me to write to them.. I changed the destination file of my code to a blank file, and it wrote fine, but when its the mp3 it won't write.

This topic is closed to new replies.

Advertisement