|
 |
|
 |
10-04-2005, 01:43 PM
|
#1 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 12
|
String disassembling
Right, i've got to designing a program which will read a text file, and then convert each word one by one, then print it to the screen and a file - but i've ran into a problem.
Code:
while(getline(fin, temp))
cout << temp << endl;
(fin being the filesteam input)
Now, while this will read every line easily from a text file and print it, i'm not sure what to do with that line in the string - ideally, I want to break it apart word by word, convert each one, and put it back into the string, which sounds like the best way of going about things, but i'm not entirely sure how I would: break the string up into it's seperate contained words - once i've done that I can load them into a string array, convert the words, and combine them back together - presto! it's just the matter of seperating words in a string that confuses me.
I'd be very grateful for any help on this! 
|
|
|
10-04-2005, 02:12 PM
|
#2 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
You might wanna look at get() for streams, especialy the stream& get (char* s, streamsize n, char delim ); where you can tell it to use <space> as teh delimiter.
Else you might wanna checkout strtok() where this thread might give you a hint.
|
|
|
10-05-2005, 06:05 AM
|
#3 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 12
|
Quote:
|
Originally Posted by redhead
|
You sir, are brilliant - works a charm!
Just one more problem....
Code:
/* strtok example */
#include <stdio.h>
#include <string.h>
#include <iostream>
using namespace std;
void Database();
string test[100];
int i;
int j;
int main ()
{
string input = "testing";
cout << "Please enter a sentence to be converted\n>>";
cin >> input;
char str[] ="This is a sample string,just testing.";
char * pch;
printf ("Splitting string \"%s\" in tokens:\n",str);
pch = strtok (str," ");
while (pch != NULL)
{
printf ("%s\n",pch);
test[i++] = ("%s\n",pch);
pch = strtok (NULL, " ,.");
}
system("pause");
for(j = 0; j < 7; j++)
{
Database();
cout << test[j] << " ";
}
cout << "\n";
system("pause");
return 0;
}
void Database()
{
if(test[j] == "sample")
{
test[j] = "Smaple";
}
}
I have no way it seems, of entering my own text to use - it won't accept strings, or chars with an allotted size, or an open char like str - i'm really stuck here.
|
|
|
10-05-2005, 06:47 AM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
What are you talking about ??
Code:
/* strtok example */
#include <string>
#include <iostream>
#include <vector>
int main ()
{
std::string input;
std::vector <std::string> input_words;
char* ptr;
std::cout << "Please enter a sentence to be converted" << std::endl;
std::cout << ">> ";
std::getline(std::cin, input);
std::cout << "Splitting string \"" << input
<< "\" in tokens:" << std::endl;
ptr = strtok ((char*) input.c_str()," ,.");
while (ptr != NULL)
{
input_words.push_back(ptr);
ptr = strtok (NULL, " ,.");
}
for(int i=0; i < input_words.size(); ++i)
std::cout << input_words[i] << std::endl;
return 0;
}
Completely C++ without any restrictions what so ever...
Last edited by redhead; 10-05-2005 at 07:13 AM.
|
|
|
10-05-2005, 07:14 AM
|
#5 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 12
|
Hm, I think I got a little confused somewhere
I was trying to say char str[] = string, or char, or whatever, and it was throwing a fit about it.
I'll try your method above now, cheers.
|
|
|
10-05-2005, 07:21 AM
|
#6 (permalink)
|
|
Registered User
Join Date: Sep 2005
Posts: 12
|
It works great - thankyou! I think I know where I went wrong (and I was going to add getline after, but thanks for pointing that out aswell)
I was expecting to be string instead, and your code here:
Code:
ptr = strtok ((char*) input.c_str()," ,.");
Looks a little different, so I assume that had something to do with it?
Sorry for being such a noob, this function is a little new to me
Anyway, I really appreciate your help, and I know, I shouldn't be using namespace 
|
|
|
10-05-2005, 08:09 AM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
|
Code:
ptr = strtok ((char*) input.c_str()," ,.");
means, convert the string input to a standard char* (in ANSI/ISO C reference) with the .c_str()
this how ever returns a const char* but strtok() expects a regular char* which it can dynamicaly alter, const aren't supposed to be altered.
So we tell it with the cast ie: (char*)
In this case strtok() will alter the first argument to hold the remainder of the orriginal string after encountering any chars from our delimiter string, since input.c_str() is assigned to a runtime temporary variable we couldn't care less what happens to it, thus we just cast it to char* and feed it to strtok(), which in return assigns first word to our ptr.
Uppon second call to strtok() we want to keep on cyckling through our string, which at this point is assigned to an internaly known variable, hence strtok() needs to be called with NULL.
Since our orriginal conversion of the input string was a const char, we're out of the woods from our (char*) cast at this point, since now the string is assigned to the internaly used variable which allows it to be altered on each call to strtok().
Quote:
|
Sorry for being such a noob, this function is a little new to me
|
Dont be sorry, your first shot at this lead me to the assumption, that you knew about the differences and conversions between std::string and char*, so I assumed you were able to implement it into the expected use of strtok()
Quote:
|
Anyway, I really appreciate your help, and I know, I shouldn't be using namespace
|
I'm only trying to make my code examples useable in any environment they might be introduced to. Specificaly telling what namespace every instance is fetched from, helps to ensure the expected results in any environment.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 05:08 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|