-
January 25th, 2007, 06:31 PM
#1
need help with search and replace function
I am trying to remove blank spaces and replace them to '_' . This program works when you have a defined string, but I am using a string the user propulates from edit txt box or console cin input. It will not display the whole filename. I suspect I am mixing C and C++.
Please help.
Here is the code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
//char out_file_name[19];
string out_file_name;
cout << " Enter output file name with no extention followed by a return \n ";
cout << '\n';
cout << " (For example training ";
cin >> out_file_name;
// string s = "All this will be replaced by this";
string s = out_file_name;
string toFind = " ";
string replaceWith = "_";
// before find and replace
cout << s << endl;
// find and replace operation
int pos;
while ((pos = s.find(toFind)) != string::npos)
s.replace(pos,toFind.size(),replaceWith);
// after find and replace
cout << s << endl;
return 0;
}
-
January 25th, 2007, 06:50 PM
#2
Re: need help with search and replace function
1) you are reading in the filename using operator >> , which stops
reading when it encounters a whitespace. I assume that you want
to read everything ... use getline
Code:
getline(cin,out_file_name);
Note: if you have to hit enter twice for getline to work, you are using
an old (bugged) version ... you can get the updated code at :
http://www.dinkumware.com/vc_fixes.html
see fix to <istream> and <string>
2) I doubt that you really want to replace space with '_' , but
if you do ... use std::replace ...
Code:
#include <algorithm>
replace(out_file_name.begin(),out_file_name.end(),' ','_');
-
January 25th, 2007, 07:46 PM
#3
Re: need help with search and replace function
I looked at some examples and it is still confusing.
-
January 25th, 2007, 07:57 PM
#4
Re: need help with search and replace function
?? Some examples of what ??
-
January 25th, 2007, 08:14 PM
#5
Re: need help with search and replace function
I want to use user input and It won't work. Need help.
The first examples will works:
/ replacing in a string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string s = "this is a test";
int x = s.find(" ");
while (x < string::npos)
{
s.replace(x,1,"_");
x = s.find(" ", x +1);
}
cout << s << endl;
return 0;
}
This second example does not work. It does not get the whole string. It only display to the white space.
/ replacing in a string
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char s1[255];
cout << " Enter output file name with no extention followed by a return \n ";
cout << '\n';
cout << " (For example training ";
cin >> s1;
string s = s1;
int x = s.find(" ");
while (x < string::npos)
{
s.replace(x,1,"_");
x = s.find(" ", x +1);
}
cout << s << endl;
return 0;
-
January 25th, 2007, 08:20 PM
#6
Re: need help with search and replace function
I know that strings are delimited by whitespace characters, which is causing my problem. does anybody know how I can prevent this.
-
January 25th, 2007, 08:39 PM
#7
Re: need help with search and replace function
see my very first post ... use getline() not operator >>
-
January 25th, 2007, 11:45 PM
#8
Re: need help with search and replace function
I got the prototype working using the getline, but I tried to intergrate to the logic below.
Now I get the following errors.
Can some help with these errors.
error C2872: 'ofstream' : ambiguous symbol
error C2872: 'cout' : ambiguous symbol
error C2872: 'cin' : ambiguous symbol
error C2664: 'strcat' : cannot convert parameter 1 from 'class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >' to 'char *'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
-----------------------------------------------------------------------------
#include <stdio.h>
#include <fstream.h>
#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>
#include <string>
#include <windows.h>
#include <time.h> //added for time and date
#include <iostream>
using namespace std;
char img_string[] = {'<','D','I','V',' ','a','l','i','g','n','=','"',
'l','e','f','t','"','>','<','I','M','G',' ','s','r','c',
'=','"','C','a','p','t','u','r','e','.','j','p','g',
'"','>','<','/','D','I','V','>','\0'};
static FILE *fp = NULL;
void main()
{
ofstream out_stream;
time_t rawtime; // added for time
time ( &rawtime ); //added for time
char * datetime = ctime (&rawtime);
//char out_file_name[256];
char filedir [200];
char s1[256];
cout << " Enter output file name with no extention followed by a return \n ";
cout << '\n';
cout << " (For example training ";
//cin >> out_file_name;
cin.getline (s1,256);
string out_file_name = s1;
int x = out_file_name.find(" ");
while (x < string::npos)
{
out_file_name.replace(x,1,"_");
x = out_file_name.find(" ", x +1);
}
strcat(out_file_name,".html");
strcpy (filedir, "C:\\training_output\\");
strcat (filedir, out_file_name);
out_stream.open (filedir);
if (out_stream.fail( ))
{
cout << "Output file opening failed.\n";
exit(1);
}
out_stream << "<HTML>" << '\n';
out_stream << "<TITLE>test" << '\n';
out_stream << "</TITLE>" << '\n';
out_stream << "<BODY>" << '\n';
-
January 26th, 2007, 07:16 AM
#9
Re: need help with search and replace function
YOu are including both <iostream> and <iostream.h> ...
you should not be using the .h version of the C++ headers. They
are non-standard and will not even compile using newer
compilers. Your headers should be :
Code:
#include <stdio.h> // ok but better would be <cstdio>
#include <fstream>
#include <iomanip>
#include <stdlib.h> // ok but better would be <cstdlib>
#include <string>
#include <windows.h>
#include <time.h> // ok but better would be <ctime>
#include <iostream>
#include <algorithm> // for std::replace
Also, you are mixing std::string and c-stycle char arrays .. which will
cause problems when using strcat() etc. I would recommend using
std::string only , unless it is absolutely necessary to use c-style
char arrays.
A simplified version of the relevant code:
Code:
string out_file_name;
getline (cin,out_file_name);
replace(out_file_name.begin(),out_file_name.end(),' ','_'); // why ???
out_file_name += ".html";
string filedir = "C:\\training_output\\" + out_file_name;
out_stream.open (filedir.c_str());
Last edited by Philip Nicoletti; January 26th, 2007 at 07:26 AM.
-
January 26th, 2007, 11:08 AM
#10
Re: need help with search and replace function
another other problem: I used to use the following commented code.
// char systemcall[ ] = "START C:/training_output/";
// char text[255];
// strcat(systemcall,out_file_name);
// strcpy(text,systemcall);
// system(text);
Now I am Trying to use:
string systemcall = "START" + filedir;
system(systemcall);
-
January 26th, 2007, 11:30 AM
#11
Re: need help with search and replace function
system() takes a const char * variable, not a std::string variable ...
you can use the c_str() member function to get a const char * ...
Code:
system(systemcall.c_str());
(and make sure you put a space after START)
-
January 26th, 2007, 12:23 PM
#12
Re: need help with search and replace function
hi,
can consider other way by "strtok" function.
regards
dhawan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|