Is there any way to determine how many separate strings are in a file? I know that using ifstream to open a file for reading, and using that name to output to a string you can output them one by one, but is there a function to count them?
Is there any way to determine how many separate strings are in a file? I know that using ifstream to open a file for reading, and using that name to output to a string you can output them one by one, but is there a function to count them?
yo
It depends on what you consider to be a string. A text file is one big string, so you can be perfectly correct doing this:
Of course, you would be assuming that the file is not empty. If you consider a string a sequence of characters ending with a newline, simply read lines from the file and increment a counter. The same goes with sequences of characters separated by whitespace except you can use the >> operator instead of reading a while line.Code:size_t strings_in_file ( ifstream& in ) { return 1; }
My best code is written with the delete key.
you will have to read the file in to work with it
read in the chars to a pointer
Code:while (*ptr != '\0') { if (*ptr == ' ') { ++counter; } ptr++; } cout << "There are " << counter + 1 << " words\n";