can't iterate over a file correctly - c++

std::fstream pasaka("my_file", std::fstream::in | std::fstream::out);
std::fstream pusuku("my_file1", std::fstream::in | std::fstream::out);
std::string line;
if (pasaka.is_open())
{
//while (std::getline(pasaka, line))
//std::cout << line << std::endl;
// pusuku << line << std::endl;
while (!pasaka.eof())
{
if (pasaka.get() == 'a')
{
pasaka.seekp((pasaka.tellp() - static_cast<std::streampos>(1)));
pasaka.put('u');
pasaka.seekp(pasaka.tellp());
}
else if (pasaka.get() == 'A')
{
pasaka.seekp((pasaka.tellp() - static_cast<std::streampos>(1)));
pasaka.put('U');
pasaka.seekp(pasaka.tellp());
}
}
pusuku.close();
pasaka.close();
}
else {
std::cout << "Faild to open" << '\n';
}
return 0;
the issues:
if the code that is commented (first while loop and everything in it ) the program does 0 switches
2)if A is found it doesn't delete the A characther but just adds U
result received: ( from txt file )
uuuuuuu
UAUAAAA
result expected ( from txt file )
uuuuuuu
UUUUUUU

Related

ifstream: /dev/stdin is not working the same as std::cin

For my formation, an exercise ask us to create a program similar to the linux 'cat' command.
So to read the file, i use an ifstream, and everything work fine for regular file.
But not when i try to open /dev/ files like /dev/stdin: the 'enter' is not detected and so, getline really exit only when the fd is being closed (with a CTRL-D).
The problem seems to be around how ifstream or getline handle reading, because with the regular 'read' function from libc, this problem is not to be seen.
Here is my code:
#include <iostream>
#include <string>
#include <fstream>
#include <errno.h>
#ifndef PROGRAM_NAME
# define PROGRAM_NAME "cato9tails"
#endif
int g_exitCode = 0;
void
displayErrno(std::string &file)
{
if (errno)
{
g_exitCode = 1;
std::cerr << PROGRAM_NAME << ": " << file << ": " << strerror(errno) << std::endl;
}
}
void
handleStream(std::string file, std::istream &stream)
{
std::string read;
stream.peek(); /* try to read: will set fail bit if it is a folder. */
if (!stream.good())
displayErrno(file);
while (stream.good())
{
std::getline(stream, read);
std::cout << read;
if (stream.eof())
break;
std::cout << std::endl;
}
}
int
main(int argc, char **argv)
{
if (argc == 1)
handleStream("", std::cin);
else
{
for (int index = 1; index < argc; index++)
{
errno = 0;
std::string file = std::string(argv[index]);
std::ifstream stream(file, std::ifstream::in);
if (stream.is_open())
{
handleStream(file, stream);
stream.close();
}
else
displayErrno(file);
}
}
return (g_exitCode);
}
We can only use method from libcpp.
I have search this problem for a long time, and i only find this post where they seems to have a very similar problem to me:
https://github.com/bigartm/bigartm/pull/258#issuecomment-128131871
But found no really usable solution from them.
I tried to do a very ugly solution but... well...:
bool
isUnixStdFile(std::string file)
{
return (file == "/dev/stdin" || file == "/dev/stdout" || file == "/dev/stderr"
|| file == "/dev/fd/0" || file == "/dev/fd/1" || file == "/dev/fd/2");
}
...
if (isUnixStdFile(file))
handleStream(file, std::cin);
else
{
std::ifstream stream(file, std::ifstream::in);
...
As you can see, a lot of files are missing, this can only be called a temporary solution.
Any help would be appreciated!
The following code worked for me to deal with /dev/fd files or when using shell substitute syntax:
std::ifstream stream(file_name);
std::cout << "Opening file '" << file_name << "'" << std::endl;
if (stream.fail() || !stream.good())
{
std::cout << "Error: Failed to open file '" << file_name << "'" << std::endl;
return false;
}
while (!stream.eof() && stream.good() && stream.peek() != EOF)
{
std::getline(stream, buffer);
std::cout << buffer << std::endl;
}
stream.close();
Basically std::getline() fails when content from the special file is not ready yet.

ofstream outputfile() in while loop usually makes empty file

I encountered a funny issue with this code but I am wondering why this happened:
#include <stdio.h>
#include <unistd.h>
#include <iostream>
#include <wiringPi.h>
#define BUTTON_PORT 25
#define FILE_PATH "/path/to/output.txt"
int main(void) {
int buttonState = 0;
int pastButtonState = 0;
int buttonCounter = 0;
if (wiringPiSetupGpio() == -1) return 1;
pinMode(BUTTON_PORT, INPUT);
while(buttonCounter < 100){
if (buttonState == 1 && pastButtonState == 0) buttonCounter++;
usleep(100);
ofstream outputfile(FILE_PATH);
outputfile << buttonCounter << endl;
outputfile.flush();
outputfile.close();
pastButtonState = buttonState;
}
I expected output.txt to show current # of buttonCounter,
but file is always empty, and 1 of 10 times the file shows # of buttonCounter.
(with continuously exec less /path/to/output.txt)
I know certain repeat of ofstream outputfile in while loop is not a good solution, but I have no idea why output.txt is usually empty.
I might think it's ugly to use ofstream outputfile in while loop since ofstream outputfile takes some time since it's a bit high-level function.
While putting outputfile -related function into
if (buttonState == 1 && pastButtonState == 0) {...}
works fine. Any ideas?
I would a add check to make sure the file was opened successfully. Also, the results of executing less on the file is not a reliable method to test the contents of the file while you are running the program. usleep(100) does not give you enough time to check the contents of the file before it is opened again.
I suggest the following change to your code.
while(buttonCounter < 100){
if (buttonState == 1 && pastButtonState == 0) buttonCounter++;
writeToFile(buttonCounter, FILE_PATH);
showContentsOfFile(FILE_PATH);
`
pastButtonState = buttonState;
}
where writeToFile is:
void writeToFile(int buttonState, std::string const& file)
{
std::ofstream outputfile(file);
if ( outputfile )
{
outputfile << buttonCounter << std::endl;
}
else
{
std::cerr << "Unable to open " << file << " for writing to.\n";
}
}
and showContentsOfFile is:
void showContentsOfFile(std::string const& file)
{
std::ifstream inputfile(file);
if ( outputfile )
{
int buttonCounter;
if ( inputfile >> buttonCounter )
{
std::cout << "Button counter: " << buttonCounter << std::endl;
}
else
{
std::cerr << "Unable to read buttonCounter from file " << file << std::endl;
}
}
else
{
std::cerr << "Unable to open " << file << " for reading from.\n";
}
}

Creating a class with an open file stream

I've created a class which is supposed to read in DNA sequences: It contains an if stream private member:
Interface:
class Sequence_stream {
const char* FileName;
std::ifstream FileStream;
std::string FileFormat;
public:
Sequence_stream(const char* Filename, std::string Format);
NucleotideSequence get();
};
Implementation:
Sequence_stream::Sequence_stream(const char* Filename, std::string Format)
{
FileName = Filename;
FileStream.open(FileName);
FileFormat = Format;
std::cout << "Filestream is open: " << FileStream.is_open() << std::endl;
}
NucleotideSequence Sequence_stream::get()
{
if (FileStream.is_open())
{
char currentchar;
int basepos = 0;
std::string name;
std::vector<Nucleotide> sequence;
currentchar = FileStream.get();
if (currentchar == '>' && false == FileStream.eof()) { // Check that the start of the first line is the fasta head character.
currentchar = FileStream.get(); // Proceed to get the full name of the sequence. Get characters until the newline character.
while(currentchar != '\n' && false == FileStream.eof())
{
if (true == FileStream.eof()) {
std::cout << "The file ends before we have even finished reading in the name. Returning an empty NucleotideSequence" << std::endl;
return NucleotideSequence();
}
name.append(1, currentchar);
currentchar = FileStream.get();
} // done getting names, now let's get the sequence.
currentchar = FileStream.get();
while(currentchar != '>' && false == FileStream.eof())
{
if(currentchar != '\n'){
basepos++;
sequence.push_back(Nucleotide(currentchar, basepos));
}
currentchar = FileStream.get();
}
if(currentchar == '>')
{
FileStream.unget();
}
return NucleotideSequence(name, sequence);
} else {
std::cout << "The first line of the file was not a fasta format description line beginning with '>'. Are you sure the file is of FASTA format?" << std::endl;
return NucleotideSequence();
}
} else {
std::cout << "The filestream is not open..." << std::endl;
return NucleotideSequence();
}
}
However if I test it:
int main()
{
std::cout << "Let's try and read in a sequence!" << std::endl;
std::cout << "First we'll create a stream!" << std::endl;
Sequence_stream MyDNAStream("~/Dropbox/1_20dd5.fasta", "fasta");
std::cout << "Done!" << std::endl;
std::cout << "Now let's try and get a sequence!" << endl;
NucleotideSequence firstsequence = MyDNAStream.get();
return 0;
}
I see that the if stream is not open:
Let's try and read in a sequence!
First we'll create a stream!
Filestream is open: 0
Done!
The filestream is not open...
logout
[Process completed]
Although I thought the constructor function opens the if stream. What do I need to do to correct this so as the object is created and contains an open stream? (I know I'm yet to include a destructor which will close the stream upon destruction of the object).
Thanks,
Ben.
Your example shows that is_open returned false. I think you should check in your constructor that the file is indeed open, and throw if not.
In your case, I suspect this is due to passing "~/Dropbox/1_20dd5.fasta" as an input parameter. Did you test with a full pathname, with no ~? I have no knowledge of a C++ library that handles real path expansion (like python's os.path).

Expecting eof but instead arbitrary data near eof

I have sought to discover the cause of unwanted trailing end-data in a file I am writing specific data into and do not believe I have made errors in writing to the file.
The output looks like:
building room_numbr capacity
packard | 101 | 500 |
painter | 514 | 10 |
ÿÿÿÿÿÿÿÿÿÿ | Attempt to seek file pointer error
The Attempt to seek file pointer error is normal as it represents a thrown exception when attempting to move the file pointer on an invalid stream. However, the ÿÿÿÿÿÿÿÿÿÿ is not normal nor expected in a fixed size file format using either 10 or 20 bytes to write data.
Create file here:
BinarySearchFile::BinarySearchFile(std::string file_name){
// concatenate extension to fileName
file_name += ".dat";
// form complete table data filename
data_file_name = file_name;
// create or reopen table data file for reading and writing
binary_search_file.open(data_file_name, std::ios::out | std::ios::in | std::ios::app);
if(!binary_search_file.is_open()){
binary_search_file.clear();
binary_search_file.open(data_file_name, std::ios::out);
binary_search_file.close();
binary_search_file.open(data_file_name, std::ios::out | std::ios::in | std::ios::app);
}
try{
if(binary_search_file.fail()){
throw CustomException("Unspecified table data file error");
}
}
catch (CustomException &custom_exception){ // Using custom exception class
std::cout << custom_exception.what() << std::endl;
return;
}
}
Write data to file
void BinarySearchFile::writeT(std::string attribute){
try{
if(binary_search_file){
for(auto start = attribute.begin(); start != attribute.end(); ++start){
binary_search_file.put(' ');
binary_search_file.put(*start);
}
binary_search_file.flush();
/*
attribute.resize(attribute.length() * 2);
const char *write_this = attribute.data();
binary_search_file.write(write_this, attribute.length());
*/
}else if(binary_search_file.fail()){
throw CustomException("Attempt to write attribute error");
}
}
catch(CustomException &custom_exception){ // Using custom exception class
std::cout << custom_exception.what() << std::endl;
return;
}
}
Read data file here:
std::string BinarySearchFile::readT(long file_pointer_location, long size_of_data)
{
try{
if(binary_search_file){
std::string data = "";
binary_search_file.seekp(file_pointer_location);
binary_search_file.seekg(file_pointer_location);
while (size_of_data > 0 ){
binary_search_file.get();
data += binary_search_file.get();
size_of_data -= 2;
}
/*
char data[20];
binary_search_file.seekp(filePointerLocation);
binary_search_file.seekg(filePointerLocation);
binary_search_file.read(data, sizeOfData);
*/
return data;
}else if(binary_search_file.fail()){
throw CustomException("Attempt to read attribute error");
}
}
catch(CustomException &custom_exception){ // Using custom exception class
std::cout << custom_exception.what() << std::endl;
}
}
Code that reads the file and prints the result to the screen:
while(true){
//reinitialize the catalog pointer to the beginning
catalog->setPointerBegin();
//display data
do{
if (boost::iequals((domain = catalog->getAttributeDomain()), "string")){
if(dataFile->binary_search_file_status()){
std::cout << dataFile->read_data(filePointer, 20) << " | ";
if (!writer_.fail())
writer_ << dataFile->read_data(filePointer, 20) << " | ";
}
else{
std::cout << "\n";
if (!writer_.fail())
writer_ << "\n";
return true;
}
// update the file pointer
filePointer += 20;
dataFile->set_file_pointer(filePointer);
}
else{
if(dataFile->binary_search_file_status()){
std::cout << dataFile->read_data(filePointer, 10);
if (!writer_.fail())
writer_ << dataFile->read_data(filePointer, 10);
for(int i = 0; i < 5; i++){
std::cout << " ";
if (!writer_.fail())
writer_ << " ";
}
std::cout << " | ";
if (!writer_.fail()){
writer_ << " | ";
}
}
else{
std::cout << "\n";
if (!writer_.fail()){
writer_ << "\n";
}
return true;
}
// update the file pointer
filePointer += 10;
}
} while(catalog->traverseForward() != nullptr);
std::cout << "\n";
if (!writer_.fail())
writer_ << "\n";
}
}
std::ifstream::get returns std::char_traits<char>::eof on failure, which usually has the int value -1. If you interpret that blindly as a valid character and cast to char, you will get '\xff', which in ISO-8859-15 is ÿ.
You should be checking for eof and/or eofbit when you read characters from the file, and especially after seeking.

c++ get one line data in txt file

I want to read data from a txt file, but i am not able to get it. I am new to c++.
Here is my code, but it does not work. I used getline(),
ifstream inFile;
string sPassWord;
inFile.open("QdatPassWordconfig.config");
inFile.seekg(0,ios::end);
int length=inFile.tellg();
if (inFile.is_open())
{
while (!inFile.eof())
{
getline(inFile,sPassWord);
cout<<sPassWord<<endl;
}
cout<<"get data from txt file"<<endl;
// here ,I cannot read data from file
cout<<sPassWord<<endl;
}
if(!inFile.is_open() || length==0)
{
cout<<"file is create or write"<<endl;
sPassWord="BdsWUjT26";
ofstream outFile;
outFile.open("QdatPassWordconfig.config");
outFile<<sPassWord<<endl;
outFile.close();
}
inFile.close();
cout<<sPassWord<<endl;
It isn't clear if you are trying to read the first line of the file, the last line of the file, or all the lines of the file. Here program snippets for each possibility:
To read the first line of the file:
// UNTESTED
{
ifstream inFile("QdatPassWordconfig.config");
string sPassWord;
if(std::getline(inFile, sPassWord)) {
std::cout << "Password is: " << sPassWord << "\n";
} else {
std::cout << "No password available.\n"
}
}
To read all of the lines of the file:
// TESTED
#include <iostream>
#include <fstream>
#include <string>
int main ()
{
std::ifstream inFile("QdatPassWordconfig.config");
std::string sPassWord;
while(std::getline(inFile, sPassWord)) {
std::cout << "Password is: " << sPassWord << "\n";
}
}
To read the last line of the file:
// UNTESTED
{
ifstream inFile("QdatPassWordconfig.config");
string sPassWord;
int lineCount = 0;
while(std::getline(inFile, sPassWord)) {
lineCount++;
}
if(lineCount) {
std::cout << "Password is: " << sPassWord << "\n";
} else {
std::cout << "No password available.\n";
}
}
inFile.seekg(0,ios::end);
int length=inFile.tellg();
1.You forgot seek back to the beginning. Like this:
inFile.seekg(0,ios::end);
int length=inFile.tellg();
inFile.seekg(0,ios::beg);
2.You need to practice on your if and else statement.
3.Don't use std::ifstream::eof. Use std::getline.
Do something like this:
// Declare local variables
std::ifstream inFile;
std::string sPassword = "";
::UINT length = 0;
// Attempt to open file
inFile.open( "QdatPassWordconfig.config" );
// Use your if and else statement like this:
// Is inFile open?
if( inFile.is_open( ) )
{
// Read file line by line using std::getline
while( std::getline( inFile, sPassword ) ) {
// Print sPassword
std::cout << sPassword << std::endl;
}
// Done with inFile, close it
inFile.close( );
}
else
{
// Do whatever if inFile can't be open
}
There are so many errors with your code, so I decided to show you how I would have done it (please do read the comments):
void Example( void )
{
// DECLARATION
bool bInputMode = true;
std::fstream ioFile;
::UINT nFileSize = 0;
std::string strPassword = "";
// INITIALIZATION
// *Open or create ioFile
// ioFile can now do both input and output operations
ioFile.open( "Passwords.pw",
std::fstream::in |std::fstream::out | std::fstream::app );
// *Calculate/set the value of bInputMode
// first, calculate the size of the file
// if the size of the file is = 0,
// bInputMode = false - which means to be in output mode
ioFile.seekg( 0, std::ios::end );
if( ( nFileSize = ioFile.tellg( ) ) = 0 )
bInputMode = false;
ioFile.seekg( 0, std::ios::beg );
// DO WHATEVER
// *Since bInputMode == true,
// we shall read each line from ioFile by using std::getline
if( bInputMode )
{
// *Get each line within ioFile and "copy" it to strPassword
// and then print strPassword
// *With std::getline, we could get the spaces
while( std::getline( ioFile, strPassword ) )
std::cout << strPassword << std::endl;
}
// *Since bInputMode == false,
// we shall create a new from ioFile and then write to it
else
{
std::cout << "Creating/writing a new file..." << std::endl;
strPassword = "Password123";
ioFile << strPassword << std::endl;
}
// CLEAN-UP
// We are done with ioFile, close it.
ioFile.close( );
};
Please point out any errors! Some feedback and suggestions would be great as well.