I'm trying to read some values from a config file in c++ and getline doesn't seem to be working.
The relevant portion of the code looks like this:
#include <iostream>
#include <fstream>
#include <unistd.h>
using namespace std;
// ...
ifstream config( configPath );
string line;
Message( DiagVerbose, "CONFIG: %s\n", configPath.c_str());
bool exists = ( access( configPath.c_str(), F_OK && R_OK ) != -1 );
Message( DiagVerbose, "Exists?: %s\n", exists ? "true" : "false");
// This was causing a fail bit to set
//config.open( configPath );
if (config.is_open())
{
Message( DiagVerbose, "Config is open%s\n", config.good() ? "good" : "bad" );
while ( getline( config, line ) )
{
Message( DiagVerbose, "Line: %s", line.c_str());
extension_t extension = parseConfig( line );
extensions[ extension.name ] = extension.type;
}
config.close();
} else {
FatalError( "Could not open file %s\n", configPath.c_str());
}
The Message function is just a wrapper for printf and prints the following:
CONFIG: ./tool.conf
Exists?: true
Config is open: good
74181 Segmentation Fault: 11
But everything in the while loop is skipped. Also the file I'm reading from does actually have data in it.
Why is getline not getting the line even though the file exists and is readable?
UPDATE
I changed the code above to reflect the changes suggested by #rici however, now I'm facing a Segmentation Fault at the same line before anything in the while loop is called.
You're opening config twice (once in the constructor, and again using an explicit open). The second open is an error, which causes the failbit to be set (even though the ifstream is still open.) From then on, all I/O operations to the ifstream will fail.
You opening same file twice
Remove the
config.open( configPath );
before
if (config.is_open())
Related
I've recently switched a project from using the VS2013 compiler to VS2017, and now the following code appears not to be working:
#include <windows.h>
#include <Fcntl.h>
#include <io.h>
#include <iostream>
#include <exception>
if( RedirectOutput )
{
//Setup stdout
HANDLE handle_stdout = GetStdHandle( STD_OUTPUT_HANDLE );
int fileDesc_stdout = _open_osfhandle( (long)handle_stdout, _O_TEXT );
if( fileDesc_stdout == -1 )
{
throw std::exception( "fileDesc_stdout is not valid" );
}
FILE* new_stdout = _fdopen( fileDesc_stdout, "w" );
if( !new_stdout )
{
throw std::exception( "new_stdout is not valid" );
}
FILE old_stdout = *stdout;
*stdout = *new_stdout;
std::cout.clear();
std::cout << "Output Redirected!\n";
}
This code is intended to redirect standard output to a console window, either the one that kicked off the current process or a console that is created via AllocConsole. (I've added the last line for testing purposes.)
The first time writing to cout occurs, the following exception is thrown (it otherwise doesn't write output and fails silently from then on):
Debug Assertion Failed!
Program: w:\build\MyApp.exe
File: minkernel\crts\ucrt\src\appcrt\stdio_flsbuf.cpp
Line: 26
Expression: ("inconsistent IOB fields", stream->_ptr - stream->_base >= 0)
For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.
(Press Retry to debug the application)
I can see that stdout is coming from corecrt_wstdio.h, but when I insert a breakpoint and add a watch, it says that the stdout is undefined, so I can't check the value.
Any ideas?
So I searched around a bit and found this post on SO.
Essentially, all of the code I posted can be replaced with the following:
freopen("CONOUT$", "w", stdout);
The first parameter of freopen is a filename, so I wonder if CONOUT$/CONIN$ represent an actual file, or if the function treats that input as a special case.
I know that when a program is executed in the console, one can specify an output file where the console can be saved. For instance, in Windows:
C:\>myprogram.exe > output.txt
However, is there a way to establish via code, i.e. programatically: 1) whether the console should be saved to a file; and 2) the name of the file to which the output should be saved, if any.
I know that I can of course use fprint or std::cout to print each string to a file the same I can do to the console. But for performance sake, I would like to know if it is possible to establish via code that the entire console should be saved to a file.
Yes, you can write code like this:
int main( int argc, char * argv[] ) {
if ( argc > 1 ) {
// there is a filename on the command line
ofstream ofs( argv[1] ); // open named file
// do something with ofs
}
else {
// do something with standard output
}
}
You can use dup2 function (in windows _dup2). It can solve the problem to log to the console exclusively or log to the file exclusively. This is not a solution to log to both.
You can use some logging library (log4cxx, log4cpp, Boost.Log, QDebug, etc.) They should have abilities you need - e.g. log to both console and file.
Solution with dup2/_dup2:
You can open new file, then call dup2 to exchange stdout with that opened file. It can work with c++ streams but I did not try it.
Relevant part of Microsoft example (all checks removed, pleas se original example. I don't have Windows so I can't verify it.)
#include <stdlib.h>
#include <stdio.h>
#include <io.h>
int main(int argc, char ** argv) {
FILE *DataFile;
fopen_s( &DataFile, "data", "w" ); // open file "data" for writing
_dup2( _fileno( DataFile ), 1 ); // exchange "standard output/console" with file "data"
printf("this goes to 'data' file'\r\n"); // print to standard output, but it will be saved to "data" file
fflush( stdout );
fclose( DataFile );
}
Complete linux verified and working C++ example
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <iostream>
int main(int argc, char ** argv) {
FILE *DataFile;
DataFile = fopen( "data", "w" ); // open file "data" for writing
dup2( fileno( DataFile ), 1 ); // exchange "standard output/console" with file "data"
std::cout << "this goes to 'data' file from c++" << std::endl;
fflush( stdout );
fclose( DataFile );
}
Some Background
Originally made a project on mac, now I want to use my Linux machine for the same project. The settings folder relied on:
#include <QJsonObject>
#include <QJsonDocument>
#include <QJsonArray>
#include <QJsonParseError>
These weren't included in my qt-devel install for SL5 - it uses QT v4. So I downloaded QJson from Sourceforge and installed using cmake. Some example output:
--Installing: /usr/include/qjson/parser.h
--Installing /usr/lib/libqjson.so
That's fine. So I added to my .pro:
LIBS += -L/usr/lib/ -lqjson
INCLUDEPATH += /usr/include/qjson/
The Actual Problem
Now I have the task of translating my old settings.cpp file to parse data in this slightly new method.
{
"HwDescription": {
"ConnectionsName": "file://settings/connections.xml",
"ShelveId": 0,
"BeBoard": {
"Id": 10,
"connectionId": "board0",
"boardType": "GLIB"
}, // + more boring stuff
So now I have this json above in a QString, just as I did for my old working method, then I try to parse as per the instructions given to me. I used: #include <qjson/parser.h> and I don't think I need any forward declarations here.
QJson::Parser parser;
bool ok;
QVariantMap result = parser.parse (raw_json, &ok).toMap(); //where raw_json is a QString - this is where I get an error
if (!ok)
{
qFatal("An error occured during parsing");
exit (1);
}
The error I get:
error: no matching function to call to 'Qjson::Parser:parse(QString&, bool)
If I remove the include, the error says:
QJson has not been declared
So it must be finding the libraries at least. Any ideas on what's going wrong?
Comments aren't parsed by default in QJson.
Here is a small adaptation I made on my QJson logic to handle comments. Notice the simple Regex to remove comments.
QFile file( filename );
//File can't be opened!
if ( !file.open( QFile::ReadOnly ) )
{
qDebug("Couldn't load config file: %s", filename.toLatin1().data());
return;
}
//Strip out comments
QStringList list = QString( file.readAll() ).split('\n');
for ( int i = 0; i < list.size(); i++ )
list[i].replace( QRegExp("//[^\"]*$"), "" );
//Load the file, converting into an object file
QJsonParseError e;
QJsonObject json =
QJsonDocument::fromJson( list.join('\n').toLatin1(), &e ).object();
//Was there an error?
if ( e.error != QJsonParseError::NoError )
{
qDebug( "Json parse error: %s", e.errorString().toLatin1().data() );
return;
}
I'm having some trouble writing a Linux console app which reads apache logs.
I need to handle bash script arguments, the last one being a path to the log file.
My problem is that if the file doesn't exist, I would like to throw an exception.
But when I try to open the file in read-only mode, instead of failing it creates the file !
Here's the code :
// logreader.h
#include <string>
#include <sstream>
#include <iostream>
#include <fstream>
#include <stdexcept>
class LogReader
{
public:
LogReader(int, const char **);
virtual ~LogReader();
// ...
private:
std::ifstream log_;
};
// logreader.cpp
#include <logreader.h>
LogReader::LogReader(int argc, const char ** argv):
log_()
{
log_.exceptions(std::ifstream::failbit | std::ifstream::badbit);
for (int i = 1; i < argc; ++i)
{
std::string arg(argv[i]);
if (i == argc - 1)
{
try
{
log_.open(arg.c_str(), std::ifstream::in);
}
catch (std::ifstream::failure)
{
throw std::runtime_error("The file " + arg + " wasn't opened");
}
}
}
}
LogReader::~LogReader()
{
}
// main.cpp
#include <logreader.h>
int main(int argc, const char ** argv)
{
LogReader(argc, argv);
return 0;
}
Script call:
jmcomets $ ./test -g -l
jmcomets $ ls -l
-rw-rw-r-- 1 jmcomets jmcomets 0 Nov 14 22:41 -l
Since you are opening an std::ifstream it is necessary to add std::ios_base::in (or any other spelling of the std::ios_base::openmode) according to 27.9.1.9 [ifstream.members] paragraph 4: The flag is automatically added by the call to open(). Note that an std::ofstream or an std::fstream would automatically add std::ios_base::out (27.9.1.13 [ofstream.members] paragrpah 3) or std::ios_base::in | std::ios_base::out (27.9.1.17 [fstream.members] paragraph 3), both of which resulting in a new file being created if it doesn't exist (and there are write permissions, etc.).
If the code you posted creates a new file, the implementation of the standard C++ library is wrong: when only the flag std::ios_base::in is specified, the file is open "as if" using the open mode "r" with fopen() (27.9.1.4 [filebuf.members] paragraph 5). fopen() in turn doesn't create a new file when it gets an open mode of "r" (7.21.5.3 paragraph 3).
You can set the failbit in the exceptions flag for the ifstream:
std::ifstream log;
log.exceptions ( std::ifstream::failbit );
try {
log.open ("test.txt");
}
catch (std::ifstream::failure e) {
std::cout << "Exception opening/reading file\n";
}
Source
I've tested, and ifstream will throw a failure exception if the file cannot be opened, e.g. file not found, no read permissions. It will open read-only.
You need to specify ifstream::in as a second parameter as:
log.open(arg.c_str(), ifstream::in)
You can also do:
std::ifstream log(arg.c_str(), ifstream::in);
and skip the call to open()
Edit with something Linux compatible;
Try opening with fopen before writing. If the file DNE the FILE pointer will be null.
FILE * file;
file = fopen ("myfile.txt","r");
if (file == NULL)
//throw if fopen didn't already.
else
//do stuff with my file
I'm trying to load up a Comma Separated file called POSDATA.GAMEDATA. I've looked up several places on the internet and it turns out I need to do some tweaking and / or a different class.
I tried using ifstream. However, it cannot open the file. Xcode 4.3.2 cannot seem to find my POSDATA.GAMEDATA file. I also tried to make the file using ofstream but when I use open() in both cases, the file is not opened.
My code is something like this:
using namespace std;
void FileLoader::loadFile( string p_WhichFile ) {
// Local Variables
string thisLine;
// Open POSDATA.GAMEDATA
ifstream dataStream;
dataStream.open( p_WhichFile.c_str( ) );
// Check if file is opened
if ( !dataStream ) {
cerr << "[ ERROR ] Cannot load file:" << p_WhichFile.c_str( ) << endl;
exit( 1 );
}
// Get lines of strings
while ( getline( dataStream, thisLine ) ) {
fileContents.push_back( thisLine ); // fileContents is a vector< string > object
}
dataStream.close( );
cout << "[ NOTICE ] Finished reading file" << p_WhichFile << endl;
}
I've seen CCFileUtils but I can't seem to get how to use it.
EDIT: I've tried supplying the absolute path ( /Users/LanceGray/Documents/LanceDev/COCOS2DX/cocos2dx/TestGame/Data/POSDATA.GAMEDATA ) and it worked. However, I cannot do this since the game is supposed to be used in iOS devices and Android, so the path is not always the same on each device. Any help will be grealy appreciated.
I got working by using CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( "POSDATA.GAMEDATA" );
A more detailed explanation:
Add the files that you need in the Project by going to the Project Tree on the left and Right-click -> Add Files. What I did was I added a new folder called Data on the same level as the Resources and Classes folders and placed my POSDATA.GAMEDATA file there. In Xcode, I added a new group and added that file in that group.
Then I used ifstream to open the file.
When opening the file, use CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( ) to get the absolute path of the file. Supply the file name on the fullPathFromRelativePath( ) as argument.
Try to run it and it should work fine.
A small example:
// FileReader.h
#include "cocos2d.h"
using namespace std;
using namespace cocos2d;
class FileReader {
private:
vector< string > mFileContents;
public:
FileReader( string pFileName, char pMode = 'r' );
};
// FileReader.cpp
#include "FileReader.h"
#include <fstream>
#include "cocos2d.h"
using namespace cocos2d;
using namespace std;
FileReader::FileReader( string pFileName, char pMode ) {
// Create input file stream
ifstream inputStream;
string thisLine;
// Open file
inputStream.open( CCFileUtils( )::sharedFileUtils( ) -> fullPathFromRelativePath( pFileName ).c_str( ) );
// Check if it is open
if ( !inputStream.is_open( ) ) {
cerr << "[ ERROR ] Cannot open file: " << pFileName.c_str( ) << endl;
exit( 1 );
}
while ( getline( inputStream, thisLine ) ) {
// Put all lines in vector
mFileContents.push_back( thisLine );
}
inputStream.close( );
cout << "[ NOTICE ] Finished opening file: " << pFileName.c_str( ) << endl;
}
This class will load a file with the name pFileName and place it on its member variable mFileContents. ( Note that it should have a public get function like vector< string > getFileContents( ) to access the mFileContents because it is private )
EDIT: The above sample will work on iOS, however, it won't on Android devices. So to fix this, instead of using ifstream, use CCFileUtils::sharedUtils( ) -> getFileData( ) instead. In conjunction with CCFileUtils::sharedUtils( ) -> fullPathFromRelativePath( ), we will be able to achieve our goal of reading a plain text file that works on both iOS and Android.
The FileReader class would then be like this:
// FileReader.cpp
#include "FileReader.h"
#include <fstream>
#include "cocos2d.h"
using namespace cocos2d;
using namespace std;
FileReader::FileReader( string pFileName, char pMode ) {
// Initialize variables needed
unsigned long fileSize = 0;
unsigned char * fileContents = NULL;
string thisLine, result, fullPath, contents;
// Get absolute path of file
fullPath = CCFileUtils::sharedFileUtils( ) -> fullPathFromRelativePath( pFileName.c_str( ) );
// Get data of file
fileContents = CCFileUtils::sharedFileUtils( ) -> getFileData( fullPath.c_str( ) , "r", &fileSize );
contents.append( ( char * ) fileContents );
// Create a string stream so that we can use getline( ) on it
istringstream fileStringStream( contents );
// Get file contents line by line
while ( getline( fileStringStream, thisLine ) ) {
// Put all lines in vector
mFileContents.push_back( thisLine );
}
// After this, mFileContents will have an extra entry and will have the value '\x04'.
// We should remove this by popping it out the vector.
mFileContents.pop_back( );
// Delete buffer created by fileContents. This part is required.
if ( fileContents ) {
delete[ ] fileContents;
fileContents = NULL;
}
// For testing purposes
cout << "[ NOTICE ] Finished opening file: " << pFileName.c_str( ) << endl;
}
// For versions less than v2.0.1
// The version I am using is 0.12.0
unsigned long fileSize = 0;
char* pBuffer = CCFileUltils::getFileData("relative_path","r",&fileSize);
CCLOG("Data is %s",pBuffer);
You can reference from Cocos's wiki
Read/write file in cocos2d