Reading of text

Please i need help with a work i'm doing, i'm just a beginner in C++. What i want to do is i have some information integers to be precised in a text file and i need to read this text file such that each line is an array and i can mamipulate each element on each line as members of an array that is line one is a different array with like 10 elements, line 2 is a different array with like 5 elements and so om, i've tried different things and checked some help online but i don't seem to get the anwers
Actually what i'm doing is to validate a tagtree to be sure the tree is not cyclic and my file contains the tree with the first element on each line being the node and the number following it being its children
I would really appreciate any concrete help
Since you don't show any code, I am completely unaware of what you actually know of C++. Therefore, the best I can do is point you towards the concepts that you need to accomplish your task.

For file input/output, read: http://www.cplusplus.com/doc/tutorial/files/

For dynamic memory allocation (to create arrays of varying sizes at runtime), read: http://www.cplusplus.com/doc/tutorial/dynamic/
Yeah, why don't you show some code?
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;



void split(string& s, char c,vector<string>& v) {
int i = 0;
int j = s.find(c);

while (j != string::npos) {

v.push_back(s.substr(i, j-i));

i = ++j;
j = s.find(c, j);


if (j == string::npos)
v.push_back(s.substr(i, s.length( )));

}
}

int main() {
vector <string> lines;
string line;
ifstream inputfile ("tree.txt");
while (getline(inputfile, line)){
if (line.size()==1)
lines.push_back(line);
split(line, ' ', lines);}
for (int i=0; i<lines.size(); i++) {
cout <<lines[i]<<endl;
}
}




Now, this code is leaving out my nodes that do not have any children in the list that is, if a line contains just a number it does not pick dat number on the line it just skips to the next, i understand that is what the split function tells it to do but i've not been able to implement this exclusion in the code, i need help pls
no help yet??
First and foremost, please use code tags. http://cplusplus.com/articles/z13hAqkS/

Second, your code is pushing into the vector 'lines' the original line and the individual items. I guess this may not be correct. Also note that you can have a vector of type int with the input data instead of storing the numbers as strings. I guess this would be more beneficial to your purposes.

Regarding your problem, yes, your split() function doesn't save any data in the collection unless a space character is found. If you only have the node number, then no space is found and nothing is added to the collection.

I'd do this something like the following, taking into account that a string stream can output the numbers for me quite easily and converted to an int.

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <string>
#include <sstream>
#include <fstream>

typedef std::vector<int> NodeItemCollection;
typedef std::vector<int> NodeCollection;

NodeItemCollection Split(const std::string &line, NodeCollection &nodes)
{
    std::istringstream ss = line;
    int value;
    ss >> value;
    if (ss.good())
    {
        //No errors retrieving the node id.
        nodes.push_back(value);
    }
    else
    {
        //The data in variable 'line' is no good.  Return an empty collection.
        //Or if you want to signal the error, raise an exception.   Your choice.
        return NodeItemCollection();
    }
    NodeItemCollection items;
    while (ss.good())
    {
        ss >> value;
        if (ss.fail())
        {
            //Problem retrieving value.  Return what we have.
            //Or, throw an exception to signal the error.  Up to you.
            return items;
        }
        else
        {
            items.push_back(value);
        }
    }
    //Done.  Return the collection of items.
    return items;
}

int main()
{
    std::string line;
    std::ifstream inputFile = "tree.txt";
    NodeCollection nodes;
    while (std::getline(inputFile, line))
    {
        NodeItemCollection items = Split(line, nodes);
        std::cout << "Node ID " << nodes.back() << ":  ";
        if (!items.size()) std::cout << " No children." << std::endl;
        else
        {
            for(NodeItemCollection::const_iterator it = items.begin(); it != items.end(); ++it)
            {
                std::cout << " " << *it;
            }
        }
    }
    return 0;
}
thanks, it really helped
I Have to read some data from txt file (A.txt & B.txt) these files has (3x3) integer values. I wish to read them in to two int array of (3x3) and do some math on them, the only point that I stuck is not able to use getline with int value any help is appreciated.

her is my code

************************************************************
************************************************************


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

int main()
{
string strA[3][3];
string strB[3][3];

ifstream myfileA("A.txt");
ifstream myfileB("B.txt");
int a = 0;
int b = 0;
int lnA=0, lnB=0;
if(!myfileA||!myfileB)
//Always test the file open.
{
cout<<"Error opening output file"<<endl;
system("pause");
return -1;
}
while(!myfileA.eof() || !myfileB.eof() )
{

getline(myfileA,strA[a][b],' ');
lnA++; // this will acount the number of lines that read or number of rows
getline(myfileB,strB[a][b],' ');
lnB++;

if(a ==lnA)
{
a=0;
++b;
getline(myfileA,strA[a][b],' ');
getline(myfileB,strB[a][b],' ');

}
a++;
}
system("pause");



cout<<"A matrix\n";
for (a=0; a<lnA; a++)
{
for (b=0; b<lnB; b++)
{
cout <<strA [a][b];
cout<< "\t";
}
cout<<"\n";
cout<<"************\n";

cout<<"B matrix\n";


for (a=0; a<lnA; a++)
{
for (b=0; b<lnB; b++)
{
cout <<strB [a][b];
cout<< "\t";
}
cout<<"\n";

char res;
cin>> res;
return 0;
}
}
}
@Sarhat:

1. Use code tags. http://cplusplus.com/articles/z13hAqkS/
2. Don't hijack threads. Start your own thread.
Topic archived. No new replies allowed.