-
April 8th, 2008, 04:59 PM
#16
Re: Editing a single word in a file
I found the problem is indeed with the "(getline(infile, messageword, '\n'))". Is there any way to have more than one delimiter in this? My instruction say "Allow words to be separated by newlines, blanks, tabs, formfeeds, and punctuation symbols like commas, periods, single and double quotes, and parentheses." I was just trying to get the basics before worrying about all that. I tried putting two conditions in the line, something like
"(getline(infile, messageword, '\n' || ' '))", but that doesn't work.
-
April 8th, 2008, 05:34 PM
#17
Re: Editing a single word in a file
Unless you have some pressing need to get an entire line at once, you should normally stick to the >> operator. That automatically skips all whitespace.
-
April 8th, 2008, 05:36 PM
#18
Re: Editing a single word in a file
Originally Posted by veronicak5678
I found the problem is indeed with the "(getline(infile, messageword, '\n'))". Is there any way to have more than one delimiter in this? My instruction say "Allow words to be separated by newlines, blanks, tabs, formfeeds, and punctuation symbols like commas, periods, single and double quotes, and parentheses." I was just trying to get the basics before worrying about all that. I tried putting two conditions in the line, something like
"(getline(infile, messageword, '\n' || ' '))", but that doesn't work.
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string x;
ifstream inFile;
inFile.open("test.txt");
if (!inFile) {
cout << "Unable to open file";
exit(1); // terminate with error
}
while (inFile >> x) {
cout<<"x : "<<x<<endl;
}
inFile.close();
return 0;
}
Use something like this
-
April 8th, 2008, 05:47 PM
#19
Re: Editing a single word in a file
Then how do I write it back to the message file with all the whitespace and parentheses and everything else?
-
April 8th, 2008, 06:09 PM
#20
Re: Editing a single word in a file
And any idea how I can change all but the first letter? I could do this in a 2D array, but in a vector...?
-
April 8th, 2008, 06:09 PM
#21
Re: Editing a single word in a file
read up on how to read and write binary files reading seekp/seekg and tellp/tellg
-
April 8th, 2008, 06:23 PM
#22
Re: Editing a single word in a file
That's a good point.....you could certainly use tellp after each read to get the ending position in the file. You would store this along with the actual word in the vector----probably it would be a vector< pair<string,streampos> > then----and use this information to control your write-back.
The key observations would be that a word starts string::size() bytes prior to the tellp() position you got after reading it, and that you'd need to make sure you wrote exactly string::size() '*' characters so as not to mess up the rest of the file.
-
April 8th, 2008, 06:36 PM
#23
Re: Editing a single word in a file
Originally Posted by Lindley
That's a good point.....you could certainly use tellp after each read to get the ending position in the file. You would store this along with the actual word in the vector----probably it would be a vector< pair<string,streampos> > then----and use this information to control your write-back.
The key observations would be that a word starts string::size() bytes prior to the tellp() position you got after reading it, and that you'd need to make sure you wrote exactly string::size() '*' characters so as not to mess up the rest of the file.
You can do the same by the following
Code:
//GLOBAL DEFINITION
int ast_length=0;
char *ast;
//WITHIN THE LOOP
{
ast_length=inFile.tellp() - x.length()
ast = new char[ast_length];
for (int i=0;i<ast_length;i++;
ast[i]='*';
//MORE CODE TO SUBSTITUTE VECTOR DATA
delete [] ast;
}
-
April 8th, 2008, 06:42 PM
#24
Re: Editing a single word in a file
Sorry, but I've never even seen a pair vector before. Would I do two pushbacks in a row? And how do I use that to dictate my writing out?
-
April 8th, 2008, 07:23 PM
#25
Re: Editing a single word in a file
A std:air is constructed using make_pair(), and the members are accessed at .first and .second.
-
April 8th, 2008, 07:35 PM
#26
Re: Editing a single word in a file
You can actually create a Vector of a struct
Code:
struct words
{
string word;
int pos;
};
-
April 8th, 2008, 07:46 PM
#27
Re: Editing a single word in a file
Well, if you're going to modify words in-place anyway, there's no need to represent the file as a vector at all. You could just read a word, check if it's on the list, replace it if so, and move on.
-
April 8th, 2008, 07:56 PM
#28
Re: Editing a single word in a file
Originally Posted by Lindley
Well, if you're going to modify words in-place anyway, there's no need to represent the file as a vector at all. You could just read a word, check if it's on the list, replace it if so, and move on.
I agree with Lindley here. You can read the file with the censored words into an array or a Vector (safer option)
-
April 8th, 2008, 08:04 PM
#29
Re: Editing a single word in a file
Well, that's not quite what I was saying, but that's valid also. Instead of using a vector of strings, you could just use a single string for the entire file, using cin.get() to pull in characters individually and appending them with +=.
You could then do your replacement on this string using std::string::find(). Of course, your find routine would be slightly more complicated due to the need to check all the words in the censored list, and the fact that find() only returns the first instance after the specified index.
-
April 8th, 2008, 08:19 PM
#30
Re: Editing a single word in a file
Originally Posted by Lindley
Well, that's not quite what I was saying, but that's valid also. Instead of using a vector of strings, you could just use a single string for the entire file, using cin.get() to pull in characters individually and appending them with +=.
You could then do your replacement on this string using std::string::find(). Of course, your find routine would be slightly more complicated due to the need to check all the words in the censored list, and the fact that find() only returns the first instance after the specified index.
In short there is not optimal way of doing things here
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
|