Data file question

This is a discussion on Data file question within the C Programming forums, part of the General Programming Boards category; Hi folks . For the second part of my assignment at university, I need to start storing values in a ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    3

    Data file question

    Hi folks .

    For the second part of my assignment at university, I need to start storing values in a file.

    Basically, I'm not too keen on posting my code as the assignment is still active, and I'm sure other people will end up finding this place when looking for assistance with their code. I've still got the best part of a month to get this finished, so expect a flurry in 3-4 weeks time .

    I need to store the contents of a multidimensional array in the file, and a couple of other variables. I'm just wondering whether anyone can give me any pointers as to where I should start? I've had a look through a couple of books and the file examples on this site, but they seem to be geared toward just reading files, rather than reading individual values from the file.

    My file needs to contain the following:-

    A list of distances, to be read and stored in a multidimensional array - possibly edited and then saved back out to the file.
    A cost per mile value, possibly edited and then possibly saved out to the file again.

    I don't want any routines from people on here, all I'd like are a few hints as to the best way to store the data in the file, and a hint as to how I could read the data into the values within my code .

    Sorry, I guess my post is fairly babbling. I'll return to my little rock now .

    Thanks in advance!

    --Rich

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I'm not too keen on posting my code
    Then it will be hard to help you with specific code problems later.

    >A list of distances, to be read and stored in a multidimensional array
    Fixed size or variable? If it's fixed then the program will be easy:
    Code:
    /*
      Assuming file formatted like so:
    
      0 1 2 3 4
      5 6 7 8 9
      10 11 12 13 14
      15 16 17 18 19
    
      20
    */
    FILE *in;
    double dist[M][N];
    double cpm;
    int i, j;
    
    /* Open the file for reading */
    
    for ( i = 0; i < M; i++ ) {
      for ( j = 0; j < N; j++ )
        fscanf ( "%lf", &dist[i][j] );
    }
    fscanf ( "%lf", &cpm );
    
    /* Close the file */
    /* Make changes */
    /* Open the file for writing */
    
    for ( i = 0; i < M; i++ ) {
      for ( j = 0; j < N; j++ )
        fprintf ( "%f ", dist[i][j] );
      fprintf ( "\n" );
    }
    fprintf ( "\n%f\n", cpm );
    
    /* Close the file */
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    Thanks for that . I'll post my code if I need specific help - though I'm keen to avoid it, if at all possible.

    Regarding formatting the file data, the data should have a static number of values. But I'd like to go slightly above and beyond, and create it so further values can be added at a later date. This will mess up the reading of the file when using the above template - is it possible to create the data file with a data structure similar to below?

    Code:
    Array = 1 3 67 32 6546 454 2323 6565 etc etc 4543 22 78 5
    Cost = 100
    I'm not too sure if that is understandable or not... it's been a long day .

    Thanks for your help .

    --Rich

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >is it possible to create the data file with a data structure similar to below?
    Of course, but you will need to consider both reading and writing the "Array = " and "Cost = " parts properly. Since you want to allow for a variable length matrix, marking the beginning of new data is a good thing, but complicates your parsing algorithm. But before getting into that, write the simplest program that works and meets the requirements. Then you can make it better.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Mar 2004
    Posts
    3
    Fair point, I'll have a crack at implementing it later today . Thanks for your thoughts.

    --Rich

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  2. Please Help Reading Data From A File
    By NickHolmes in forum C Programming
    Replies: 5
    Last Post: 05-29-2005, 11:24 PM
  3. gcc problem
    By bjdea1 in forum Linux Programming
    Replies: 13
    Last Post: 04-29-2002, 06:51 PM
  4. File Database & Data Structure :: C++
    By kuphryn in forum C++ Programming
    Replies: 0
    Last Post: 02-24-2002, 10:47 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 02:58 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21