#include <stdio.h>
#include <stdlib.h>
#include <time.h>
float* read(char* location){
FILE* mFile = fopen(location, "r");
float * numbers;
numbers = new float[750000];
if(numbers == NULL){
printf("not enough memory\n");
system("PAUSE");
}
printf("Number's read: %d\n\n", fread(numbers, sizeof(float), 750000, mFile));
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;
numbers = read("test.txt"); //read the numbers back in
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
having problems with reading/writing data
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:
Have yout tried writing say 10 numbers to the file (something readable) to verify that they are all being written correctly?
Matt
Matt
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".
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.
It worked. I've been pulling my hair out over this.
Thank's so much.
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement