how to select a sentence from a text with c++ - c++

i have a question, how to separate one file .txt into 3 files based on the keywords using c++. so each keyword has it's own sentence. so each new sub file contains keywords with their respective sentences. i have tried to show it on console, and it works, but i can't separate the text by it's keywords.
so i have a file. every sentence in this file
so I have a file. There are many sentences here. So, every sentence starts with the words error, warning, and information. how to separate each sentence starting with each of these words, and make them 3 separate files
can you help me please?
i've tried this code, and its failed.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main(){
ifstream myFile;
string data,output,buffer, line;
bool isData = false;
myFile.open("try.txt");
while(getline(myFile, buffer)){
if (buffer == "Error"){
getline(myFile,buffer);
cout<< buffer <<endl;
}
}
cin.get();
return 0;
}

Related

How to write and read Cyrillic properly from file?

I have the following code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string rus = "абвгдеёжзийклмнопрстуфхцчшщъыьэюяАБВГДЕЁЖЗИЙКЛМНОПРСТУФХЦШЩЪЫЬЭЮЯ";
string lat = "abvgděëžzijklmnoprstufhcčšŝŭeûâABVGDĚËŽZIJKLMNOPRSTUFHCČŠŜŬEÛÂ";
ifstream gdata("data.txt");
if(!gdata){
gdata.open("data.txt");
}
string temp;
while(gdata){gdata >> temp;}
gdata.close();
ofstream sdata("data.txt", ios::out | ios::trunc);
for(unsigned int i = 0; i < temp.length(); i++){
int index = rus.find(temp[i]);
if(index == -1){sdata << temp[i];}
else{sdata << lat[index];}
}
sdata.close();
return 0;
}
I would like to read Russian Cyrillic from a file. Then, program would find the index of each character in the string rus, and if it finds the character, then it finds the corresponding letter within lat string. This letter would then be written to the file.
Unfortunately, when I type something into the file and then run the program, I get weird output such as #>A8 with random squares (not visible here for some reason). How can I make my program read the Cyrillic properly?
I have already looked at over 10 questions here about similar subjects, but considering I'm very much a beginner in C++, nevermind encoding, I didn't understand the answers in the slightest, mainly as no example was provided that I could understand.
Also, even if most characters are latin and there is just one Cyrillic in the text, the entire text becomes malformed into random letters like #>A8

Read from text file C++ (fname, lname, class, seat number), store and verify

