I have a question about file input in c++. I want to be able to create a string variable and read in a SENTENCE from the file. How would I do this? This is the code I have so far.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string word;
ifstream fin;
// Open the file
fin.open("file.txt");
// Read in the sentence from the file
fin >> word;
cout << word << endl;
return 0;
}
The problem is, when I try to read in a sentence from the file, it only reads in a word and not the sentence.
So if I had a sentence in a textfile (assuming that there is no line break) it would only print out the single word.
How would I fix this?
If you just want to split on '.' then Jason Caldwell answer is what you were looking for:
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
static std::string trim(const std::string& str)
{
size_t first = str.find_first_not_of(" \n\t\r\v");
size_t last = str.find_last_not_of(" \n\t\r\v");
return str.substr(first, (last-first+1));
}
int main()
{
std::vector<std::string> sentences;
std::ifstream ifs("sentences.txt");
if (ifs.is_open() == false)
{ std::cerr << "Couldn't open file..." << std::endl; return -1; }
std::string line;
while(getline(ifs,line, '.'))
{ sentences.emplace_back(trim(line) + "."); }
for (auto sentence : sentences)
{ std::cout << "sentence: " << sentence << std::endl; }
ifs.close();
}
Note that this code uses c++11 features (auto , emplace_back...)
But if you assume that a sentence is something a bit more complex, I would suggest the same as Jason once again, use regexes. But be sure that your compiler implement them correctly (ex: g++-4.9)
This answer show you how to do it. You will probably have to split your string with std::getline for simplicity.
Edit: added check on file and note about c++11 features.
getline
is what you're looking for: http://www.cplusplus.com/reference/string/string/getline/
For more complex pattern matching, regex might be useful: http://www.cplusplus.com/reference/regex/
A similar question was posted here with lots of replies: http://www.cplusplus.com/forum/general/94419/
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
string word; // You have to give word an actual string for this to work xd
ofstream writer("file.txt");//Creating the text document // Creating the variable that writes to it
ifstream fin("file.txt");//The variable that reads it`
if(!writer){//Checking to make sure it nothing bad occurred when opening file
cout << "An error occurred opening " << writer;
return 1; //Return an error occurred
}else{
cout << word; // Print the string word whatever it is on file.txt
}
char letter;//To get every letter that get iterated over
vector <string> textInFile(999999);//Saves letter into an array
if(!fin){
cout<<"Problem opening"<<fin; // Check if file opened safely
}else{
for(int x = 0; ! fin.eof(); x++){//eof = End of file // It basically iterates over the entire .txt;
fin.get(letter); // Gets every letter
textInFile[x] = letter; // vector stores the letters
}
for(int x = 0; x < textInFile.size(); x++){ // For size of <vector> textInFile iterate over all indexes
cout << textInFile[x]; // Prints every letter in the vector
}
}
cout<<endl;
fin.close();
return 0;
}
Sorry about the messy post, this is like my first post and I have no clue how this boxing things works xd
Use getline
#include <iostream>
#include <fstream>
#include <string>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;
int main()
{
string sentence;
set <string> sentences;
ifstream file("thisfile.txt");
if(file.is_open())
{
while(getline(file, sentence, "."))
{
sentence.erase(remove_if(sentence.begin(), sentence.end(), IsChars("\n")), sentence.end());
sentences.insert(sentence);
}
}
file.close(); //close file
return 0;
}
The problem is, when I try to read in a sentence from the file, it only reads in a word and not the sentence.
The >>
extraction operator skips whitespace by default, hence, it only extracts one word.
To read a line at a time, you can use std::getline
. The following code will read the first line in the text file. You can put getline
inside a while loop to read line by line.
int main()
{
string line;
ifstream fin("file.txt");
// Read in the sentence from the file
getline(fin, line);
cout << line << '\n';
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.