Public Group

This topic is 4642 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey guys, I managed to write a program that woudl read in an .ASE file (its a file that 3d studio max can export about a model, and its an ascii file). But since it's an ascii file, its pretty large. So i wanted to resave all the data to a data file. But i kept having problems reading the data once I wrote it to the file. I coudln't figure out what was wrong, so I wrote a smaller program to just write 750,000 floating point numbers to a file, and then read them back in. And i'm having the same problem I did before, It will write all the data out, but it won't read them all in. here's the entire code:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

FILE* mFile = fopen(location, "r");
float * numbers;

numbers = new float[750000];

if(numbers == NULL){
printf("not enough memory\n");
system("PAUSE");
}

system("PAUSE");

fclose(mFile);

return(numbers);
}

void write(float * numbers, char* location){

FILE* mFile = fopen(location, "w");

printf("wrote %d numbers\n", fwrite(numbers, sizeof(float), 750000, mFile));
fclose(mFile);
}

int main(int argc, char *argv[])
{
float * numbers;
srand( time(NULL));

numbers = new float[750000];

for(int x=0;x<750000;x++){
numbers[x] = (float) ((rand()%RAND_MAX))/RAND_MAX * 1000;
}

write(numbers, "test.txt");     //write the numbers to the data file
delete[] numbers;

delete[] numbers;

system("PAUSE");
return EXIT_SUCCESS;
}


I hope someone can tell me what i'm doing wrong, I've spent almost 5 hours trying to get this read/write working :( Thanks Gavin

Share on other sites
Have yout tried writing say 10 numbers to the file (something readable) to verify that they are all being written correctly?

Matt

Share on other sites
It seems to me that you are opening the file in ASCII mode, but reading/writing the data in binary mode. Try adding a 'b' to the mode string in fopen. ie. "rb", "wb".

Share on other sites
Holy crap Rpg_code_master. I LOVE YOU SO MUCH.

It worked. I've been pulling my hair out over this.

Thank's so much.

Share on other sites
For the future, matthughson has a really good point: your test data files to start with should be pretty small so you can see that you are in fact reading and writing what you think you're reading and writing. That is, with our best debug tools: notepad and our eyes <3

Share on other sites
Yes, I did test it with smaller numbers, and it did work properly with smaller numbers (between 20-70 float values), but when i had more than that, it would mess up.

1. 1
Rutin
24
2. 2
3. 3
4. 4
JoeJ
18
5. 5

• 14
• 19
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631761
• Total Posts
3002179
×