-3

I have a .txt file as below:

Peter   He is a boy.

Mary    She is a girl.

Tom     It is a cat.

...

I would like to read the file using c++ and store the names into an array of string name[] and store the descriptions into another array of string description[] but without the fullstop ".".

Names and descriptions are separated with a tab.

How can I do it?

Matt Johnson-Pint
  • 230,703
  • 74
  • 448
  • 575

2 Answers2

0

Assuming you already know how to read an entire line of a text file, you could delimit the text with a specific char (i use ',' a lot).

Fore example you could read the first line in "Peter He is a boy." but if you were to set it up as a delimited string it can look like this "Peter,He is a boy.". then you can loop through each char of the string till the particular char in question = ',' and you can then split the string accordingly.

Something like this may what you're looking for:

#include <string>
#include <sstream>
#include <vector>

int main()
{
    vector<string> lines(3);
    vector<string> names(3);
    vector<string> descriptions(3);
    lines.at(0) = "Peter,He is a boy.";
    lines.at(1) = "Mary,She is a girl.";
    lines.at(2) = "Tom,It is a cat.";

    for(int i = 0; i < lines.size(); i++) {
        for(int j = 0; j < lines[i].size(); j++) {
            if(lines[i][j] == ',') {
                for(int d = 0; d < j; d++) {
                    stringstream ss;
                    ss << lines[i][d];
                    ss >> names.at(i);
                }
                for(int d = j + 1; d < lines[i].size(); d++) {
                    if(lines[i][d] != '.') {
                        stringstream ss;
                        ss << lines[i][d];
                        ss >> descriptions.at(i);
                    } else {
                        break;
                    }
                }
                break;
            }
        }
    }
}

EDIT: This code will look for 3 spaces instead of a single char ','.

#include <string>
#include <sstream>
#include <vector>

int main()
{
    vector<string> lines(3);
    vector<string> names(3);
    vector<string> descriptions(3);
    lines.at(0) = "Peter,He is a boy.";
    lines.at(1) = "Mary,She is a girl.";
    lines.at(2) = "Tom,It is a cat.";

    for(int i = 0; i < lines.size(); i++) {
        for(int j = 0; j < lines[i].size(); j++) {
            if(lines[i][j] == 0x20 && lines[i][j + 1] == 0x20 && lines[i][j + 2] == 0x20) {
                for(int d = 0; d < j; d++) {
                    stringstream ss;
                    ss << lines[i][d];
                    ss >> names.at(i);
                }
                for(int d = j + 3; d < lines[i].size(); d++) {
                    if(lines[i][d] != '.') {
                        stringstream ss;
                        ss << lines[i][d];
                        ss >> descriptions.at(i);
                    } else {
                        break;
                    }
                }
                break;
            }
        }
    }
}
miken32
  • 42,008
  • 16
  • 111
  • 154
Sam
  • 350
  • 2
  • 9
  • Is there any method that can separate a line into two strings without adding a ',' ? Or is there any method that read a line two times and each time assigning name or description into the array of string? Sth like: fin >> name[i]; getline (fin, description[i]); – Jackie Cheung Mar 19 '15 at 13:41
  • Well theoretically a delimitation can be of anything, even the set of spaces in your original string can be used but using a ',' is just easier. – Sam Mar 19 '15 at 13:45
  • @JackieCheung I updated the answer to help with your original data. If this is deemed to be the sufficient answer to your question you should tick it as answered by clicking on the grayed out tick on the left of the answer so it is green. – Sam Mar 19 '15 at 13:54
0

I only know how to read an entire line or a word.

This is what I have written:

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

int main(){
    string line[6153];

    ifstream fin;
    fin.open("dict.txt");
    if (fin.fail()){
        cout << "File input failed.\n";
        exit(1);
    }

    else{
        int i;
        for (i = 0; i < 6153; i++){
            getline(fin, line[i]);      
        }
    }
    fin.close();
    return 0;
}

p.s. there are 6153 lines in "dict.txt".