C++ counterpart to keyword DATA

Jul 17, 2011 at 5:35pm
Way back in the day, I utilized the DATA keyword in BASIC environments to perform specific tasks. I have a book that is not currently at this location that shows C++ having a similar functionality. Wondering if anyone could point me in the direction of what that was.

The DATA statement in BASIC and its variants was used like this:
DATA 1,1,0,1,0,1,1
DATA 1,1,0,1,0,1,1
DATA 1,1,1,1,1,1,1
DATA 1,0,1,1,1,0,1
DATA 1,1,0,0,0,1,1

Along with a subsequent READ statement, I could extract the individual values into variables. A loop READing and assigning it to variables a, b, and c would give the following values:
a = 1
b = 1
c = 0

I like this ability for certain situations, but an alternative might exist. The only thing that comes close is text file I/O operations.

Thanks!
Jul 17, 2011 at 7:00pm
Don't know what you mean, but it looks like std::bitset is c++ equivalent to this.
Jul 17, 2011 at 7:27pm
The above DATA usage is actual code. I chose 1's and 0's for example, but the statements could include all sorts of data types including strings, leaving the READ statement to place it into the appropriate type.
Jul 17, 2011 at 7:34pm
not sure what you mean. maybe a structure?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
struct test
{
    string name;
    int      age;
    bool   alive;
 };

// usage 1
test ts;
ts.name = "Mike";
ts.age = 22;
ts.alive = true;

// usage 2

test mytest = { "Dave" , 62 , false };
Last edited on Jul 17, 2011 at 7:42pm
Jul 18, 2011 at 2:32am
I think what the OP meant is in Basic programming language, you can put your input data as in above format in the same file as your source code. And then along the way when your program want to us them, you can reference them directly. You don't need any file input IO to read any input data in. This is pretty useful feature for "small data for testing". I think C, C++ do not offer this feature except like what acorn provided in above post.

In Perl it does offer this feature also Key in Perl __DATA__ and search. There must be a "name" for such feature isn't it ? A "virtual input file" ?



Jul 20, 2011 at 4:28pm
Just a follow up.

Thanks to all of those who tackled this. It was mainly a convenience factor for a near seamless transition from one language to another (methodology wise) as the data doesn't exist in any other form. Amongst the great power and control that comes with C/C++, it's worth noting the usefulness of this feature as sohguanh has pointed out.

It can be done, just with unneeded overhead of preparing an array for visual purposes in code just to put it into another array for program use (pseudo code example):

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
int array[] = {
		1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
		2,2,2,2,3,3,3,3,1,1,1,1,2,2,2,2,3,3,3,3,
		2,2,2,2,3,3,3,3,1,1,1,1,2,2,2,2,3,3,3,3,
		2,2,2,2,3,3,3,3,1,1,1,1,2,2,2,2,3,3,3,3,
		2,2,2,2,3,3,3,3,1,1,1,1,2,2,2,2,3,3,3,3,
		2,2,2,2,3,3,3,3,1,1,1,1,2,2,2,2,3,3,3,3,
		2,2,2,2,3,3,3,3,1,1,1,1,2,2,2,2,3,3,3,3,
		2,2,2,2,3,3,3,3,1,1,1,1,2,2,2,2,3,3,3,3,
		2,2,2,2,3,3,3,3,1,1,1,1,2,2,2,2,3,3,3,3,
		2,2,2,2,3,3,3,3,1,1,1,1,2,2,2,2,3,3,3,3,
		2,2,2,2,3,3,3,3,1,1,1,1,2,2,2,2,3,3,3,3,
		1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
	};

int i = 0;
	for(int y = 0; y < 12; y++) {
		for(int x = 0; x < 20; x++) {
			
			functionPlotPixel(x , y, array[i]);

			i += 1;
		}
	}

functionGetImage(functionArray[]);


In the above, it would be great to organize that data without the use for an array. I could have sworn I had seen this done, but it must have been another language. I've worked around this with minimal use of a library instead. Btw, is it safe to call what I was asking for a 'data matrix'?
Last edited on Jul 20, 2011 at 4:32pm
Jul 20, 2011 at 5:47pm
What you are referring to reminds me of x86 assembly language. There is a "db" operation that defines bytes directly (in the code segment or was it the data segment?). I'm not sure how supported this type of thing is today. There are still ways of using inline assembly in C++ but I'm not sure if you can access it outside of the block or not (I would assume so).

In C++, we create arrays on the stack for this. Or, if they are changed at runtime you can use dynamic containers such as a vectors.

I'm also not sure that BASIC was doing the same thing or just defining arrays in a less verbose syntax...
Last edited on Jul 20, 2011 at 5:49pm
Jul 20, 2011 at 6:38pm
What you want is tr1::tuple. Research on them. I think they pretty much are exactly what you want.
Jul 20, 2011 at 7:14pm
I did this DATA/READ simulation;

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
#include<iostream>
using namespace std;


// struct to embed your data
// you can add more methods 
struct myData {

	static char * Data;
	short int Read(){
		if(*Data) {
			return *(Data++)- 48;
		}	
	}

} eData;
char *myData::Data = 
	"1234567890"
	"2234567890"
	"3234567890"
	"4234567890"
	"5234567890"
	"6234567890"
	"7234567890"
	"8234567890"
	"9234567890"
	"0234567890";


int main(){

	for(int i=0; i<100; i++)
		cout << eData.Read();

	cout << endl;


return 0;
}

/*
output:
1234567890223456789032345678904234567890523456789062345678907234567890823456789092345678900234567890

*/
Last edited on Jul 20, 2011 at 7:25pm
Jul 21, 2011 at 3:38pm
Acorn started the suggestion of the struct, and muratagenc nailed it with that sim. I might actually go this route after-all. Make it a const char in the struct definition and throwing in a return as an alternative for that 'if' in the struct and this thing is good to go.

Also, I am curious on the mentioned tuple method. I admit I'm not keen on the syntax, but know the relationship with struct. Maybe it can get better with it.

Thanks again all!
Topic archived. No new replies allowed.