Reading and writing files program

Write a program system that reads several lines of information from a data file and prints each word of the file on a separate line of an output file followed by the number of letters in that word.

Also, print a count of words in the file on the screen when done. Assume that words are separated by one or more blanks.

Thus far I have only opened the in and out file that we are reading and writing to. I know I have to use some type of getline function which reads each line and separates each individual word. After that I need to us a function with using getnextchar to count characters.

I think I can figure out the character counting part, but am having trouble with the getting each word from the lines. I know it is like: get char until next chat =s ' ' and this equals a word. Keep doing this until you hit NWLN or eds.eof.
this may be completely wrong^^ Can you steer me in the right directions?
thanks.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <string> 
#include <fstream>
#define inFile "E:\\CSP\\lab8l\\lab8 redo\\Ch8_3.txt"
#define outFile "E:\\CSP\\lab8l\\lab8 redo\\out.txt"
using namespace std;
void processData(ifstream&, ofstream&);
using namespace std;
string getwords(ifstream&, ofstream&);
char NWLN ='\n';
string words;
char nextchar;
int charCount; 
ifstream ids;
ofstream ods;

int main()
{
ifstream ids;
ofstream ods;



ids.open(inFile);  // I know this opens the files I am reading from and writing to.
ods.open(outFile);
if (ids.fail()){
	cerr<<"error can't open "<< inFile<<" for input"<< endl;
	return EXIT_FAILURE;
}
if (ods.fail()){
	cerr<<"error can't open "<< outFile<<" for input "<< endl;
	return EXIT_FAILURE;
}
//processData(ids, ods);

ids.close();
ods.close();
return 0;
}
string getwords(ifstream&, ofstream&)
{
	while(words!=NWLN)
	getline(ids,words,' ');
You will need a loop to loop through your input file, word by word, then out put it to the out file.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
using namespace std;

int main ()

{
  string tempWord;
  ifstream inFile;
  ofstream outFile;
  inFile.open("read.txt", ios::in);
  outFile.open("out.txt", ios::app);
    while (inFile >> tempWord)
    {

outFile << tempWord<<endl;


    }

}
WIll that separate each word based on spaces though? So since I am using functions for this project, I just need to turn the above into a function and then it will work?
That will take each word (and by word in a text file, i mean each group of characters on their own, each word is separated by a space), and place it on a new line. If it doesnt work in one funciton, you might split it up into two functions, but for this one you dont need to.
If you want each word to be on the same line in a space, just remove: <<endl; and put: cout <<" "; . or you can format it however you want.

You could give the user the option to enter what to put in the first file, and then it writes to then other file.

so:
1
2
3
4
5
6
string input;

cout << "Write something to the file: ";
cin >> input;
outFile.open("output.txt");
outFile << input; //writes what the user entered to the file; 

In fact, I suspect you're not supposed to do it this way because the implementation is terribly simple.

Depending on the requirements and contents of the file you could change line 16 to:

outFile << tempWord << ' ' << tempWord.length() << endl

and be done, but I think your instructor would like for you to use getline and work out the logic for separating words yourself.
Topic archived. No new replies allowed.