c++ data file question

A-HOL

[H]ard|Gawd
Joined
Oct 18, 2001
Messages
1,630
ok, I haven't messed with c++ in quite a while and frankly I just can't remember how to do this, I'm working on this for work and basically here is what I need to do
I have a txt file with numbers in it that is generated from input from a machine in an array type style 20 accross and 30 down so:

2 3 14 38 22........
4 53 4 33 21...
.
.
.
.
and so on, I need to read these numbers into a 2 element array that is just like its set up so I am able to access each individual number by a row and collumb. i know this isn't very hard, I've done it before but I just can't seem to remember how.

I appreciate anyones help
 
Reading from STDIN. And I'm not sure how to convert from a string to a double or int in C++.

Code:
#include <iostream>
#include <string>

using namespace std;

int main()
{
  string line;

  while (getline(cin, line)) {
    // Tokenize the line on spaces
    // Other processing
  }
}

Post back if you need more direction.
 
I haven't compiled this to make sure the syntax is correct, but you can try this.

Code:
int numberArray[30][20];

ifstream fin( "data.txt" );
for (int i = 0; i < 30; ++i) {
    for (int j = 0; j < 20; ++j) {
        fin >> numberArray[i][j];
    }
}

fin.close();

A good reference is this C++ website.
 
well that does basically what I've done already, but the problem is it gets single characters as apposed to double like I need it to so it can do double digit numbers
 
A-HOL said:
well that does basically what I've done already, but the problem is it gets single characters as apposed to double like I need it to so it can do double digit numbers
well, given that solution, just process as follows: if it's a digit, multiply temp by ten and add the new input to temp. if it's not, put temp into permanent storage and reset temp to 0. You don't want to write something tied to an input of a fixed number of characters/digits if at all possible, that's way too fragile. You'll also want to be able to detect a negative sign.
 
A-HOL said:
well that does basically what I've done already, but the problem is it gets single characters as apposed to double like I need it to so it can do double digit numbers

Based on that code, and what you're describing, it sounds like BROKEN BEHAVIOUR(tm). The solution above, as posted, should work correctly. Would you care to post your source? What compiler are you using?
 
Back
Top