Public Group

# inputting a text file

This topic is 2106 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Well I am attempting to read in a text file using c++. I am using vs 2010. Where should I put my text file so that my program can find it. Here is the code I am using.

#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
using namespace std;
int main()
{
double index=0;
int syllabus,words,sentences;
string filename;
cout << "Enter the data file name: ";
cin >> filename;
ifstream infile;
infile.open(filename.c_str());

if(infile.fail())
{
cout << "Error opening " << filename << endl;
system("pause");
return 1;
}
?
infile.close();
system("pause");
return 0;
}


##### Share on other sites

I have another question how do I read in a text file word by word?

##### Share on other sites

Well I am attempting to read in a text file using c++. I am using vs 2010. Where should I put my text file so that my program can find it. Here is the code I am using.

#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
using namespace std;
int main()
{
double index=0;
int syllabus,words,sentences;
string filename;
cout << "Enter the data file name: ";
cin >> filename;
ifstream infile;
infile.open(filename.c_str());

if(infile.fail())
{
cout << "Error opening " << filename << endl;
system("pause");
return 1;
}
?
infile.close();
system("pause");
return 0;
}


That particular code will try to open the file relative the current working directory. What the working directory is depends on how you run your program. For example, if you run your program from the explorer by, say, double clicking it, the working directory is the directory of the program executable itself. If you make a short cut to your program, you can set the working directory within the short cut. If you execute the program from an IDE, the working directory is typically where the project file is, by default.

edit for clarification: The file is relative the working directory if you specify a relative file name. If you specify an absolute file name, then the file name in the absolute name on your file system and the working directory is irrelevant.

I have another question how do I read in a text file word by word?

std::string word;
infile >> word;


Put that in a loop to read as many words as you want.

Edited by Brother Bob

##### Share on other sites

I solved my problem locating the text file, now I will work on reading my text file word by word, thanks bob.

##### Share on other sites

is there any way to count the number of syllables in a text file.

##### Share on other sites

If you want to do that algorithmically and accurate, you need some serious linguistical research information on how to split a word into syllables properly.

If you want some potential suggestions to get started, you can look up the hyphenation algorithm Latex uses to hyphenate words. The hyphenation points corresponds roughly to the split between syllables, so the number of syllables is one more than the number of hyphenation points. The algorithm is not trivial and also contains a fair number of pre-hyphenated words that overrides the algorithm itself.

Another approximation is to split the syllables at sequences of consecutive consonants between two vowels. Had a look at a few long words in this thread, and seems like a decent approximation.

##### Share on other sites

Another approximation is to split the syllables at sequences of consecutive consonants between two vowels. Had a look at a few long words in this thread, and seems like a decent approximation.
I am going to use this approximation.

##### Share on other sites

Maybe if you said what the heckers you wanted to extract syllables for we might be able to help more. If you are writing a karaoke app, I expect they would split the words by hand and don't use a magic algorithm (and doing a karaoke app with cout is not going to be too good anyway).

Is this an exercise from your book, because if it is I expect you have read the exercise requirements incorrectly...

##### Share on other sites

well I am able to count the number of words in a sentence. however I am unable to count the number of syllables in words. also my code counts two periods at the end of a sentence not just one.

#include <iostream>
#include <string>
#include <math.h>
#include <fstream>
using namespace std;
int main()
{
double index=0;
int syllabus,words=0,sentences=0;
string filename;
cout << "Enter the data file name: ";
cin >> filename;
ifstream infile;
infile.open(filename.c_str());
if(infile.fail())
{
cout << "Error opening " << filename << endl;
system("pause");
return 1;
}
while(!infile.eof())
{
string word;
infile >> word;
cout << word << endl;
if(word.size() > 0)
words++;
}
cout << words << endl;
infile.close();
infile.open(filename.c_str());
while(!infile.eof())
{
char sentence;
infile >> sentence;
cout << sentence << endl;
if(sentence=='.'|| sentence=='?'||sentence=='!'||sentence==':'||sentence==';')
{
sentences++;
}
}
cout << sentences << endl;
infile.close();
system("pause");
return 0;
}


##### Share on other sites

Only count a sentence if there have been any letters before the period.

Edited by Brother Bob

• 13
• 18
• 29
• 11
• 27