trouble with delimiters

Hi all,

I have a flat file storing data like this,

Testing,[1,2,3]


What I'm trying to do is to retrieve the data "1", "2" and "3".
So far I manage retrieve the data by using the code below,

Code:

while (getline(file, line))
{

if (line.find("Testing")!=string::npos)
{
start = line.find("[");
end = line.find("]", start+1);
string result = line.substr(start + 1, end - start - 1);

End of Code:

With the code above, I only manage to retrieve "1,2,3".
However, instead of a string "1,2,3", I want to retrieve it as individual int so as to store the individual value into an array (set).
E.g. "1","2" and "3" as int value.

Can anyone kindly provide some suggestions?
Thanks!
If you want to delimit the comma, you can overload the getline function to accept this (assuming line is a string):


1
2
3
4
5
6
7
ifstream file("myFile.txt");
string line;

while (getline(file,line,','))
{
    cout << "Line : " << line << endl;
}

This would be your output:

Line : Testing
Line : [1
Line : 2
Line : 3]


Another thing you could try is recognizing the individual characters which I'll write below:
1
2
3
4
5
6
7
8
9
10
11
12
ifstream file("myFile.txt");
string line;
int digit[100] = {0}; //Limited to 100 numbers in a file. Modify as req'd
int j = 0;

while (getline(file,line))
    for (int i=0; i<line.size();i++) //Check each character in the string
        if (line[i]>=0x30 && line[i]<= 0x39) //Filter ascii code for characters 0~9
            digit[j++] = (int)(line[i]-0x30); //Translate the numeric character to integer

for (int i=0; i<j; i++)
    cout << digit[i];


This method will evaluate each character of the input line and then store it as an integer. The only thing is that the stored integers will be single digits only.
Last edited on
I just had a thought! The following will allow you to get multi-digit numbers as well:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ifstream file("myFile.txt");
string line;
int digit[100] = {0}; //Limited to 100 numbers in a file. Modify as req'd
int j = 0;

while (getline(file,line))
    for (int i=0; i<line.size();i++) //Check each character in the string
        if (line[i]>=0x30 && line[i]<= 0x39) //Filter ascii code for characters 0~9
            if (line[i-1]>=0x30 && line[i-1]<= 0x39) //Check if the previous character is a digit
                digit[j-1] *=10, digit[j-1]+=(int)(line[i]-0x30); //Multiply the previous integer by 10 and add the next digit.
            else
                digit[j++] = (int)(line[i]-0x30); //Translate the numeric character to integer

for (int i=0; i<j; i++)    //digit[] contains your array of integers with j elements.
    cout << digit[i];


Sorry about omitting the 'Martha-Stewart' brackets. It just seemed so much shorter without them. It will still work. Hopefully you can still read it.
Last edited on
Thanks for your response!

I need to store those individual int into a STL container "set" as I need to retrieve respective int value for calculation afterwards.

Based on my research, most of them actually suggested to convert it to "stringstream" and make use of getline("stringstream", "string", "delimiter").

This is what I've done so far.

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
else if (file.good())
				{
					int record = 0;
					string line;
					string line2;
					set<string> setTesting;
					set<string>::iterator test = setTesting.begin();

					while (getline(file, line)) 
					{
						record++;
						
						if (line.find("test")!=string::npos)
						{
							start = line.find("[");
							end = line.find("]", start+1);
							string result = line.substr(start + 1, end - start - 1);
							stringstream ss(result);
							while(getline(ss,line2,','))
							{
								setPoint2D.insert(p2d,line2);
							}
								while(test!=setTest.end())
								{
									cout << *test << endl;
									test++;
								}

						}


I've created my SET Container with ADT <Point2D>; Point2D is a class with 2 variables - int x and int y. My ultimate goal is to store the individual int into my <Point2D> SET Container.

The code above is able to compile, however when I execute it, there isn't any display of codes.

My flat file contains data in this format-
1
2
test, [1, 2]
test, [1, 2]


Can you advise me where went wrong?
Thanks for your help!
Argh, my conscious is eating at me. Here is it with the 'Martha Stewart' brackets:
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
ifstream file("myFile.txt");
string line;
int digit[100] = {0}; //Limited to 100 numbers in a file. Modify as req'd
int j = 0;

while (getline(file,line)) 
{
    for (int i=0; i<line.size();i++) //Check each character in the string
    { 
        if (line[i]>=0x30 && line[i]<= 0x39)  //Filter ascii code for characters 0~9
        {
            if (line[i-1]>=0x30 && line[i-1]<= 0x39)  //Check if the previous character is a digit
            {
                digit[j-1] *= 10; // Multiply the previous integer by 10
                digit[j-1] += (int)(line[i]-0x30); // add the next digit.
            }
            else 
            {
                digit[j++] = (int)(line[i]-0x30); //Translate the numeric character to integer
            }
        }
    }
}

for (int i=0; i<j; i++)    //digit[] contains your array of integers with j elements.
{
    cout << digit[i];
}
Last edited on
Thanks stewbond, however in my flat file, it contains several data.

1
2
3
4
test, [1, 2]
test, [1, 2]
test2, [1,2,3]
test3, [1,2,3]


I need to retrieve all data into STL Containers based on their category. E.g. "test", "test2", "test3"
In this case, I will create a total of 3 Containers to store the respective values.

Currently I'm facing some problems on my codes which I posted earlier on.

Is there anything wrong with my codes?
Topic archived. No new replies allowed.