• Advertisement
Sign in to follow this  

having problems with reading/writing data

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

If you intended to correct an error in the post then please contact us.

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>

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

Share this post


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

Matt

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement