Well, I need to store it in an array,
then using the array, code how many number of words are in the document, deleting a string from the document, inserting a string, all that...
If you want to use an array, then you will have to make two passes to the file: one to count the number of words in it, and the other to store the words in an array.
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
// Count the number of words in the file.
ifstream file;
int wordCount = 0;
file.open("input.txt");
string s;
file >> s;
while (file)
{
wordCount++;
file >> s;
}
file.close();
// Store the words in an array.
string v[wordCount];
wordCount = 0;
file.open("input.txt");
file >> s;
while (file)
{
v[wordCount++] = s;
file >> s;
}
file.close();
// Display the contents of the array.
for (int i = 0; i < wordCount; i++)
{
cout << v[i] << ' ';
}
return 0;
}
Very similar except that you need to know the maximum size of the file first or the content will be cut off. This one will read the first 100 lines from a file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
ifstream fin("input.txt");
string line[100];
for (int i = 0; i < 100; ++i)
{
getline(fin, line[i]);
if ( !fin.good() )
break;
}
return 0;
}