OK. First thing first. You're using C++ so you should think about using std::string rather than statically sized character arrays. Benefits - dynamically resized, memory automatically managed, public methods, easy to use operators (like equality). Downsides - you're going to have to learn them.
Especially since you "#include <string>".
Also if you're going by words you might as well do something like:
// Prep work like setting up input stream, testing it etc.
std::string str;
while( in >> str ) {
std::cout<< "This is a word: " << str << "\n";
// etc...
}
Note or warning - that stops at whitespace. Not at punctuation so you're going to have to think about that.
I don't think you're going to be able to open a stream to both write and read a file at the same time. Open a stream. Read the contents. Close the stream. Open another one. Output the contents. Close that stream then.
Something else to consider is using a vector to store all the stuff you read rather than a 2D array.
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
Skill Endorsements: 6
Why are you using a 2d array? Is the string you need to change confined to one line?
iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34
It's the latter, I think. Look at vectors later on. In short they're expandable arrays which aren't set in size on build.
He wants to store the whole file, I believe.
You know where you fin.open and fout.open. Only deal with one aspect of the proess at a time. First open the file and store the contents. Then close it and open the out file and write the contents you have stored by reading from the first file. Then close the output file.
So change:
ifstream fin;
ofstream fout;
fout.open(File);
fin.open(File);
while (!fin.eof()){
fin.getline(line[counter], 125);
counter++;
}
for(counter=0; counter<10; counter++){
cout<<line[counter]<<endl;
}
fin.close();
to
ifstream fin;
// READ THE INPUT FILE
fin.close()
fout.open(File);
// WRITE THE OUTPUT FILE
fout.close();
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
Skill Endorsements: 6
>> is there a better way to do it?
Run this:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int main( ) {
string line, word;
ifstream in;
// ONE WAY OF READINF FROM A FILE
in.open( "FILENAME.TXT" );
if ( in ) // If stream opened successfully
while ( std::getline( in, line ) )
cout<< line << "\n";
in.close();
// OTHER WAY OF READING FROM FILE
in.open( "FILENAME.TXT" );
if ( in )
while ( in >> word )
cout<< word << " ";
in.close();
return 0;
}
And see if you can understand it. THe first way reads a file line by line and the other way does it word by word.
twomers
Posting Virtuoso
1,877 posts since May 2007
Reputation Points: 453
Solved Threads: 57
Skill Endorsements: 6
Please answer my question:
Is the string you need to change confined to one line?
If it is not neither example you have been shown will work.
iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34
If the string you need to find is not confined to one line, then reading through the text file line by line won't work.
A simple solution, would be to concatenate each line together and treat the entire text file as one long string.
Searching it would be the same as you search any other string. Perhaps you might need to take a little extra care with the left over '\n' (newlines) either discarding them altogether or keeping them.
I'm not sure.
iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34
Which part don't you get?
iamthwee
Posting Genius
6,254 posts since Aug 2005
Reputation Points: 1,567
Solved Threads: 476
Skill Endorsements: 34
Question Answered as of 5 Years Ago by
iamthwee
and
twomers