CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Threaded View

  1. #1
    Join Date
    May 2009
    Location
    Boston
    Posts
    364

    parsing of char array won't finish

    I have some cpp code that is looping through an array of char looking for a delimiter. The code saves the chars in a string until it finds the delimiter, then it adds the string to a vector of string and continues processing.

    The input char array is,
    Cl.N1C=CC=N1.HCl
    the delimiter is '.', so the parsed strings should be,
    Cl
    N1C=CC=N1
    HCl

    The code seems to work find for the first two strings, but then it seems to stop and not find the third string.

    This is the code,
    Code:
    #include <iostream>
    #include <vector>
    #include <string>
    
    extern "C" { int parse_string_test_( char *INP_PASS, int *LENGTH); }
    
    int parse_string_test_( char *INP_PASS, int *LENGTH ) {
    
       vector<string> found_fragments;
       string temp_read;
       int i, passed_length;
    
       // number of occupied positions in passed char array
       passed_length = *LENGTH;
    
       // print initial values
       cout << "char array" << endl;
       cout << "LENGTH " << passed_length << endl;
       for(i=0; i<passed_length; i++) { cout << INP_PASS[i]; }
       cout << endl;
       cout << endl;
    
       // parse char array into strings using . as delimiter
       for(i=0; i<passed_length; i++) {
    
          // add characters to temp_read until delimiter is found
          if(INP_PASS[i] != '.') {
             temp_read.push_back(INP_PASS[i]);
          }
    
          // when delimiter is found
          else if(INP_PASS[i] == '.') {
    
             // add temp read string to vector of string
             found_fragments.push_back(temp_read);
    
             cout << "temp_read " << found_fragments.size() << endl;
             cout << temp_read << endl;
    
             // clear temp read structure to continue looking for next string
             temp_read.clear();
          }
       }
    
    return 0;
    }
    The output from he above is,
    char array
    LENGTH 16
    Cl.N1C=CC=N1.HCl

    temp_read 1
    Cl
    temp_read 2
    N1C=CC=N1

    ...then nothing. The function returns to the calling code, but may not have returned normally, it is hard to tell.

    This appears to work up to a point, but does not find the last string HCl. The size of the char array that is passed to the functions prints as correct (16), so the for loop should process all 16 characters in the array. I don't see here why it stops.

    This was compiled using g++-3. Some of the unusual syntax results from this being a cpp function that is called from fortran. The char array that is passed to the cpp prints as correct and has the correct size, so I don't think that is part of the problem.

    Any suggestions,

    Thanks,

    LMHmedchem
    Last edited by LMHmedchem; May 6th, 2013 at 12:58 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured