trouble with delimiter


 
Thread Tools Search this Thread
Top Forums Programming trouble with delimiter
# 1  
Old 11-20-2011
trouble with delimiter

Hi all,

I have a flat file storing data like this,
Code:
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);

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!

---------- Post updated at 08:39 PM ---------- Previous update was at 01:47 AM ----------

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.

Code:
else if (file.good())
                {
                    int record = 0;
                    string line;
                    string line2;
                    set<string> setTesting;
                    set<string>::iterator test = setTesting.begin();

                    while (getline(file, line)) 
                    {
                        
                        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-
Code:
test, [1, 2]
test, [1, 2]
test2, [1,2,3]
test3, [1,2,3]

Can you advise me where went wrong?
Thanks for your help!
# 2  
Old 11-20-2011
I don't see you inserting anything to setTest, or is that a typo?...

--ahamed
# 3  
Old 11-20-2011
I thought I insert it in here,
Code:
while(getline(ss,line2,','))
                            {
                                setPoint2D.insert(p2d,line2);
                            }

---------- Post updated at 10:08 AM ---------- Previous update was at 09:36 AM ----------

I found out when I convert my string value into a stringstream. The value turns weird.

Code:
string result = line.substr(start + 1, end - start - 1);

"result" returns value of "3, 2"

Code:
stringstream ss(result);

However when I convert it to stringstream, it returns "0xbfa7c4bc".

I'm wondering which part went wrong.
# 4  
Old 11-21-2011
And why are you trying to do this in C++ when it can be easily handled by awk or perl...
# 5  
Old 11-21-2011
I am no c++ programmer, but why don't you use strtok?
# 6  
Old 11-21-2011
Thanks for your response!

Reason why I don't use awk is because currently I'm learning c++.

anyway I've managed to solve my issues.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl Code to change file delimiter (passed as argument) to bar delimiter

Hi, Extremely new to Perl scripting, but need a quick fix without using TEXT::CSV I need to read in a file, pass any delimiter as an argument, and convert it to bar delimited on the output. In addition, enclose fields within double quotes in case of any embedded delimiters. Any help would... (2 Replies)
Discussion started by: JPB1977
2 Replies

2. Shell Programming and Scripting

Shell script to put delimiter for a no delimiter variable length text file

Hi, I have a No Delimiter variable length text file with following schema - Column Name Data length Firstname 5 Lastname 5 age 3 phoneno1 10 phoneno2 10 phoneno3 10 sample data - ... (16 Replies)
Discussion started by: Gaurav Martha
16 Replies

3. Shell Programming and Scripting

How to cut by delimiter, and delimiter can be anything except numbers?

Hi all, I have a number of strings like below: //mnt/autocor/43°13'(33")W/ and i'm trying to get the numbers in this string, for example 431333 please help thanks ahead (14 Replies)
Discussion started by: sunnydanniel
14 Replies

4. Programming

Trouble with C

Hey, i am having a problem First, i know java well and i have used C++ on occasion so i thought i would be able to deal with a class where they program in C. unfortunately i have hit some speed bumps that i am having problems. Here is my problem: I have a structure cache_t in the sample... (0 Replies)
Discussion started by: zephoid
0 Replies

5. Shell Programming and Scripting

Substring based on delimiter, finding last delimiter

Hi, I have a string like ABC.123.XYZ-A1-B2-P1-C4. I want to delimit the string based on "-" and then get result as only two strings. One with string till last hyphen and other with value after last hyphen... For this case, it would be something like first string as "ABC.123.XYZ-A1-B2-P1" and... (6 Replies)
Discussion started by: gupt_ash
6 Replies

6. UNIX for Dummies Questions & Answers

X trouble

Hi there, I'm new to unix-environments. I'm richard, and i'm mostly a web-developer, under php. I've done work in unix env before, but never had my own. Today, I've got debian 3.1 r4 from the official site, and i've attempted to install it twice. I installed it initially as "Desktop... (0 Replies)
Discussion started by: izua
0 Replies

7. Solaris

Trouble with tr

I'm not sure where to post this but it's happening on a SunOS 5.8 server so I'll try here. I've discovered some unexpected behavior when using tr. For example: echo a | tr Z echo b | tr a echo a | tr B echo a | tr B echo a | tr A (8 Replies)
Discussion started by: Mike@Work
8 Replies

8. UNIX for Dummies Questions & Answers

The trouble about SU ...

Hi all, having read lots of posts about SU I don't quiet understand this : I'm doing regular backups of my database (u betta do) and therefore use su - username -c "sqlscript special data_base" in a unixscript which is even using cron. (yep!) Now I need some other script, still with this... (4 Replies)
Discussion started by: nulnul7
4 Replies

9. What is on Your Mind?

The trouble with...

Welcome to "The trouble with...." with your host, ZazzyBob. Todays offering - "The trouble with letting other people host your website" I use a certain web hosting service, who shall of course remain nameless here. They are running PHP 4.3.10 I decide to write a script to test their PHP... (6 Replies)
Discussion started by: zazzybob
6 Replies

10. UNIX Desktop Questions & Answers

trouble

Hello, The system reboots in single user mode what command to use to bring the system up regularly?? I get INIT: cannot create /var/adm/utmpx Type control -d to proceede with a normal startup or give root passwd for system maintaince. either way i don't have a change and ... (1 Reply)
Discussion started by: awk
1 Replies
Login or Register to Ask a Question