0
wer234 cwx1 20139   

asd223 cwx2 09678 

sda232 cwx3 45674 

ukh134 cwx4 23453 

plo209 cwx5 09573 

How can I read data into three string array? first column into 1st string array, second column into 2nd string array, third column into 3rd string array. This is the code I tried, but the last array take into the line.

#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

using namespace std;
 int main(){
    //load the text file and put it into a single string:
    std::ifstream in("test.txt");
    std::stringstream buffer;
    buffer << in.rdbuf();
    std::string test = buffer.str();
    std::cout << test << std::endl << std::endl;

//create variables that will act as "cursors". we'll take everything between them.
size_t pos1 = 0;
size_t pos2;

//create the array to store the strings.
std::string str[5];
std::string str2[5];
std::string str3[5];

int x;
int y;

for(y=0; y<5;y++){

    for ( x=0; x<3; x++){
        
        pos2 = test.find(" ", pos1); //search for the bar "|". pos2 will be where the bar was found.
    
       if(x==0){
       
        str[y] = test.substr(pos1, (pos2-pos1)); //make a substring, which is nothing more 
    
       }else if(x==1){
       
        str2[y] = test.substr(pos1, (pos2-pos1)); //make a substring, which is nothing more 
    
       
       }else if(x==2){
       
        str3[y] = test.substr(pos1, (pos2-pos1)); //make a substring, which is nothing more 
       
       }                                       //than a copy of a fragment of the big string.
      //  std::cout << str[x] << std::endl;
       // std::cout << "pos1:" << pos1 << ", pos2:" << pos2 << std::endl;
        pos1 = pos2+1; // sets pos1 to the next character after pos2. 
                         //so, it can start searching the next bar |.
    }
}

for (int p=0; p<5; p++){
    
    cout << str[p] <<endl;
    cout << str2[p] <<endl;
    cout << str3[p] <<endl;
}

    return 0;
 }
2
  • 1
    You should have simply used operator>>. Commented May 17, 2016 at 20:02
  • 2
    Why not use operator>> with the ifstream directly, do this in the for loop with index variable y in >> str[y] >> str2[y] >> str3[y]; Commented May 17, 2016 at 20:05

2 Answers 2

2

Putting the whole file in one string can be very inefficient if you consider really big files.

The goal you are trying to achieve isn't really that complicated as you'd expected(at least in c++). You can just do:

for(size_t ind = 0; ind < 5; ++ind)
    in >> str[ind] >> str2[ind] >> str3[ind];
Sign up to request clarification or add additional context in comments.

2 Comments

yes, >> operator is simply efficient but what if my data got space between like a name:Adam Maxwell. I need to save the whole name in 1 string. But the >> operator will think Adam as 1 string, Maxwell be the another one.
To be clear: what is your delimiter? Is it space or "|" because both of them exist in your code?
0

I tried it in a different way, let's understand the simple code below.

while(in.good())
{
string stri;
int i=1,a=0,b=0,c=0;
in >> stri;

switch(i%3) {
case 1:
str[a]=stri;
a++;
break;
case 2:
str2[b]=stri;
b++;
break;
case 3:
str3[c]=stri;
c++;
break;
}
i++;

}

Here variables a,b and c counts array index. Every loop, it calculates (i%3) and fills the correct array.

Code may have some issues, it is just for idea. I did not even test it.

NOTE THAT this code will work if and only if you have 3 columns.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.