Click here to Skip to main content
14,766,261 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote:

void contare()

{
	
vector <string> va;

fstream fs;

int n=0;

char ch;
		
fs.open("C:\\Users\\Mio\\Desktop\\ciao.txt",ios::in);

while (! fs.eof())

{
 	ch = fs.get();
 	
 	cout<<ch;
 	
va[n] += ch;
 
if (ch== ' ')
 n++;


}


fs.close();

cout<<n;
	

  //cout<<worda<<endl;
}


What I have tried:

but it doesn't work, when I wrote in vectore there is a block, have you have some ideas?
Posted
Updated 28-Oct-19 6:01am
v2
Comments
Patrice T 28-Oct-19 10:48am
   
What is a "block" ?
Describe the problem
Member 14594285 28-Oct-19 10:54am
   
in my file there is written:
"ciao ciao ciao"

program prints only "c"

Hi Valentina, there is no need to explicitely handle file opening/closing. You could make the program accepting input from standard in and then redirect the intended file on program call. e.g.
my_program < my_input_file

Also, there is no need to explicitely find word boundaries, cin can gently do such a job for you.
Try
#include <iostream>
#include <vector>
using namespace std;

int main()
{
  vector <string> vw; // vector of 'words'

  while (true)
  {
    string word;
    cin >> word;
    if ( ! cin ) break;
    vw.push_back(word);
  }

  cout << "count of words is " << vw.size();
}
   
Comments
Member 14594285 28-Oct-19 11:03am
   
yes but in this way I write always in a string, I wanto yo write only a word in vw[0], vw[1]...ecc..
k5054 28-Oct-19 11:23am
   
Why make things more complicated than they have to be? Still, if you insist then:
string s;
while( !fs.eof() ) {
    ch = fs.get();
    if(ch == ' ') {
       v.push_back(s);
       s.erase();
    } else {
       s += ch;
    }
}

Note that both CPallini and I have given very similar answers, so that might be a clue how an experienced developer might approach the problem. You might want to think about why you want to read a single char at a time. Note that for large files, reading single characters at a time may be significantly slower than reading word by word.
Unlike arrays, vectors have no fixed length. When you define a vector as vector<type> v, it has an initial length of zero. That means that if you try to reference any vector element, you will most likely get a program crash due to attempting to access unassigned memory. In general you want to use vector.push_back() to add elements to the end of a vector.

Try this
ifstream ifile("myfile.txt");
vector<string> words;
string word;
while(ifile >> word) {
    words.push_back(word);
}
   
Comments
Member 14594285 28-Oct-19 11:30am
   
I wote:
void contare(vector<string> words)

{
ifstream ifile("C:\\Users\\Mio\\Desktop\\ciao.txt");

string word;

while(ifile >> word)
{
words.push_back(word);
}




}


int main()
{
vector<string> words;



contare(words);



but it doesn't works when I pass wors to contare
k5054 28-Oct-19 11:40am
   
C++ uses "pass by value" by default. That means that when you have
void foo(vector<string> words)
{
   ...
}
int main()
{
    vector<string> v;
    foo(v);
}
the call to foo() creates a copy of vector v, passes that to foo(). When foo() returns, the copy is destroyed. What you want to do is use pass by reference. Look up references in C++.
CPallini 28-Oct-19 11:48am
   
Then write:
vector <string> contare()
{
ifstream ifile("C:\\Users\\Mio\\Desktop\\ciao.txt");

string word;
vector <string> words;
while(ifile >> word)
{
words.push_back(word);
}
return words;
}

and call it this way:
int main()
{
vector<string> words = contare();
//...
Member 14594285 28-Oct-19 12:23pm
   
thanks a lot, I solved

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900