Reading a File Modified by A Different Process/Thread in C++ - c++

I want to dynamically read a file for the appended content. The file in written by another thread. I'm using ubuntu, and had a look at inotify, but wanted to try and implement a simple way for this since I only need to read the appended content. What I tried is as follows:
ifstream logFile("path_to_file", ifstream::in);
if(logFile.fail()) {
cout << "file not found" << endl;
exit(0);
}
while (true) {
string line;
getline(logFile, line);
if( logFile.eof() ) {
//wait for 2 seconds
logFile.clear();
}
if(!line.empty())
cout << line << endl;
However, when the EOF is encountered, the flag doesn't get reset when there is more content appended at the end and even if I call clear() to clear the error condition. What can be the issue in this approach? Do I need to open the file in a shared mode?
Thanks.

Related

For Loop not reading ifstream

I'm trying to read multiple files in a folder so I can parse through their data.
I first try to fill the list using a text document with all the file names in it, then based on that vector of string, continuously call ifstream so I can read every file and process the word data.
The problem I'm running into is that ifstream is failing to open all of the files, except one in the middle of the list?
Heres the output, its failing to read the dbfiles but they all have the right names?
These files aren't more than 8GB a piece so it should be able to handle it but it's not?
maybe theres a problem with the file paths?
std::ifstream dbfiles(argv[1]);
if (!dbfiles)
{
std::cerr << "Failed to open database " << argv[1] << " for reading." << std::endl;
}
std::string word;
std::vector<std::string> dbfile_names;
std::string file_name;
while (getline(dbfiles, file_name))
{ //reading in the file names
dbfile_names.push_back(file_name);
}//populate list of dbs
dbfiles.close();
for (unsigned int j = 0; j < dbfile_names.size(); j++)
{ //for every single file
std::ifstream dbfile(dbfile_names[j].c_str());
if (!dbfile)
{
std::cout << "Failed to open database file" << dbfile_names[j] << " for reading. READ FAILURE" << std::endl;
}else{
std::cout << "currently reading " << dbfile_names[j] << std::endl;
}
while (dbfile >> word)
{
//do stuff with the segments of data
//here I gather the data word by word and process it
}
dbfile.close();
}
I went into my debugger and found that due to getline, all the file names had a /r at the back of them.
The post over here Getting std :: ifstream to handle LF, CR, and CRLF?, helped describe the problem and how to easily fix it.
My files are now reading accordingly

ifstream not working with variable parameter using C++11 and c_str() appended

Note: I am using the C++11 standard, so I don't see why this isn't working with or without c_str() appended.
I have the following code:
// open streams
ifstream in(input);
ofstream out(output);
// get which file to open
in.ignore(INT_MAX, ':'); // we don't need the beginning part
in.ignore(); // remove trailing whitespace
string fileLocation;
getline(in, fileLocation);
out << "Loading: " << fileLocation << endl;
cout << "Loading: " << fileLocation << endl;
// now that we know where the file is, load it:
ifstream file(fileLocation);
which reads from a file that looks vaguely like this
File: file.txt
(Subcommands below)
I know that I am pulling the correct filename because of the terminal output.
Anyway, I noticed that the stream wasn't opening properly, so I added this conditional to check:
if ( !file )
{
cout << "File wasn't loaded properly." << endl;
}
And sure enough, I see that message when running the program.
My question is this: how come, when I hard-code the file location, e.g. ifstream file("file.txt") it opens up no problem? How do I get this working properly?

find word in a text in C++ and print some next specific lines

I wrote a code in C++ that writes a .txt file.
Then I want to open the code again and give some information, so I can get a new text depending on what I gave as an input.
For example I want to give the name of a month, and print in another .txt file all the lines that came after the word "November".
I found some solutions, but none of them worked for me!
One solution that I found on stack overflow is the following:
void Keyword(ifstream & stream, string token) {
string line;
while (getline(stream, line)) {
if (line.find(token) != string::npos) {
cout << line << endl;
}
}
cout << token << " not found" << endl;
}
I can't print the next lines with the code above.
Any suggestion would be helpful!
Thanks!
If you want to perform operations on files such as 'Read' and/or 'Write',you might want to search on the net(or if you have a C++ book) on topics such as "File I/O operations using C++". Anyways moving on, C++ has 2 basic classes to handle files which are ifstream and ofstream. And to use them you have to include ethier the header fstream(i.e #include<fstream>) or include them separately as #include<ifstream> and #include<ofstream>. ifstream is basically used for all input operations such as reading files etc. Similarly ofstream is used for all output operations such as writing data to files.
You can open a file and write data to it by doing the following,
ofstream myFile("filename");// Create an instance of ofstream and open file for writing data
and to write data to the file use the << operator like below,
myFile<<data;
Similarly, You can open a file and read data as follows,
ifstream myFile("filename");//Create an instance of ifstream and open file to read data
and to read data from the file use the >> operator as shown below,
myFile>>data;
You can also open a file using the method void open(const char *filename, ios::openmode mode); as shown below,
//Writing only
ofstream outFile;
outFile.open("filename.txt",ios::out);
//Reading only
ifstream inFile;
inFile.open("filename.txt",ios::in);
//For reading and writing
fstream file;
file.open("filename.txt",ios::in|ios::out);
//For closing File
outFile.close();
//or
inFile.close();
//or
file.close();
Note the open() method takes various flags such as ios::in for reading mode, ios::out for writing mode, ios::app for adding data to the end etc.
All of these can also combined by using the bit OR operator | as shown below,
outFile.open("filename.txt",ios::out|ios::app);
There is a lot more in IO. I just covered the things required to start.
Here is the solution to your problem. Try to understand it.
#include<iostream>
#include<fstream>
#include<cstring>
using namespace std;
int main()
{
ofstream outFile;
ifstream inFile;
char fileName[10],data[50];
int noLines;
cout<<"Enter Month:"<<endl;
cin>>fileName;
cout<<"Enter Number of lines you want to enter:"<<endl;
cin>>noLines;
outFile.open(fileName,ios::out);
cout<<fileName<<"(Enter Data):";
for(int i=0;i<=noLines;i++)
{
cin.getline(data,50);
outFile<<data<<endl;
}
outFile.close();
cout<<"Openening "<<fileName<<" :"<<endl;
inFile.open(fileName,ios::in);
for(int i=0 ;i<=noLines ;i++)
{
inFile.getline(data,50);
cout<<data<<endl;
}
inFile.close();
return 0;
}
OP has found most of the solution already:
string line;
while (getline(stream, line)) {
if (line.find(token) != string::npos) {
cout << line << endl;
}
}
cout << token << " not found" << endl;
But this only prints the lines with the keyword. And always prints the "not found" message. Ooops.
Instead I pitch:
string line;
bool found = false;
while (!found && getline(stream, line))
{ // search for keyword
if (line.find(token) != string::npos)
{
found = true; // found keyword. Stop looking
}
}
if (found)
{ // print out all remaining lines in the file
while (getline(stream, line))
{
cout << line << endl;
}
}
else
{
cout << token << " not found" << endl;
}
The above splits the finding of the token and the printing of the remaining file into two stages for readability. It can be compressed into one loop, but two things make this a sucker bet:
this program will be IO bound. It will spend the vast majority of its time reading the file, so little tweaks that do not address getting the file into memory are wasted time.
combining the loops would require the addition of logic to the loop that would, over along run, dwarf the minuscule cost of switching loops.
Try this:
http://www.cplusplus.com/doc/tutorial/files/
and this:
http://www.cplusplus.com/forum/beginner/14975/
It's about reading and writing files in c++ and about searching in files.

fstream using formatted data

i am new to this site , and this my first question !
i have a question about fstream function .
fstream f("new.dat",ios::out|ios::in);
fstream is for both input and output , so when we use it like this , and there is a new.dat file before it will output and input both . but it is strange , when i do that , it will output data correctly , but it is unable to input .
i found out if you close it , and reopen it , it will input . why it is like that??
int main()
{
fstream writeFile("newFile.dat", ios::out|ios::in);
char i[3];
char u[3]="HI";
if (!writeFile)
{
cerr << "error" << endl;
}
writeFile << u <<endl;
writeFile >> i;
cout << i << endl;
}
this is my full code , and result is an empty line.
The fstream object has a position in its output file, and since you opened it just for output and input without any position or writing modifiers, that position is at the end of the file. When you output i to the file, writeFile writes i to the file, and then moves its position past i so when you ask it to write more, you don't overwrite i.
You can reset the position to the start of the file with a call to writeFile.seekg(0), which places that internal position at the 0 position in the file (at the start).
If you're curious about stream manipulation, I'd suggest a look at cppreference.com and specifically its documentation on c++'s input and output libraries here.
Couple things going on here:
You can't open a file for reading if it doesn't exist, this includes a file you want to read and write. No file, no open.
Once you manage to open a file, the stream keeps track of where it is in the file. As you read or write, obviously the location moves.
There is only one location marker in the stream, so you can read to where you want to write, then write. Unfortunately this means any further reading will pick up after the write. If that's not what you want, get and store the current location (with tellg) before writing, and seek (with seekg) to the stored location after writing.
This has some problems such as what if the block of data you wish to insert is longer or shorter than the block of data you want to overwrite? The simple solution to this problem is read into buffer, edit buffer, write buffer back to file.
When you open a file and start writing into it, you overwrite whatever was in the file. If you want to add to a file, open with ios::app. This sets the stream's location to the end of the file. I am unaware of any sort of insert that pushes existing data along as you write in new data.
Some simple file handling example code
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
fstream f1("filename", ios::out);
if (f1.is_open())
{
if (f1 << "Hi")
{
cout << "wrote"<<endl;
}
f1.close();
}
fstream f2("filename", ios::out|ios::app);
if (f2.is_open())
{
if (f2 << " there!")
{
cout << "appended"<<endl;
}
f2.close();
}
fstream f3("filename", ios::in);
if (f3.is_open())
{
cout << f3.rdbuf()<< endl;
f3.close();
}
fstream f4("filename", ios::in|ios::out);
if (f4.is_open())
{
f4.seekg(3);
if (f4 << "Fred!")
{
cout << "overwrote"<<endl;
}
f4.close();
}
fstream f5("filename", ios::in);
if (f5.is_open())
{
cout << f5.rdbuf()<< endl;
f5.close();
}
// note the extra ! on the end left over from Hi there! I do not know how
// to get rid of this. I have always just done stuff like this to get around it.
fstream f6("filename", ios::in);
stringstream s1;
string token;
f6 >> token;
s1 << token << " Tim!";
f6.close();
fstream f7("filename", ios::out);
f7 << s1.rdbuf();
f7.close();
// and then moved temp over filename.
fstream f8("filename", ios::in);
cout << f8.rdbuf()<< endl;
f8.close();
}

C++ File I/O Have to re-open file to write to it? [duplicate]

This question already has answers here:
Reading and writing to the same file using the same fstream
(2 answers)
Closed 9 years ago.
I have a pretty simple code:
void LookupName( string tname ){ //Checks for name in records.txt and enters it if it does not exist
//Variables
string fname; //Used to store the name
string throwaway; //Used to dispose of extra data
bool found = false; //Used to track if a name is found in the file
//objects
fstream file ( STATS_FILE ); //Open the file
if (file.is_open()) {
while ( !file.eof() ) {
getline (file, fname, DELIM); //Fill name
getline (file, throwaway, '\n'); //throw away rest of line
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
if ( fname == tname ) { //Otherwise, continue
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
found = true;
}
}
if ( found == false ) { //if the name is not found
//Reopen the file so that we can write to it
file.close();
file.open( STATS_FILE, fstream::in | fstream::out | fstream::app );
cout << "Not found" <<endl;
Pause();
file << tname << ",0,0\n"; //Add it to the file with 0 wins and losses
}
//Cleanup
file.close();
}
}
This works, but if you notice at the bottom when I check to see if the name is found, I have to close and re-open the file.
The follow will not work for some reason:
void LookupName( string tname ){ //Checks for name in records.txt and enters it if it does not exist
//Variables
string fname; //Used to store the name
string throwaway; //Used to dispose of extra data
bool found = false; //Used to track if a name is found in the file
//objects
fstream file ( STATS_FILE, fstream::in | fstream::out | fstream::app ); //Open the file
if (file.is_open()) {
while ( !file.eof() ) {
getline (file, fname, DELIM); //Fill name
getline (file, throwaway, '\n'); //throw away rest of line
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
if ( fname == tname ) { //Otherwise, continue
cout << "Found: " << fname << " tname: " << tname << '\n';
Pause();
found = true;
}
}
if ( found == false ) { //if the name is not found
cout << "Not found" <<endl;
Pause();
file << tname << ",0,0\n"; //Add it to the file with 0 wins and losses
}
//Cleanup
file.close();
}
}
I'm curious to know why it doesn't work in the second example as it seems more efficient to open the file only 1 time with the correct flags, do what I need to do and close it.
I have a feeling this might have something to do with the position of the cursor, I have atttempted to use something like file.seekg(0) and file.seekg(0, ios_base::beg) but they don't seem to work as advertised (or I just mis understood the advertisement).
any input would be appreciated.
Edit: The couts were for debugging.
Edit 2: I Suppose I should emphasize the question a little more.
The problem is that the second example does not write to the file where as the first one does. I understand that there might be some concern regarding the !file.eof() condition but in this instance I don't care if it runs an extra time as it wont negatively effect the outcome (additionally, the text file that is being read from has been formatted correctly so that this won't happen).
Edit 3:
I created a very small program that ran:
//Testing bs
fstream file("Test.txt", fstream::in | fstream:: out | fstream::app );
string temp;
//ClearScreen
system(CLEAR_SCREEN);
file << "Line one\n";
getline(file, temp);
file << temp;
file << "Line two\n";
Pause();
return Menu;
Only the first line is written to the file. I'm betting that getline is changing the mode of the stream which is why it is unwritable afterwords.
Final Edit:
after a bunch of research it appears that in the above situation, re-opening the file is the best resolution. Ultimate,y the issue is in the use of getline() vs file.getline(). I'd have to rewrite far too much of the 1000 lines of the program to do it "correctly." Moral of the story? if you are having this issue, spend some time researching the difference between istream::getline and getline(string) and learn to identify when you are going to use which so that you don't get stuck in this situation. Fortunately, it's not imperative I fix it now, but it might be for others in the future.
I can't give you a definite answer, but I can give you a pretty good idea, based on inferences.
You are using fstream (file stream), instances of which are used for dealing with files. If you take a look at the documentation for fstream.open (link), notice that the second parameter is set up such that, by default, it will be either inputting or outputting to a file. Notice that the default is one or the other. This means that you can't assume, when you open the file, that the file exists. More importantly, since you might be inputting from the file, from the perspective of fstream, it shouldn't be assumed that if the file doesn't exist, it should be created.
Another way to think of it: I will assume that you are familiar with ifstream (input file stream) and ofstream (output file stream), which are generally introduced earlier in C++ tutorials/guides than fstream. If you take a look at the documentation for ifstream (link) and for ofstream (link), you'll notice that both of them derive from fstream. Now remember that when you call ifstream.open, if the file doesn't exist, it isn't created - rather, the failbit is set. Contrastingly, when you call ofstream.open, if the file doesn't exist, it is created.
Now, since ifstream and ofstream both derive from fstream, we have two possibilities:
ofstream.open derives directly from fstream.open (ie it has no extended functionality), and we can reasonably expect fstream.open to create file, given that it doesn't exist at first
ifstream.open derives directly from fstream.open, and we can reasonably expect fstream.open not to create a file, given that it doesn't exist at first
It should go without saying that both of these cannot be true; ofstream.open and ifstream.open cannot both derive from the same thing and yet differ in what they do; fstream.open cannot both do and not do something.
If you think about it, the second option is more likely, and here is why: If ofstream.open doesn't directly derive from fstream.open, all it has to do is add an extra step, in which it creates the file if it doesn't exist, and resets the failbit. All of the preceding behavior can be achieved by calling the open function of the parent class. On the other hand, if ifstream.open doesn't directly derive from fstream.open (implying that fstream.open creates the file, given that it doesn't exist at first), ifstream.open must be a complete re-implementation; otherwise there would be no way to skip the step in which the non-existent file is created.
Since it would be less efficient to have a function which re-implements so much than having a different function which simply adds something on, it's more likely that fstream.open doesn't create/open a file if it doesn't already exist.
I realize that this might not be a definitive solution to your problem, but I think this should explain why the second block of code you posted wouldn't (and shouldn't) work. When I look at your first code block (which you said works) I'm not entirely sure what about it makes it work, but I would guess it has something to do with your addition of | fstream::append to the open mode parameter of fstream.open.
I hope this helped.