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0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 1 1 1 1 1 1 1 1 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]];


I would like to read this file into separate arrays, like the ones in the file.

Any help would be much appreciated.
well, this looks like a homework for me....
anyway, i will give you a function to read an array into its argument.
the rest is up to you.
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
#define X 5     //these are the dimensions of the array that will
#define Y 3     //hold the values.
#define Z 20
void ExtractArray( int a[X][Y][Z], ifstream& in);

//your code here


/*	pre-condition:	(a) is pointing to an enough allocated memory.
			(in) is connected to the input file.
 	post-condition: (a) contains the extracted array.   */
void ExtractArray( int a[X][Y][Z], ifstream& in)
{

	for(int x = 0 ; x < X ; x++)
	{
		for(int y = 0 ; y < Y ; y++)
		{
			for(int z = 0 ; z < Z ; z++)
			{
				while(in.peek() < '0' || in.peek() > '9')
				{
					in.ignore();	//ignore everything other than a digit.
				}

				a[x][y][z] = in.get() - '0';	//extract the digit, and transform
								//it from ascii to value.
			}
		}
	}
}


i think this should be enough for you to complete this journey.
Last edited on
Thank you very much, this was very helpful!

(This is not a homework question.)

I have one more question. Those lines with words contain performance measures (like solve time, error bound), so they also contain numbers. So to set to pointer to where I want I'm doing this, because I know there are 14 lines before "x=":

1
2
3
4
char semmi[256];
	for(int i=0; i<14;i++){
		file.getline (semmi,256);
        }


I was wondering if there was a better way to do it. To actually find the position of "x=", in case the number of lines change.

Thank you!
i would try to ignore those lines instead of extracting them.

1
2
3
4
5
6
7
8
9
10
unsigned int nLine = 0;   //hold the number of lines passed.
do
{
    if(file.peek() == '\n')   //if reach a newline, increase nLine by 1.
    {
        nLine ++ ;
    }
    file.ignore();
}
while( (nLine < 14) && ( !file.eof() ) );  //keep iterating until passed 14 lines or reach eof. 
Thank you very much for your help!
I have one more question. Now I am trying to read in the data file (the previous file was the output). The data file looks like this:

T=20;
R=3;
B=3;
P=5;
K=3;
S=[15,15,15];
cf=20;
cv=2;
n=2;
d=[5,10,8,7,8];
...


My question is, how can I read in the double digit numbers.

Thank you!
closed account (Dy7SLyTq)
are you asking how to read in just the doubles? write a mini parser type thing
Yes. For the singe digits, I'm using the following modification of the code above:

1
2
3
4
5
6
7
8
9
10
11
12
int ExtractArraySmall2( int room, ifstream& in)
{
	   while(in.peek() < '0' || in.peek() > '9')
				{
					in.ignore();	//ignore everything other than a digit.
				}

				room = in.get() - '0';	//extract the digit, and transform
								//it from ascii to value.
				
				return room;
}


This takes one digit at a time, so I don't know how to read the double digit numbers in.

While I was working on this, I came across another problem. The X, Y, Z numbers that are defined at the beginning are things that I would like to read in from the data file (X=P, Y=R, Z=T), but when I do that this error comes up: expression must have a constant value; because these are the sizes of the arrays. Is there any way to go around that?

Thank you!
closed account (Dy7SLyTq)
vectors
I'm sorry, could you be more specific? As I said in the beginning of my first post, I am new to C++.

And what about reading the double digits?

Thank you!
i would try implement this algorithm:

1
2
3
4
5
6
7
8
9
10
int number = 0;
while(in.peek()>='0' && in.peek() <= '9')
{
    char temp = in.get(); // extract one byte of the number.
    temp -= '0';  // convert it from ASCII value to binary value.
    number |= temp;  // paste the extracted byte on the low order byte of number.
    number <<= temp;  //shift number 8 bits left to clear space for the next byte.
}
number >>= 8;  //  after the loop breaks, number is shifted 8bits
//left more than needed, so correct that.  


after the algorithm finishes, number will contain one number of the source stream.

EDIT - PS:
this algorithm shall be changed to be used with different encodings, like little indian.
also, if the file already contains the number as a binary value, you would delete line 5.
Last edited on
Thank you very much, but it did not give the right number (which is 20). Using the following checkpoints it gave me this

1
2
3
4
5
6
7
8
9
10
11
12
13
while(in.peek()>='0' && in.peek() <= '9')
	{
	    char temp = in.get(); // extract one byte of the number.
	    temp -= '0';  // convert it from ASCII value to binary value.
	    number |= temp;  // paste the extracted byte on the low order byte of number.
		cout<<number<<" one\n";
	    number <<= temp;  //shift number 8 bits left to clear space for the next byte.
		cout<<number<<" two\n";
	}
	cout<<number<<" three\n";
	number >>= 8;   //  after the loop breaks, number is shifted 8bits
					//left more than needed, so correct that.  
	cout<<number<<" four\n";


Output:
2 one
8 two
8 one
8 two
8 three
0 four


The file is ANSI encoded.
Topic archived. No new replies allowed.