how to read data from .txt if the row is not fixed?

hello guys, hope u're fine. i would like to ask your opinion on how to read the data from text file. The data doesn't have a fixed number in its rows. I give u the example :

2
9

1 4 3
7 4 5

3 5 4 1
4 4 3 2 0
2 3 1
4 8 4 2 1 Figure 1.0
6 8 7 5 3 1 0
4 7 6 4 0
2 7 5
4 8 6 5 4
3 7 4 3

As u can see above, the two earliest numbers (2 and 9) ->
type 1 : 2 => represents 1 4 3
7 4 5
Type 2 : 9 => represents from 3 5 4 1
4 4 3 2 0 etc..

From type 2 ,the leftmost value will determine how mane that the loop should read for the next number. For example 3 5 4 1
the leftmost -> 3 . And u can see this 3, is followed by another three numbers (5 4 1).

I have an idea to connect type 1 and type 2. My idea works like this:

from type 1 u can first row contain 1 4 3. In my program, what I want is, whenever my program reach to 1 for example, then it will return 4 3 2 0 (from type 2). And same goes to others.



My problems are :
1. how I can read this consecutives numbers
2. How I can connect the type 1 and type 2 data ?

I'm not expert in array but I prefer to use array. Below is my program code, but it doesn't works like what I want. i hope anyone can help me....

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
64
65
66
67
68
69
70
#include<iostream>
#include<fstream>
using namespace std;

int **m;

void readdata1();
void readdata2();

int noofvert,bil,*mh,*num;

void main()
{
	readdata1();
}

void readdata1()
{
	ifstream infile;
	infile.open("Sorted final.txt");

	if(infile.fail())
	{
		cout << " hahahha..Sorry u cannot open this file .." << endl;
	}

	infile >> bil;
	infile >> noofvert;

	m = new int*[bil];
	for(int i=0;i<bil;i++)
		m[i] = new int[3];

	mh = new int[noofvert];
	num = new int[noofvert];

	for(int i=0;i<bil;i++)
	{
		infile >> m[i][0] >> m[i][1] >> m[i][2];
	}

	for(int j=0;j<noofvert;j++)
	{
		infile >> mh[j];
		for(int k=0;k<mh[j];k++)
		{
			infile >> num[k];
		}
	}
readdata2();
}

void readdata2()
{
	string parse;
	for(int i=0;i<bil;i++)
	{		
			for(int k=0;k<noofvert;k++)
			{
				if(m[i][0] == k)
					cout << mh[k] << endl;
				
			}
		
	}
}

I hope anyone can help me to figure out, what is wrong in my code..thank you very very much for ur help...

thank you...



Last edited on
I'm sorry but you can't. If you can not describe what you are trying to do in a coherant manner in one language then how are you supposed to do it in another?

From what I understood of this post you would have to first read the entire file into memory I would do is in a way that each line of data is in its own array like so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <string.h>
//...Your code
int main(...)
{
int place;
string line[1000];
//.... Your code
while(infile)
{infile >> line[place];
  place++;
}
//...Your code
return 0;
}


Of course the integer "place" should be a pointer and instead of hardcoding an array of "1,000" you should probably use a dynamic array or list and there are a few other things. But I wrote it this way so that what we are doing can be easy to understand. From there you either need to hardcode the logic that determines that when the first integer read from the file is '2' a.) Jump to the fourth line, display it, new line, read and display the fifth line and a simular function for what ever other integers are expected in addition to a default exception for any values that are not predicted. Or b.) ??? You tell me, I understand that it is likley English is not your first language but I need you to try rephrasing this if I am to help.
i'm so sorry Computergeek01. i'll try to improve my english..i' really sorry..thanks for willing to help me, eventough it's hard for u to understand what i mean <- my english so poor...so sorry for that..

here what i mean is.

the first two numbers which are 2 and 9 are used to determine two different groups..
first group contain 2 data. there are :

1 4 3
7 4 5

second group contain 9 data. there are :
3 5 4 1
4 4 3 2 0
2 3 1
4 8 4 2 1
6 8 7 5 3 1 0
4 7 6 4 0
2 7 5
4 8 6 5 4
3 7 4 3

the value in the leftmost, is just to determine how many data in one row .
for instance, in first row, the leftmost value is 3, then u can see there are 3 values after that which are 5 4 1..
//----------------------------------------------------------//
what i want to do is i want to connect this two groups. my idea is when the program reach to first group for example 1, then, automatically, it will return value in second group which is
4 3 2 0.


i hope this explanation can help u... thank you very much computergeek01....thank you very much....

:)
string nameFich ="fich.dat";
string line;
ifstream fich;
fich.open(nameFich,ios::in);
getline(fich,line);
Topic archived. No new replies allowed.