Hello, I want to write a program that reads a text file, which contains a table of data where the columns are separated by spaces, and store each column in different arrays with different data types.

For example, let's say I have the following data:

1 hello 1.5
2 world 2.5

I want to store the data as:

int numberArray[] = {1,2}
string wordArray[] = {"hello", "world"}
double decArray[] = {1.5, 2.5}

Here is what I have so far:

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
   string str[2];

   ifstream myFile("File.txt");

   for(int i = 0; i < 3; i++)
   {
         getline(myFile, str[i]);
   }

   return 0;
}

This stores the data in rows with only string type:
str[0] = {"1", "hello", "1.5"}
str[1] = {"2", "world", "2.5"}


So I want to:
1- Store columns, not rows
2- Change the values' data types


Things I tried but didn't work:

string numberArray[2], wordArray[2], decArray[3];

for(int i = 0; i < 3; i++)
{
   getline(myFile, numberArray[i], ' ');
   getline(myFile, wordArray[i],   ' ');
   getline(myFile, decArray[i],    ' ');
}
string str;
string numberArray, wordArray, decArray;

getline(myFile, str);

numberArray = str.substr(0,1);
wordArray   = str.substr(2,3);
decArray    = str.substr(3,4);

Thank you

Recommended Answers

All 6 Replies

The extraction operator breaks the input stream at a whitespace (a 'space' or a newline, among others). Take advantage of that and try using multiple input statements:

while (ifstream >> rowNumVar) {
  ifstream >> wordVar >> floatValVar;
}

I have a lot of data and I used the extraction operator to store each column in different arrays. But what I want to figure out is a shortcut for my code and also how to convert from an array of string to an array of integer and float.

Here is my code:

string busNumber[14], name[14], lfaNumber[14], lzNumber[14], type[14], finalVol[14],  finalAng[14], loadMW[14], loadMVAR[14], genMW[14], genMVAR[14], baseKV[14], volts[14], maxMVAR[14], minMVAR[14], shuntCon[14], shuntSus[14], remote[14];

  string str;

  while(inputFile >> str)
    {
    for(int a = 0; a < 1; a++){
      for(int b = 0; b < 1; b++){
        for(int c = 0; c < 1; c++){
          for(int d = 0; d < 1; d++){
            for(int e = 0; e < 1; e++){
              for(int f = 0; f < 1; f++){
                for(int g = 0; g < 1; g++){
                  for(int h = 0; h < 1; h++){
                    for(int i = 0; i < 1; i++){
                      for(int j = 0; j < 1; j++){
                        for(int k = 0; k < 1; k++){
                          for(int l = 0; l < 1; l++){
                            for(int m = 0; m < 1; m++){
                              for(int n = 0; n < 1; n++){
                                for(int o = 0; o < 1; o++){
                                  for(int p = 0; p < 1; p++){
                                    for(int q = 0; q < 1; q++){
                                      for(int r = 0; r < 1; r++){

                                        inputFile >> busNumber[a];
                                        inputFile >> name[b];
                                        inputFile >> lfaNumber[c];
                                        inputFile >> lzNumber[d];
                                        inputFile >> type[e];
                                        inputFile >> finalVol[f];
                                        inputFile >> finalAng[g];
                                        inputFile >> loadMW[h];
                                        inputFile >> loadMVAR[i];
                                        inputFile >> genMW[j];
                                        inputFile >> genMVAR[k];
                                        inputFile >> baseKV[l];
                                        inputFile >> volts[m];
                                        inputFile >> maxMVAR[n];
                                        inputFile >> minMVAR[o];
                                        inputFile >> shuntCon[p];
                                        inputFile >> shuntSus[q];
                                        inputFile >> remote[r];

                                      }}}}}}}}}}}}}}}}}}
    }

Instead of reading the entire line at a stretch and then trying to cut it convert it etc try reading column by column with space as the delimiter as

//istream& getline ( istream& is, string& str, char delim );
getline(fPtr,str,' ');

If you know exactly how the file is stored then can always read it effectively but developing a generic program may be difficult.You can refer to getline for more details of getline.

