streams and files question

Apr 3, 2012 at 1:21pm
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.

Not sure what the best way to attack this one would be... Any suggestions?
Apr 3, 2012 at 1:26pm
Apr 3, 2012 at 1:26pm
input, calculate, output.
Apr 3, 2012 at 3:33pm
Thanks. But how do I process each word in a stream? My professor said something like you have to set up a constant char for each space and once it gets to that that is a new word. He also said said something about setting up EOLN='/n'. I don't know how to go about actually setting up this code. Any tips?
Apr 3, 2012 at 3:49pm
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
#include <iostream> //for the input output stream
#include <fstream> //for file stream
#include <cstring> //for strtok

using namespace std; //all objects are under namesapce std

int main(){
int wordCount = 0; //to store word count
char* fileName; //to store file name
cout << "\nInput File Name : ";
cin.getline(fileName); //to get the file name from user

ifstream file(fileName);//open the file for input

while(!file.eof()){ //continue to end of file
 char* line; //to store a line
 char* word; //to store a word

 file.getline(line); //get a line from file
 
 word = strtok(line," "); //get a pointer to string till space
 
 while(word){ //till getting a null
   
  word = strtok(NULL," "); //keep getting string
  cout << word << endl; //print the word
  wordCount++; //increment wordCount
 
}

}

cout << "\nTotal words in file" << fileName << " are " << wordCount;
return 0;
}


The example does need debugging, but would give you an idea of how to do the job.
Last edited on Apr 3, 2012 at 3:51pm
Apr 3, 2012 at 5:09pm
Thanks for your help. I tried out your code and am in the process of debugging it. I am unfamiliar with using strtok so I am having a little trouble debugging this.

1>------ Build started: Project: lab 8, Configuration: Debug Win32 ------
1> l8.cpp
1>g:\csp\lab8\lab 8\lab 8\l8.cpp(14): error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
1>g:\csp\lab8\lab 8\lab 8\l8.cpp(22): error C2661: 'std::basic_istream<_Elem,_Traits>::getline' : no overloaded function takes 1 arguments
1> with
1> [
1> _Elem=char,
1> _Traits=std::char_traits<char>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


No overloaded function takes 1 arguments? I think it is referring to the cin.getline(filename) .
I don't see what is wrong becuase it was not predeclared with a certain amount of inputs. Any suggestions?
Apr 3, 2012 at 5:31pm
try

cin.getline(fileName, 80); //read 80 chars from input into fileName
Apr 3, 2012 at 5:33pm
and I think then you have to change all the instances of

source.getline(storingVariable, numberOfChars);
Apr 3, 2012 at 5:38pm
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
using namespace std; //all objects are under namesapce std

int main(){
int wordCount = 0; //to store word count
char* Ch8_3; //to store file name
cout << "\nInput File Name : ";
cin.getline(Ch8_3,80); //to get the file name from user

ifstream file(Ch8_3);//open the file for input

while(!file.eof()){ //continue to end of file
 char* line; //to store a line
 char* word; //to store a word

 file.getline(line,80); //get a line from file
 
 word = strtok(line," "); //get a pointer to string till space
 
 while(word){ //till getting a null
   
  word = strtok(NULL," "); //keep getting string
  cout << word << endl; //print the word
  wordCount++; //increment wordCount
 
}

}

cout << "\nTotal words in file" << Ch8_3 << " are " << wordCount;
return 0;
}


Ok thanks. I did the same with the second one to fix it as well (file.getline(line,80)). Both of these errors were fixed.

I also changed fileName to the file name of my text document (Maybe I shouldnt have done this). I get no errors until the cmd actually pops up.

Then it says Debug error! Run-time Check Failure #3 - the variable ch8_3 is being used without being initialized. Press retry...

Initialized? I am not sure what it means by that. The text document exists and we referenced it... Thanks.
Apr 3, 2012 at 5:50pm
If you have a file Ch8_3.txt

then remove

1
2
3
4
char* Ch8_3; //to store file name
cout << "\nInput File Name : ";
cin.getline(Ch8_3,80); //to get the file name from user
ifstream file(Ch8_3);//open the file for input 


and just use

ifstream file("Ch8_3.txt");//open the file for input
Apr 3, 2012 at 5:56pm
Ok thanks. I just tried that. It still gives me a debug error when I open up the cmd. This time it says:

Runt time check #3. The variable line is being used without being inititialized... I thought we initialized line here :
char* line; //to store a line
Thanks for your help. Sorry for the nooby questions. I am really new to this :(
Apr 3, 2012 at 5:57pm
Initialized? I am not sure what it means by that. The text document exists and we referenced it...


1
2
3
char* Ch8_3; //to store file name
cout << "\nInput File Name : ";
cin.getline(Ch8_3,80); //to get the file name from user  


Here you use Ch8_3 just as a variable name, so once get
Input File Name :

you enter the file name,
Ch8_3.txt

otherwise if you just press ENTER / RETURN, it gets no value (in other words it is uninitialized, as we just declared this variable but did not assign any value to it). So when you use it in

ifstream file("Ch8_3.txt");//open the file for input

There is nothing in Ch8_3, thus a Run time Error

but in this

ifstream file("Ch8_3.txt");//open the file for input

You are directly passing the value.
Last edited on Apr 3, 2012 at 6:03pm
Apr 3, 2012 at 6:00pm
Ok I understand. So the original way would have allowed me to do this for multiple different text documents because you input it in the program. Where as in the second method it only works for ch8_3. I understand that now, but how do I get rid of the error I am getting in above post. (btw I am using the second way of directly passing the value right now)
Apr 3, 2012 at 6:09pm
Apr 3, 2012 at 6:21pm
Yah I checked that out earlier. So I understand what it does with the tokens.
I went back to the original way where you actually input the file you want to run the code on. Here is my current code:
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
#include <iostream>
#include <fstream>
using namespace std;
#include <iostream> //for the input output stream
#include <fstream> //for file stream
#include <cstring> //for strtok

using namespace std; //all objects are under namesapce std

int main(){
int wordCount = 0; //to store word count
char* fileName; //to store file name
cout << "\nInput File Name : ";
cin.getline(fileName,80); //to get the file name from user

ifstream file(fileName);//open the file for input

while(!file.eof()){ //continue to end of file
 char* line; //to store a line
 char* word; //to store a word

 file.getline(line,80); //get a line from file
 
 word = strtok(line," "); //get a pointer to string till space
 
 while(word){ //till getting a null
   
  word = strtok(NULL," "); //keep getting string
  cout << word << endl; //print the word
  wordCount++; //increment wordCount
 
}

}

cout << "\nTotal words in file" << fileName << " are " << wordCount;
return 0;
}


I am getting this error:

http://snag.gy/g8khP.jpg

Any suggestions? thanks.
Apr 3, 2012 at 6:37pm
Try replacing

char* fileName; //to store file name

with

char fileName[81]; //to store file name
Apr 3, 2012 at 6:40pm
Thanks. But now I get run time check 3 variable line is being used without being inititialized.
Apr 4, 2012 at 1:11pm
Try replacing

char* line; //to store file name

with

char line[81]; //to store file name
Last edited on Apr 4, 2012 at 1:12pm
Topic archived. No new replies allowed.