Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > String Manipulation

Reply
Thread Tools

String Manipulation

 
 
David Sharp
Guest
Posts: n/a
 
      09-01-2003
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
 
Reply With Quote
 
 
 
 
Jon Bell
Guest
Posts: n/a
 
      09-02-2003
In article <(E-Mail Removed) >,
David Sharp <(E-Mail Removed)> 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 <(E-Mail Removed)> Presbyterian College
Dept. of Physics and Computer Science Clinton, South Carolina USA
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      09-02-2003

"David Sharp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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


 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      09-02-2003
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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
String manipulation Chris ASP .Net 1 05-24-2004 02:19 PM
Advanced String Manipulation (C#) Aaron ASP .Net 2 01-02-2004 06:50 PM
String Manipulation Aaron ASP .Net 3 12-31-2003 03:39 PM
Perl string manipulation jared in ecs Perl 2 10-22-2003 05:36 PM
String Array Manipulation Problem Garfield ASP .Net 5 08-27-2003 10:07 PM



Advertisments