Jump to content
  • Advertisement
Sign in to follow this  
Gavinl

having problems with reading/writing data

This topic is 4642 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
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
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!