defining variables at real time

Hello. I am trying to define new variables at real time and i dont know how.

for(int i=0; i<imax; i++)
{
string istr=int2str(i);
string line="line";
line+=istr;
cout<<line.c_str()<<endl;
string ... line;
}
What is that supposed to mean? What are you really trying to do?

By the way, you can/should just write:
cout << line << endl;
I am trying to make...

string line0;
string line1;
string line2;
...............

then ....

line0="line0";
line1="line1";
line2="line2";

and then open with a prosedure the line0,line1,line2, etc.. files from disk.
At the beggining i give the number of files.


Not exactly. I have a string at real time. For example "a_string". Then i whant the name of strin to be a variable name.
string a_string;

How that?
See here, it's essentially the same problem:
http://www.cplusplus.com/forum/beginner/29998/
Suposse i have ...

map<string,string> concurrencyMap;
concurrencyMap["ccy1"]="EURUSD";
concurrencyMap["ccy2"]="USDJPY";

How i open EURUSD file on disk and the data holder is ccy1?

vector<string> ccy1;
dkaip wrote:
I am trying to make... [...]

Don't tell us the way you're trying to implement what you want to do :P Just tell us what you want to do.

You want to open some files and refer to them as "line0", "line1" etc... ? In that case, you may want to associate the file handlers (e.g. ifstream pointers) with strings (map<string,ifstream*>)

You want to open a file and store its lines in variables named "line0", "line1" etc... ?

Something else?

Try being more specific on describing the problem you have, and not your approach to solve it.
Thats correct. But you nust know that some people dont know english good. If the forum language is in ancient greek with pleasure i write my thouts with the right way.
enta3ei loipon, e3hghse auto p 8eleis na kaneis sta ellhnika ;) 'h steile m PM kalutera.

EDIT: (translation)

ok then, describe what you want to do in greek ;) or better, send me a PM.
Last edited on
Εντάξει λοιπόν και σε ευχαριστώ. Να λοιπόν τι θέλω να κάνω. Έχω τρία αρχεία που δημιουργούνται από ένα άλλο project, τα οποία μπορεί να είναι και περισσότερα. Δεν είναι γνωστός ο αριθμός τους.
Πρέπει όλα αυτά τα αρχεία, τα οποία είναι ascii να τα φορτώσω σε conatiners με τίλους ανάλογους.
Παράδειγμα.

vector<string>lines0;
vector<string>lines1;
vector<string>lines2;
vector<string>lines3;

Τα αρχεία έχουν τίτλους ...

f_m0
f_m1
f_m2
.........

Ψάχνω λοιπόν μια μέθοδο με την οποία θα δίνω τον αριθμό των αρχείων και το πρόθεμα, εδώ είναι το f.
Νομίζω ότι στη C μπορεί να γίνει η δημιουργία ονόματος μεταβλητής σε πραγματικό χρόνο, δηλαδή όταν τρέχει το πρόγραμμα. Με αυτό τον τρόπο ένα loop θα δημιουργούσε τις μεταβλητές, μια διαδικασία θα άνοιγε τα αρχεία και τέλος θα συνέχιζε η ροή του προγράμματος χωρίς να είμαστε αναγκασμένοι σε δημιουργία μεταβλητών από πριν καθορισμένων.
Στη lisp γίνεται αυτό εύκολα. Στη C κάποτε παλαιότερα το είχα συναντήσει.
Ευχαριστώ και καλό βράδυ, αν είστε εδώ Ελλάδα.


Ok, I think the solution to your problem is simple. Try this -> vector < vector <string> > files; This is a vector of vectors of strings. Since each inner vector represents one file, the outer vector is actually a container of files. And since it's a vector, it can grow at will during run-time. You can then do something like this:

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
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <sstream>
using namespace std;

//...

    vector<vector<string> > files;

    int file_id=0;

    while (true)
    {
        stringstream buffer;
        buffer << "f_m";
        buffer << file_id;

        string cur_file_name=buffer.str();

        ifstream fin(cur_file_name.c_str());
        if (!fin.is_open()) break;
        // if file doesn't exist stop

        vector<string> cur_file;

        while (true)
        {
            string cur_line;

            //get cur_line and
            //push it back to cur_file

            //when you're done
            //break;
        }

        files.push_back(cur_file);
        file_id++;
    }

//... 

If your files are big in size, you may want to use a vector < vector <string>*> instead,
because files.push_back(cur_file); can be slow.
Last edited on
I just open. I will try it soon as posible.
Thank's very mutch for great help and good day.
Jim
Last edited on
Topic archived. No new replies allowed.