read file into multiple dimension array

closed account (zbREy60M)
Hello, and thank you for your help :)

I am writing a program that will do some math on some data I have in a file. The problem is that there is different types of data in the file. The data looks like this:

[code]Name GL GB DM
J0030+0451 113.14 -57.61 4.333
J0034
String can hold all of this. You can then go on to cast back to int if you need to do some calculations with them
closed account (zbREy60M)
How would I do that? If a string can hold all of this, how do I do it? Do I create an array of strings? I am sorry, I am very new to C++, and have a little bit more experience with Java, but not familar with the weird string objects of the C language.
Last edited on
closed account (o3hC5Di1)
Hi there,

It seems to me like you would need the fscanf function for this.

Check http://www.cplusplus.com/reference/clibrary/cstdio/fscanf/ , an example usage is provided there.

Hope that helps.

All the best,
NwN
He could also use C++ methods for achieving this. Since his data is only separated by white-spaces, the >> operators would work, wouldn't they?
closed account (zbREy60M)
Do you know of any place where I can get some more detailed examples of how to use that? I read the page, but am still quite confused. I have googled this problem many times, and am still at a loss. I don't have a lot of knowledge with these class based languages.

edit:* or any languages actually :(
Last edited on
closed account (o3hC5Di1)
Hi there,

ResidentBiscuit is probably right there, but I'm not too familiar with that way (or any file handling in C++ for the moment).

Here's a tutorial on the subject:

http://bytes.com/topic/c/insights/652951-how-parse-file-c

Google for "c++ parsing text file to array" or similar shows you quite a few results.

Hope that helps.

All the best,
NwN
closed account (zbREy60M)
Alright, I've been trying to use getline to get a the columns into an array, i went ahead and separated the data into 3 files so it would be easier (i am newbie).

Here is what my data looks like now.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
4.333		
J0034–0721	
J0108–1431		
73.779
34.797
26.833
57.1420
J0437–4715	
J0452–1759		
14.495
39.570
J0613–0200
J0630–2834
N/A
13.977
J0720–3125


And I'm trying to put it into an array called DMArray[123]. The array has 123 slots because i have 123 rows of data. Here is the code I am using:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int main()
{
    string DMArray[123];
	ifstream myfile ("DM.txt");
	if (myfile.is_open())
	{
		for (int i = 0; i < 123; i++)
		{
			getline (myfile,DMArray[i]);
			cout << DMArray[i] << endl;
		
		myfile.close();
		}
	}
	 else cout << "unable to open file";

system("PAUSE");
return 0;
}


When I run the code the data I get is:

1
2
3
4
5
4.333
 
 
 
 


And so on, that means, it seems to only be putting the first piece of data into the array and then leaving the rest blank. help?
Last edited on
closed account (o3hC5Di1)
Hi there,

You're closing the file in your for {} loop.

1
2
3
4
5
6
7
for (int i = 0; i < 123; i++)
		{
			getline (myfile,DMArray[i]);
			cout << DMArray[i] << endl;
		
		myfile.close();
		}


Which means the file will be closed after the first iteration (i=0), so no more data can be fetched.

Try and put that closing statement outside of the for loop :)

Hope that helps,

All the best,
NwN
closed account (zbREy60M)
Thank you so much! I can now do the rest of my project :) I'll come back here if I need any help!
closed account (zbREy60M)
Need help again!

The code I was using made an array of strings, however I need to do mathematical functions on it.

How do I change it from an array of strings to a float so that I can perform operations on it? Or, how do i modify my code so that it will be an array of floats from the very beginning? It seems that getline only works for strings...
closed account (zbREy60M)
Here is my code so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
int main()
{
        string dmArray[123];
	string NAMEArray[123];
	string LONGAArray[123];
	string LATAArray[123];
	double DMArray[123];

	ifstream dmfile ("DM.txt");
	ifstream namefile ("names.txt");
	ifstream longafile ("longa.txt");
	ifstream latafile ("lata.txt");
	ofstream mofile ("dmodel.txt");

	if (dmfile.is_open())
		{
			for (int i = 0; i < 123; i++)
				{
				getline (dmfile,dmArray[i]);
				}
			dmfile.close();
			DMArray[123] = atof(dmArray[123].c_str());
			cout << DMArray[123] << endl;
		}
	 else cout << "unable to open DM file";

    system("PAUSE");
	return 0;

}


The problem here is a few. When I try to cout the DMArray[123] which should be in double format, it only returns "0".

However, when I try to cout dmArray[123] which is an array of strings, that should've been transformed by the point I try to cout it, it puts lots of weird symbols on the screen and my speakers start beeping frantically until windows shuts down.

How do I properly convert this array of strings to a double array so that I can perform mathematical operations on it?
Last edited on
closed account (o3hC5Di1)
Hi there,

As far as I know you would have two options, but I'm sure some of the experts around here could tell you the best solution.

- You could use stringstreams and output the data into floats from there (just have a look at this site's tutorial pages where it has some examples).
- You could use the function sscanf() on your string, which is not true c++, but a C function, but should work nonetheless. More on that here: http://www.cplusplus.com/reference/clibrary/cstdio/sscanf/

All the best,
NwN
Topic archived. No new replies allowed.