streams: files -> char*

Discussion in 'C++' started by Aaron, Nov 16, 2005.

  1. Aaron

    Aaron Guest

    I have some existing code that accepts a filename, opens that file, and
    then processes it line-by-line. What I want to do is modify this
    routine so that instead I can simply pass a huge char* string and it
    will parse that just as it currently does the file contents. I am
    still wrapping my head around streams and was wondering if there was a
    simple way to modify the below code to accomodate this change. Your
    time, and any assistance you could provide, is greatly appreciated. I
    just don't know where to go to find this kind of information.

    Aaron.

    --BEGIN CODE--
    ifstream designFile(dF);
    if(designFile.fail() )
    {
    cout << "could not open " << dF << endl;
    return(false);
    }

    char c;
    do{
    designFile >> c;
    if( designFile.eof() )
    break;

    //do stuff
    }
    --END CODE--
     
    Aaron, Nov 16, 2005
    #1
    1. Advertisements

  2. Aaron

    Mike Wahler Guest

    void f(const char *s)
    {
    char c;
    std::istringstream iss(s);
    do{
    iss >> c;
    // etc
    } while(/* whatever */);
    }

    'istringstream' is declared by <sstream>

    -Mike
     
    Mike Wahler, Nov 16, 2005
    #2
    1. Advertisements

  3. Aaron

    Aaron Guest

    Thank you, Mike. That did the trick beautifully.

    Aaron
     
    Aaron, Nov 18, 2005
    #3
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.