String Manipulation

D

David Sharp

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
 
J

John Harrison

David Sharp said:
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
 
T

Thomas Matthews

David said:
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
 

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. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top