reading from file.txt to array of strings.

Aug 30, 2012 at 10:03pm
hello

i have a little question here.
i have read the tutorial, but didn't quite understood how to work with input/output to/from files.

let's suppose i have a file, called file.txt:
1
2
word1, word2, word3
word4, word5, word6


and i create an array in the main():
string mArray[5];


how can i make each one of the array members get one of the words? i mean, how to tell the program "from here to here will be mArray[0], so mArray[0] = "word1". from here to here will be mArray[1], so mArray[1] = "word2", etc"?

i think a 'for' loop would do fine. i just need help to take a serie of characters from the .txt file and convert it to a string. i have no idea on how to do that...

thanks in advance! :)

EDIT: i tought about putting a specific character between the words i want, so the program can separate them. is this possible?
Last edited on Aug 30, 2012 at 10:08pm
Aug 30, 2012 at 10:31pm
You don't need commas between words, remove the commas and this will work fine (be sure to leave spaces between words):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <fstream>

int main()
{
    using namespace std;

    ifstream file("file.txt");
    if(file.is_open())
    {
        string myArray[5];

        for(int i = 0; i < 5; ++i)
        {
            file >> myArray[i];
        }
    }

}
Last edited on Aug 30, 2012 at 10:32pm
Aug 30, 2012 at 11:09pm
really that simple? wow
thanks for the quick answer.
another question, then.

is there any way to make a string hold more than a single word? example: mArray[0] = "word1 word2";
and then, make the same as above, but with these "sentence" strings?

thanks again!
Aug 31, 2012 at 1:54pm
hi everybody! sorry for the double post.
after a bigger search, i found out how to do this, and just wanted to share the solution, in case anyone wants to do the same thing.
how i did it: i store the whole contents of the .txt file in a single string, close the file, and then, split the string, based on searching a specific character: a | bar.

text file test.txt:
helloooooooooo|cool cool|another string|laaaaast one|
the bar "|" is needed to separate words/sentences.

and main.cpp:
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
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

 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[4];

    for (int x=0; x<=3; x++){
        pos2 = test.find("|", pos1); //search for the bar "|". pos2 will be where the bar was found.
        str[x] = test.substr(pos1, (pos2-pos1)); //make a substring, wich 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 |.
    }
 }


EDIT: and the output:
helloooooooooo|cool cool|another string|laaaaast one|

helloooooooooo
pos1:0, pos2:14
cool cool
pos1:15, pos2:24
another string
pos1:25, pos2:39
laaaaast one
pos1:40, pos2:52

Process returned 0 (0x0)   execution time : 0.047 s
Press any key to continue.
Last edited on Aug 31, 2012 at 2:01pm
Topic archived. No new replies allowed.