String Manipulation

Discussion in 'C++' started by David Sharp, Sep 1, 2003.

  1. David Sharp

    David Sharp Guest

    Hi,

    I am trying to write a routine which opens a file, reads a line and
    splits the line in two, each of the two words per line going into
    different variables. For example:

    The line in the file reads :

    MOVB,12

    MOVB is placed in the variable 'char *operand'.
    12 is placed in the variable 'char variable'

    I have tried many things but keep messing up!

    Any ideas?

    Thanks

    David Sharp
     
    David Sharp, Sep 1, 2003
    #1
    1. Advertising

  2. David Sharp

    Jon Bell Guest

    In article <>,
    David Sharp <> wrote:
    >
    >I have tried many things but keep messing up!


    Show us one of your attempts and someone will probably be happy to comment
    on it.

    --
    Jon Bell <> Presbyterian College
    Dept. of Physics and Computer Science Clinton, South Carolina USA
     
    Jon Bell, Sep 2, 2003
    #2
    1. Advertising

  3. "David Sharp" <> wrote in message
    news:...
    > Hi,
    >
    > I am trying to write a routine which opens a file, reads a line and
    > splits the line in two, each of the two words per line going into
    > different variables. For example:
    >
    > The line in the file reads :
    >
    > MOVB,12
    >
    > MOVB is placed in the variable 'char *operand'.
    > 12 is placed in the variable 'char variable'
    >
    > I have tried many things but keep messing up!
    >
    > Any ideas?
    >
    > Thanks
    >
    > David Sharp


    Without seeing any code, I only have one idea.

    Instead of using char* for your strings (which as you've found is very
    tricky), use the C++ string class 'std::string' instead which is much
    easier.

    Using std::string instead of char* will mean you have to unlearn some of
    what you've already learned but it will be worth it on the long run.

    john
     
    John Harrison, Sep 2, 2003
    #3
  4. David Sharp wrote:

    > Hi,
    >
    > I am trying to write a routine which opens a file, reads a line and
    > splits the line in two, each of the two words per line going into
    > different variables. For example:
    >
    > The line in the file reads :
    >
    > MOVB,12
    >
    > MOVB is placed in the variable 'char *operand'.
    > 12 is placed in the variable 'char variable'
    >
    > I have tried many things but keep messing up!
    >
    > Any ideas?
    >
    > Thanks
    >
    > David Sharp


    #include <iostream>
    #include <fstream>
    #include <string>
    #include <cstdlib>
    using std::ifstream;
    using std::string;

    int main(void)
    {
    ifstream my_file("my_data.txt");
    if (!my_file)
    return EXIT_FAILURE;

    string instruction;
    unsigned int value;
    //...
    getline(my_file, instruction, ',');
    my_file >> value;
    // ...
    cout << "instruction: " << instruction << '\n';
    cout << "value: " << value << '\n';

    return EXIT_SUCCESS;
    }


    --
    Thomas Matthews

    C++ newsgroup welcome message:
    http://www.slack.net/~shiva/welcome.txt
    C++ Faq: http://www.parashift.com/c -faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.learn.c-c++ faq:
    http://www.raos.demon.uk/acllc-c /faq.html
    Other sites:
    http://www.josuttis.com -- C++ STL Library book
     
    Thomas Matthews, Sep 2, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jared in ecs

    Perl string manipulation

    jared in ecs, Oct 22, 2003, in forum: Perl
    Replies:
    2
    Views:
    824
    Roy Johnson
    Oct 22, 2003
  2. Garfield

    String Array Manipulation Problem

    Garfield, Aug 27, 2003, in forum: ASP .Net
    Replies:
    5
    Views:
    2,840
    Michael Lang
    Aug 27, 2003
  3. Aaron

    String Manipulation

    Aaron, Dec 31, 2003, in forum: ASP .Net
    Replies:
    3
    Views:
    4,667
    Dave Davidson
    Dec 31, 2003
  4. Aaron

    Advanced String Manipulation (C#)

    Aaron, Jan 2, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    863
  5. Chris

    String manipulation

    Chris, May 24, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    468
    Pete Wright
    May 24, 2004
Loading...

Share This Page