Reading string

This is a discussion on Reading string within the C++ Programming forums, part of the General Programming Boards category; Hello, I am trying to read a line of characters separted by commas from a file. I need to be ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    9

    Exclamation Reading string

    Hello, I am trying to read a line of characters separted by commas from a file. I need to be able to use the string groups before each comma later in my program. I have been using the "find()" function and the
    "substr()" function, but I am having difficulting. Help in any form will be greatly appreciated. Thank you in advance.

  2. #2
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    read in a line using getline, then use find, im just goint to put each word into a vector:
    Code:
    vector<string> vec;
    string strLine;
    getline(inFile,strLine);
    int cpos = strLine.find(",");
    while(cpos)
    {    vec.push_back(strLine.substr(0,cpos);
          strLine = strLine.substr(cpos+1,strLine.length()-cpos);
          cpos = strLine.find(",");
    }
    that should work, havent tested it though.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  3. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    9

    Question Vector?

    I am pretty new to the programming game, so I am unfamiliar with vectors. Can you please give me an alternative? Once again, thank you in advance and your help is greatly adored.

  4. #4
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    just make an array of strings,
    Code:
    string myStrings[100];
    getline(inFile,strLine);
    int cpos = strLine.find(",");
    int index = 0;
    while(cpos && index < 100) // 100 b/c thats the size of myStrings
    {    myStrings[index] = strLine.substr(0,cpos);
          index++;
          strLine = strLine.substr(cpos+1,strLine.length()-cpos);
          cpos = strLine.find(",");
    }
    Last edited by xds4lx; 10-01-2002 at 09:39 PM.
    "only two things are infinite, the universe and human stupidity, and im not sure about the former." - albert einstein

  5. #5
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Just a note. Don't use that "while (cpos)" stuff because string::find() returns string::npos (which is -1) if the search data isn't located. you'll be stuck in an infinite loop. check if cpos == -1. it makes sense because if find() returned 0 when it didn't find a match, it would be telling you that it was located at the first position of the string.

  6. #6
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    like i said i thought of that right off the top of my head fast.

  7. #7
    Zwen
    Guest

    strtok

    You can also sequentially truncate string by delimiters - in your case the ",":

    http://www.cplusplus.com/ref/cstring/strtok.html

  8. #8
    Registered User
    Join Date
    Sep 2002
    Posts
    9

    Exclamation Array of Strings

    I have tried all of your suggestions and I understand how the code came about, but I am coming up with this one huge error. It prints out
    "no suitable conversion function 'std::basic_string<char, std::char_traits<char>, std::allocator<char>> ' to
    'char' exists myStrings[index] = strLine.substr( 0, cpos);"

    where int cpos = strLine.find(":");

    Can you tell me what that means?

    Thank you for all of your previous help !

  9. #9
    Registered User xds4lx's Avatar
    Join Date
    Nov 2001
    Posts
    630
    how did you declare your array? did you declare it as a char array?, if so it should be a string array, because that code compiles for me exactly how it is.

    Code:
    string myArray[100];

  10. #10
    Registered User
    Join Date
    Sep 2002
    Posts
    9

    Array of Strings

    Yes, I declared the array string, but i didn't not include the amount of elements in the array for it is unknown and subject to change.

  11. #11
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    string ** myArray = new string*;

    *myArray = new string[max_elements];

    Or if you want to have more control over constructors and size, just do:

    string ** myArray = new string*;

    while(!done) {

    *(myArray++) = new string("blah");

    }



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  3. IRC, reading the stream
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 08-03-2006, 05:34 PM
  4. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  5. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21