find() doesn't find '|' for some reason


 
Thread Tools Search this Thread
Top Forums Programming find() doesn't find '|' for some reason
# 1  
Old 12-21-2010
Computer find() doesn't find '|' for some reason

Here's some sample output and my code follows it. Why can't find() find '|'. It just dosn't make a bit of sense at all.

Code:
AL01463|Pell City|Saint Clair|B|02115|AL|35125|630|Birmingham (Ann and  Tusc)|13890|40|Charter Communications|Fairfield|Charter Communications|2

-1
AL01463|Pinson|Jefferson|B|02073|AL|35126|630|Birm  ingham (Ann and  Tusc)|13890|40|Charter Communications|Fairfield|Charter Communications|2

-1
AL01463|Pleasant Grove|Jefferson|B|02073|AL|35127|630|Birmingham (Ann  and Tusc)|13890|40|Charter Communications|Fairfield|Charter  Communications|2

-1
AL01463|Pleasant Grv|Jefferson|B|02073|AL|35127|630|Birmingham (Ann and  Tusc)|13890|40|Charter Communications|Fairfield|Charter Communications|2

----------------------------------------------------------------------------------------------
I'm calling StringExplode. That's the couts I posted above.

Code:
#include </usr/include/mysql++/mysql++.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>

using namespace std;

void StringExplode(string str, string separator, vector<string>* results){
    std::cout << str << endl;
    int found;
    found = str.find(separator);
    while(found != string::npos){
        if(found > 0){
            results->push_back(str.substr(0,found));
        }
        str = str.substr(found+1);
        found = str.find_first_of(separator);
    }
    if(str.length() > 0){
        results->push_back(str);
    }
    std::cout << found << endl;
}


Last edited by pludi; 12-21-2010 at 07:23 AM.. Reason: code tags please
# 2  
Old 12-21-2010
It works absolutely fine for me:

Code:
...

int main(void)
{
        int n;
        vector<string> results;
        string s="a|b|c|d|e";

        StringExplode(s, "|", &results);

        printf("%d results:\n", (int)results.size());

        for(n=0; n<results.size(); n++)
        {
                printf("%d\t%s\n", n, results[n].c_str());
        }
        return(0);
}

Code:
$ g++ explode.cpp
$ ./a.out 
a|b|c|d|e
-1
5 results:
0	a
1	b
2	c
3	d
4	e
$


Last edited by Corona688; 12-21-2010 at 09:54 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Homework & Coursework Questions

Java doesnīt find the CLASSPATH (System variable)

1. The problem statement, all variables and given/known data: As a excercise I have to compile a program (Hello2.java) with a class file (HelloText.java) in another directory. (As you seen in the screenshot) Iīm setting a PATH and a CLASSPATH (system variables). Itīs working without a... (2 Replies)
Discussion started by: MenschTown
2 Replies

2. UNIX for Beginners Questions & Answers

Grep doesn't find what it should

Dear all, I wanted to use grep to search some files containing code - and the result was very disappointing: although I could see with my own eyes the expressions (written in the files), grep didn't give any output. Basically, I write this to the terminal grep "Find" *.txtor grep Find... (2 Replies)
Discussion started by: radudownload
2 Replies

3. HP-UX

Find command doesn't return in shell script.

Hi All, I am using below snippet to search for a string (read from a file 'searchstring.out') in all locations (/) and then iterate over the files found to write the locations and the respective owner to an output file. However, this doesn't work as I believe the find command doesn't exit's... (11 Replies)
Discussion started by: Vipin Batra
11 Replies

4. SuSE

Find command doesn't pipe the output as required.

Hi, I am using below code snippet to echo/display the files found (matching a pattern from searchstring.out file) and the corresponding owner. while read j do echo "Pattern to search is:- $j" find / -name "*$j*" |\ while read k do echo "File found is:- $k" owner=$(ls... (9 Replies)
Discussion started by: Vipin Batra
9 Replies

5. Shell Programming and Scripting

Why regex pattern doesn't work in find?

$ find /opt/data_* -maxdepth 3 -type d -name "main*" 2> /dev/null /opt/data_025/maindblogs /opt/data_026/maindblogs /opt/data_027/maindblogs /opt/data_028/maindblogs $ find /opt/data_* -maxdepth 3 -type d -name "rvlogs*" 2> /dev/null /opt/data_002/prod/rvlogs2_archive... (4 Replies)
Discussion started by: urello
4 Replies

6. OS X (Apple)

OS X 'find' nogroup/nouser doesn't traverse directories?

flamingo:~ joliver$ sudo find / -nogroup find: /dev/fd/4: No such file or directory find: /home: No such file or directory find: /Library: No such file or directory find: /net: No such file or directory find: /Network: No such file or directory find: /private: No such file or directory find:... (2 Replies)
Discussion started by: jnojr
2 Replies

7. UNIX for Dummies Questions & Answers

find command in shell script doesn't work

Hello all, Something strange going on with a shell script I'm writing. It's trying to write a list of files that it finds in a given directory to another file. But I also have a skip list so matching files that are in that skip list should be, well uhm, skipped :) Here's the code of my... (2 Replies)
Discussion started by: StijnV
2 Replies

8. UNIX for Dummies Questions & Answers

For some reason, my grep doesn't work as expected

I am trying to find only those entries where 7018 and another number appear in the end of the line. 7018 2828 1423 2351 7018 2828 14887 2828 7018 1222 123 7018 1487 I am looking for a way to generate only the last two lines. I was trying to do just "grep '7018{1,5}" but it does not... (5 Replies)
Discussion started by: Legend986
5 Replies

9. Shell Programming and Scripting

perl - how do i find out if a file doesn't contain a pattern?

how do i check a file for a pattern and perform an action if it doesn't exist? i know how to search a file for a pattern. you just place it in an array like so. #!/usr/bin/perl my $data_file = "file.txt"; open DATA, "$data_file"; my @array_of_data = <DATA>; if ($_ =~ m/pattern/i) {... (4 Replies)
Discussion started by: mjays
4 Replies

10. UNIX for Dummies Questions & Answers

How to find size of each subdirect? du -sk | ls doesn't work

Using solaris 2.5.1, and how can I get a summary of the size of each subdirectory, say for /export/home, all the users? usually I do a du -sk dirname but I have to manually type in each name, is there a better way? Thanks, (3 Replies)
Discussion started by: kymberm
3 Replies
Login or Register to Ask a Question