I have a text file of the classlook like this:
FName LName Class SeatNum
FName2 LName2 Class2 SeatNum2
...
and the list goes on.
How to read lines of strings and store them into different variables?
How to combine Class & SeatNum to be an ID (3D-20)?
How to verify for every input name and ID has to be matched?
For example, input > FName LName Class2-SeatNum2 is wrong, please try again.
Your help is greatly appreciated.
Thanks!
Just a note for next time - because you didn't detail the problem, it was hard to figure out what you mean. Anyhow:
in order to do what you asked you need to:
a) read the data from the file
b) split the data based on the character which is between the cells.
In C++, The split string algorithm is in boost - if you dont know what that is, make sure you take a look in here: http://www.boost.org/
Soltion:
I`m modifying various cPlusPlus guides here to fit your purpouse:
#include <sstream>
#include <iostream>
#include <fstream>
#include <vector>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/classification.hpp>
using namespace std;
vector<string> getData (string filePath) {
vector<string> Cells; // In the end, here we will store each cell's content.
stringstream fileContent(""); // This is a string stream, which will store the database as a string.
ofstream myfile; // the file which the database is in
myfile.open (filePath); // Opening the file
while ( getline (myfile,line) ) // Reading it until it's over
{
fileContent << line; // adding each line to the string
}
split(Cells, fileContent.str(), is_any_of(" "));// Here, insert the char which seperates the cells from each other.
myfile.close()
return Cells; // returning the split string.
}
Hope i helped :)

replace and write to file c++

I want write code to find words in a file and replace words.
I open file, next I find word. I have a problem with replace words.
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
string contain_of_file,a="car";
string::size_type position;
ifstream NewFile;
NewFile.open("plik1.txt");
while(NewFile.good())
{
getline(NewFile, contain_of_file);
position=contain_of_file.find("Zuzia");
if(position!=string::npos)
{
NewFile<<contain_of_file.replace(position,5, a );
}
}
NewFile.close();
cin.get();
return 0;
}
How can I improve my code?
lose the using namespace std;
don't declare the variables before needed;
I think the English word you were looking for was content -- but I am not an English-native speaker;
getline already returns NewFile.good() in boolean context;
No need to close NewFile explicitly;
I would change the casing on the NewFile variable;
I don't think you can write to an ifstream, and you ought to manage how you are going to replace the contents of the file...
My version would be like:
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
int main() {
std::rename("plik1.txt", "plik1.txt~");
std::ifstream old_file("plik1.txt~");
std::ofstream new_file("plik1.txt");
for( std::string contents_of_file; std::getline(old_file, contents_of_file); ) {
std::string::size_type position = contents_of_file.find("Zuzia");
if( position != std::string::npos )
contents_of_file = contents_of_file.replace(position, 5, "car");
new_file << contents_of_file << '\n';
}
return 0;
}
There are at least two issues with your code:
1. Overwriting text in a file.
2. Writing to an ifstream (the i is for input, not output).
The File object
Imagine a file as many little boxes that contain characters. The boxes are glued front to back in an endless line.
You can take letters out of boxes and put into other boxes, but since they are glued, you can't put new boxes between existing boxes.
Replacing Text
You can replace text in a file as long as the replacement text is the same length as the original text. If the text is too long, you overwrite existing text. If the replacement text is shorter, you have residual text in the file. Not good in either method.
To replace (overwrite) the text, open the file as fstream and use the ios::in and ios::out modes.
Input versus Output
The common technique for replacing text is to open the original file for *i*nput and a new file as *o*utput.
Copy the existing data, up to your target text, to the new file.
Copy the replacement text to the new file.
Copy any remaining text to the new file.
Close all files.

Debug Assertion Failed File, tokenScanner, and text files

I have written a program that processes text files one at a time and extract relevant information. My program works well with some of the text files and not others. There is no obvious difference between the files that run seamlessly through my program and those that don't.
As far as the problematic files are concerned:
the program opens the file
it reads in and processes a good chunk of the lines one at a time as it should
But then it reaches a problem line and gives the error message:
"Debug Assertion Failed File:
f:/dd/vctools/crt_bld/self_x86/src/isctype.c
Line: 56
Expression: (unsigned)(c+1) <= 256"
When I enter the debugger mode the problem seems to arise from the "while(tokenScanner)" loop in my code below. I pulled up the content of the problem line being processed and compared that across a couple of problem files and I found that the Assertion Failure message pops up at </li> where the last token being processed is ">". It's not clear to me why this is a problem. This particular token in the original text file is contiguous with <li in the form </li><li. Therefore the scanner is having trouble half way throught this string.
Any thoughts on why this is and how I can fix this? Any advice would be much appreciated!
Here is the relevant portion of my code:
#include <string>
#include <iostream>
#include <fstream> //to get data from files
#include "filelib.h"
#include "console.h"
#include "tokenScanner.h"
#include "vector.h"
#include "ctype.h"
#include "math.h"
using namespace std;
/*Prototype Function*/
void evaluate(string expression);
Vector<string> myVectorOfTokens; //will store the tokens
Vector<string> myFileNames;
/*Main Program*/
int main() {
/*STEP1 : Creating a vector of the list of file names
to iterate over for processing*/
ifstream infile; //declaring variable to refer to file list
string catchFile = promptUserForFile(infile, "Input file:");
string line; //corresponds to the lines in the master file containing the list files
while(getline(infile, line)){
myFileNames.add(line);
}
/* STEP 2: Iterating over the file names contained in the vector*/
int countFileOpened=0; //keeps track of number of opened files
for (int i=1; i< myFileNames.size(); i++){
myVectorOfTokens.clear(); //resetting the vector of tokens for each new file
string fileName;
string line2;
ifstream inFile;
fileName= myFileNames[i];
inFile.open(fileName.c_str()); //open file convert c_str
if (inFile){
while(getline(inFile, line2)){
evaluate(line2);
}
}
inFile.close();
countFileOpened++;
}
return 0;
}
/*Function for Extracting the Biographer Name*/
void evaluate(string line){
/*Creating a Vector of Tokens From the Text*/
TokenScanner scanner(line); //the constructor
while (scanner.hasMoreTokens()){
string token=scanner.nextToken();
myVectorOfTokens.add(token);
}
}
while(!inFile.eof()
is just wrong (in almost any case)
while(getline(inFile, line2))
evaluate(line2);
is better

New to <dirent.h>, trying to access data in a directory

I've never used dirent.h before. I was using istringstream to read through text files (singular), but have needed to try to revise the program to read in multiple text files in a directory. This is where I tried implementing dirent, but it's not working.
Maybe I can't use it with the stringstream? Please advise.
I've taken out the fluffy stuff that I'm doing with the words for readability. This was working perfectly for one file, until I added the dirent.h stuff.
#include <cstdlib>
#include <iostream>
#include <string>
#include <sstream> // for istringstream
#include <fstream>
#include <stdio.h>
#include <dirent.h>
void main(){
string fileName;
istringstream strLine;
const string Punctuation = "-,.;:?\"'!##$%^&*[]{}|";
const char *commonWords[] = {"AND","IS","OR","ARE","THE","A","AN",""};
string line, word;
int currentLine = 0;
int hashValue = 0;
//// these variables were added to new code //////
struct dirent *pent = NULL;
DIR *pdir = NULL; // pointer to the directory
pdir = opendir("documents");
//////////////////////////////////////////////////
while(pent = readdir(pdir)){
// read in values line by line, then word by word
while(getline(cin,line)){
++currentLine;
strLine.clear();
strLine.str(line);
while(strLine >> word){
// insert the words into a table
}
} // end getline
//print the words in the table
closedir(pdir);
}
You should be using int main() and not void main().
You should be error checking the call to opendir().
You will need to open a file instead of using cin to read the contents of the file. And, of course, you will need to ensure that it is closed appropriately (which might be by doing nothing and letting a destructor do its stuff).
Note that the file name will be a combination of the directory name ("documents") and the file name returned by readdir().
Note too that you should probably check for directories (or, at least, for "." and "..", the current and parent directories).
The book "Ruminations on C++" by Andrew Koenig and Barbara Moo has a chapter that discusses how to wrap the opendir() family of functions in C++ to make them behave better for a C++ program.
Heather asks:
What do I put in getline() instead of cin?
The code at the moment reads from standard input, aka cin at the moment. That means that if you launch your program with ./a.out < program.cpp, it will read your program.cpp file, regardless of what it finds in the directory. So, you need to create a new input file stream based on the file you've found with readdir():
while (pent = readdir(pdir))
{
...create name from "documents" and pent->d_name
...check that name is not a directory
...open the file for reading (only) and check that it succeeded
...use a variable such as fin for the file stream
// read in values line by line, then word by word
while (getline(fin, line))
{
...processing of lines as before...
}
}
You probably can get away with just opening the directories since the first read operation (via getline()) will fail (but you should probably arrange to skip the . and .. directory entries based on their name). If fin is a local variable in the loop, then when the outer loop cycles around, fin will be destroyed, which should close the file.