I have a lot of data and I used the extraction operator to store each column in different arrays. But what I want to figure out is a shortcut for my code and also how to convert from an array of string to an array of integer and float.

Here is my code:

string busNumber[14], name[14], lfaNumber[14], lzNumber[14], type[14], finalVol[14],  finalAng[14], loadMW[14], loadMVAR[14], genMW[14], genMVAR[14], baseKV[14], volts[14], maxMVAR[14], minMVAR[14], shuntCon[14], shuntSus[14], remote[14];

  string str;

  while(inputFile >> str)
    {
    for(int a = 0; a < 1; a++){
      for(int b = 0; b < 1; b++){
        for(int c = 0; c < 1; c++){
          for(int d = 0; d < 1; d++){
            for(int e = 0; e < 1; e++){
              for(int f = 0; f < 1; f++){
                for(int g = 0; g < 1; g++){
                  for(int h = 0; h < 1; h++){
                    for(int i = 0; i < 1; i++){
                      for(int j = 0; j < 1; j++){
                        for(int k = 0; k < 1; k++){
                          for(int l = 0; l < 1; l++){
                            for(int m = 0; m < 1; m++){
                              for(int n = 0; n < 1; n++){
                                for(int o = 0; o < 1; o++){
                                  for(int p = 0; p < 1; p++){
                                    for(int q = 0; q < 1; q++){
                                      for(int r = 0; r < 1; r++){

                                        inputFile >> busNumber[a];
                                        inputFile >> name[b];
                                        inputFile >> lfaNumber[c];
                                        inputFile >> lzNumber[d];
                                        inputFile >> type[e];
                                        inputFile >> finalVol[f];
                                        inputFile >> finalAng[g];
                                        inputFile >> loadMW[h];
                                        inputFile >> loadMVAR[i];
                                        inputFile >> genMW[j];
                                        inputFile >> genMVAR[k];
                                        inputFile >> baseKV[l];
                                        inputFile >> volts[m];
                                        inputFile >> maxMVAR[n];
                                        inputFile >> minMVAR[o];
                                        inputFile >> shuntCon[p];
                                        inputFile >> shuntSus[q];
                                        inputFile >> remote[r];

                                      }}}}}}}}}}}}}}}}}}
    }

:-O :confused: What exactly are you trying to do? I seriously doubt you need all those nested loops, especially if you have each one only running once.

Please describe the format of your data file better then post a couple lines from it.

I tried getline but it didn't work as I wanted. Here are 4 columns (or 14 rows) from my data file:

1     HV  3 1.060
   2     HV  2 1.045
   3     HV  2 1.010
   4     HV  0 1.019
   5     HV  0 1.020
   6     LV  2 1.070
   7     ZV  0 1.062
   8     TV  2 1.090
   9     LV  0 1.056
  10     LV  0 1.051
  11     LV  0 1.057
  12     LV  0 1.055
  13     LV  0 1.050
  14     LV  0 1.036

I want to read the data in a way to have the following arrays:

int    intAry[14]    = {1,2,3,4,5,6,7,8,9,10,11,12,13,14}
string strAry[14]    = {"HV","HV","HV","HV","HV","LV","ZV","TV","LV","LV","LV","LV","LV","LV"}
int    intAry2[14]   = {3,2,2,0,0,2,0,2,0,0,0,0,0,0}
float  fltAry[14]    = {1.060,1.045,1.010,1.019,1.020,1.070,1.062,1.090,1.056,1.051,1.057,1.055,1.050,1.036}

And this long way works but it's like what Fbody said, I doubt that I need all of those fors. I'm using one of Microsoft's MFC collection classes for my project and I'm stuck in this part.

You can do this with a single while loop, as I previously demonstrated. You just need to add an index variable. You're trying to make more work out of it than is necessary.

ifstream infile(fileName.txt);
int i = 0;
while (ifstream >> intAry[i]) {
  ifstream >> strAty[i];
  ++i;
}

The extraction operator, which is '>>', interrupts the input at any whitespace it detects. The characters 'space' and 'newline' are whitespace characters. If you set up your read statements correctly, and your file is consistently formatted, a loop structure such as I have demonstrated is sufficient. You just need to adapt the actual statements used to your data.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.