0

Hi,

I am trying to read values from a text file that has exactly 5 numbers in each line and all numbers are "tab" separated. numbers could be one digit or two digit.

I am reading it as :

if (map.is_open())
	{
		int i = 0;
		int num;
		while (!map.eof())
		{
			getline(map,line);
			if (line == "")
			{
				continue;
			}
			else
			{
				while (line >> map)
				{
				cout << "Line is " << line << endl;;
				int pos1 = 0;
				Graph[i].value = GetIntValue(line.substr(pos1+1,pos1+1));
				pos1 = line.find("	");
				cout << "Graph[" << i << "] = " << Graph[i].value << endl;
				i++;
//I need to make the reminder as my line now -- > this is killing me as I do not know //the length of line..
				}				
			}
		}
	}

GetIntValue is a small function that takes in a string and returns a int.
basically I need to read each line(lines are exactly 5 in number), store in an array. each line has exactly 5 numbers.

any sort of help would be really appreciated!
3
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by nbaztec
0

Looks like you are doing it the hard way

int n1,n2,n3,n4,n5;

while( map >> n1 >> n2 >> n3 >>n4 >> n5 )
{
   // do something with these numbers
}
0
int val_arr[5][5];
int i=0;
while (file)
    {
      string str;
      int j=0;
      while (getline( file, str, '\t' )) 
        var_array[i][j++] = ( atoi(str) );
      i++;
    }

//User val_array[][]

Edited by nbaztec: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.