Thread: Reading in data from file in certain format

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    5

    Reading in data from file in certain format

    I have two text files containing the complex values of some function:
    f[0, 6] = 0.00006639570. - 6.00456*I; f[1, 6] = 0.00003602 +0.0039842775.*I; f[0, 7] = 0.0000663- 6.00383226386*I; f[1, 7] = 60233709 + 0.00039842*I; . . . and similary another with g[x,y].

    I would like my code to perform a loop that reads in the data from these files into a list of vectors of complex numbers that I can then utilise, can this be done?

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by fpghost View Post
    I have two text files containing the complex values of some function:
    f[0, 6] = 0.00006639570. - 6.00456*I; f[1, 6] = 0.00003602 +0.0039842775.*I; f[0, 7] = 0.0000663- 6.00383226386*I; f[1, 7] = 60233709 + 0.00039842*I; . . . and similary another with g[x,y].

    I would like my code to perform a loop that reads in the data from these files into a list of vectors of complex numbers that I can then utilise, can this be done?
    Welcome to the forum, fpghost!

    This is C, we do everything we can imagine, damn near!

    Looks to me like you want to read in some floating point numbers from a file, into an array[] of doubles.

    Why not run through a bit of the C tutorial (link is in the C Tutorial tab at the top of the forum), and get familiar with some C basics.

    Then get started, and post back if and when you have a problem. We are a bit of indentation nuts, so please, use [code] tags around your code and indent just as if you were totally sane, even though that may not otherwise be proven.

  3. #3
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Quote Originally Posted by Adak View Post
    Welcome to the forum, fpghost!

    This is C, we do everything we can imagine, damn near!

    Looks to me like you want to read in some floating point numbers from a file, into an array[] of doubles.

    Why not run through a bit of the C tutorial (link is in the C Tutorial tab at the top of the forum), and get familiar with some C basics.

    Then get started, and post back if and when you have a problem. We are a bit of indentation nuts, so please, use [code] tags around your code and indent just as if you were totally sane, even though that may not otherwise be proven.
    Hi, thanks, I will try and use the code tags in future.

    I know I can use cin to read in the data. The problem is the data list is of variable length and is also in a tricky format (see OP), so I'm guessing some kind of vector might be best. I really need to be able to then access it for any given parameters, e.g. schematically: f[1,2] and get back the correct complex number, say "1.25+9.87i". Hence I was thinking some kind of vector of complex type container, but was hoping someone could help with the details..

    It is possible I could change this input format slightly, but nevertheless it boils down to me having complex numbers indexed by two parameters (x,y); I need to read them in and then be able to access the complex number for a given point in the parameter space.

    Maybe I need some kind of HashTable?
    Last edited by fpghost; 11-22-2012 at 11:02 AM.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I would use a struct (a record), with the struct members being these other parts you need to save and reference during your searches. Typically, we use fixed length arrays (although the very latest standard for C adds variable length arrays), make them a bit oversized and in the rare case that isn't enough, we reallocate the array to a larger size with realloc() (defined in the stdlib.h include file).

    If you are using cin however, that is C++, and they have container that automatically alter their size. Perhaps you were looking for the C++ forum?

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    5
    Quote Originally Posted by Adak View Post
    I would use a struct (a record), with the struct members being these other parts you need to save and reference during your searches. Typically, we use fixed length arrays (although the very latest standard for C adds variable length arrays), make them a bit oversized and in the rare case that isn't enough, we reallocate the array to a larger size with realloc() (defined in the stdlib.h include file).

    If you are using cin however, that is C++, and they have container that automatically alter their size. Perhaps you were looking for the C++ forum?
    AH yes, sorry I was looking for C++ forum. Is it possible to move my thread there? or I can repost it?

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I'd just repost it. It's a slow day being Thanksgiving and all.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    cboard.cprogramming.com/cplusplus-programming/152521-reading-data-file-certain-format.html
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-11-2011, 04:45 AM
  2. Replies: 13
    Last Post: 05-31-2009, 11:30 AM
  3. How to write image data to binary PGM file format(P5)?
    By tommy_chai in forum C Programming
    Replies: 6
    Last Post: 11-03-2007, 10:52 PM
  4. Replies: 2
    Last Post: 06-16-2005, 10:03 AM
  5. reading data format into program
    By lambs4 in forum C Programming
    Replies: 1
    Last Post: 10-23-2003, 02:27 PM