Thread: Simple File I/O Problem

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    3

    Simple File I/O Problem

    Up to this point, I have been doing my work in Matlab and reluctantly, Labview. I am trying to learn C to write some fast data analysis programs. As practice, I am currently working on generating a noisy exponential, using a simple fitting algorithm to find the exponential constant and writing the results to a binary file. (I am reading the binary file with Matlab to check my results.)

    Everything seems to work fine is I use array sizes of 10 or less. If I use an array size greater than 10, the first tens elements of the time array look fine, all other elements are gibberish.

    Any suggestions about this particular problem, or any other suggestions on my code, would be helpful.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #include <time.h>
    
    int main()
    {
        int length;
        length=200;
    
        struct data
        {
            int time[length];
            double sig[length];
            double fit[length];
        };
    
        struct data sig1;
        int i,zero_moment;
        double tau, noise,first_moment, tau_measured;
        unsigned int iseed = (unsigned int)time(NULL);
        srand(iseed);
    
        FILE *fp;
        if((fp=fopen("C:\\Users\\Brian\\Desktop\\C Files\\test2\\test2_data.bin", "w")) == NULL)
        {
            printf("Cannot save file");
        }
    
        tau=2;
        noise=0.001;
        zero_moment=0;
        first_moment=0;
    
        for (i=0; i<length; i++)
        {
            sig1.time[i]=i;
            sig1.sig[i]=exp(-i/tau)+noise*((rand() % 100)-50) ;
        }
    
        for (i=0; i<length; i++)
        {
            zero_moment=zero_moment+sig1.time[i];
            first_moment=first_moment+sig1.time[i]*sig1.sig[i];
        }
    
        tau_measured=(double)zero_moment/(2*first_moment);
    
        for (i=0; i<length; i++)
        {
            sig1.fit[i]=exp(-(double)i/tau_measured);
        }
    
        fwrite(sig1.time, sizeof(sig1.time[0]), sizeof(sig1.time)/sizeof(sig1.time[0]), fp);
        fwrite(sig1.sig, sizeof(sig1.sig[0]), sizeof(sig1.sig)/sizeof(sig1.sig[0]), fp);
        fwrite(sig1.fit, sizeof(sig1.fit[0]), sizeof(sig1.fit)/sizeof(sig1.fit[0]), fp);
    
        fclose(fp);
        return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I would be willing to bet a moderate amount of money that you don't want a struct of arrays, but an array of structs. Define your struct before main (without arrays), and then make an array of them.

  3. #3
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by tabstop View Post
    I would be willing to bet a moderate amount of money
    "..bet you dollars to doughnuts" is probably what you were looking for there....
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Code:
        int length;
        length=200;
    
        struct data
        {
            int time[length];
            double sig[length];
            double fit[length];
        };
    You're declaring a struct based on a run-time length.

    If length is fixed, then you need
    #define length 200

    > if((fp=fopen("C:\\Users\\Brian....bin", "w")) == NULL)
    Use "wb" as the mode for binary files on windose.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    Thanks for the suggestions. I tried Salem's suggestions and they worked well.

    As I said, I am used to Matlab where using for loops to do array math like this is far from ideal. Is this the same in C? For example, is used a for loop to add the elements of the zero_moment and first_moment arrays. Is this the best way to do this?

    tabstop, why would an array of structure be preferable to the reverse?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    That's because Matlab is doing the loops internally.

    I'm assuming that a data item consists of one time and its corresponding sig and fit (of which you have 200 data pieces), as opposed to you having one single solitary piece of data which happens to have 600 elements to it (which is what your code is currently claiming).

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Your file opening code simply displays a message if the file could not be opened. Shouldn't you abort the program at that point? You don't want to have that part fail and yet still go through the rest of the code only to try and output to a file that isn't even open.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by blee66 View Post
    tabstop, why would an array of structure be preferable to the reverse?
    That's about data organization... take an example where you have hundreds of students... you need to track their name, addres, phone number and home room... does it make sense to you to have 1 struct with arrays hundreds of elements long? I hope not... how about 1 struct for each student and no arrays?

    In your case, this makes a whole lot more sense...
    Code:
        int length;
        length=200;
    
        struct data
        {
            int time;
            double sig;
            double fit;
        };
    
        struct data sig1[length];
    Now each struct holds one data point... you can sort, shuffle, add and delete without separating time sig and fit... each data point is separate and unique.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    3
    I fear we are slowly approaching mere semantics, but I think what I have makes sense for my (eventual) application.

    Here is what I am slowly trying to do: I currently have a fairly large Labview program which controls several instruments including a high speed digitizer. The digitizer will provide a single data set which contains a time array (many hundreds of elements long) and a signal array (also hundreds of elements long). For each of the data sets, I need to perform various fitting and filtering. I would like this process to repeat as fast as possible (1 kHz or more).

    As I understand it, I can write snippets of C code and insert them into labview, which (I am hoping) will execute much faster than the labview G code. While I have little interest in writing the control for all my instruments in C, the data analysis/filtering part seems doable.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by blee66 View Post
    I fear we are slowly approaching mere semantics, but I think what I have makes sense for my (eventual) application.
    Trust me, it is not mere semantics.

    But you go right ahead and do as you wish...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple file processig problem
    By farukyaz in forum C Programming
    Replies: 13
    Last Post: 05-14-2011, 03:28 PM
  2. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  3. File I/O:simple code problem
    By Ardetcho in forum C Programming
    Replies: 4
    Last Post: 07-18-2006, 10:32 AM
  4. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 10:49 AM
  5. Simple End of file problem
    By tegwin in forum C++ Programming
    Replies: 7
    Last Post: 03-04-2002, 01:05 PM