Sign in to follow this  
Gavinl

having problems with reading/writing data

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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this