# Reading in data from file in certain format

Printable View

• 11-22-2012
fpghost
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?
• 11-22-2012
Adak
Quote:

Originally Posted by fpghost
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! :cool:

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. ;)
• 11-22-2012
fpghost
Quote:

Originally Posted by Adak
Welcome to the forum, fpghost! ;)

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

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?
• 11-22-2012
Adak
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?
• 11-22-2012
fpghost
Quote:

Originally Posted by Adak
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?
• 11-22-2012
Adak
I'd just repost it. It's a slow day being Thanksgiving and all.
• 11-22-2012
Salem
cboard.cprogramming.com/cplusplus-programming/152521-reading-data-file-certain-format.html