I have a simple logger class which I tried to turn into accepting and outputting wstrings instead strings.
header:
#include <fstream>
using namespace std;
class CLog
{
public:
CLog(wstring filename);
~CLog();
void WriteString(string uString);
private:
fstream m_stream;
};
cpp:
#include "stdafx.h";
#include "log.h";
CLog::CLog(wstring uPath)
{
m_stream.open(uPath);
}
void CLog::WriteString(string uString)
{
m_stream << uString.c_str() << endl;
}
CLog::~CLog()
{
m_stream.close();
}
Can anybody suggest what I should use instead of fstream?
I tried using wstringstream, but it did not even have .open to output it to file, so I thought that is the wrong approach.
I would like to keep the behaviour that it immediately writes to a file.
I use "wofstream" instead of "fstream" now, and it works perfectly.
Related
I was trying to open file and read it using C++. Here's the code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void open_file(string filename)
{
string data;
ifstream file;
file.open(filename);
file >> data;
cout << data;
}
int main()
{
open_file("file.txt");
}
But the output ends when character in file is space.
File:
This message will be printed
Output:
This
Could somebody help me?
Yes. That is the standard behaviour of the extractor.
You should use std::getline to read a complete line. This will read until the end of a line (denoted by '\n').
So:
std::getline(file, data);
Please see here for more information.
Use std::getline()
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void open_file(string filename)
{
string data;
ifstream file;
file.open(filename);
getline(file,data); //This will read until the end of a line
cout << data;
}
int main()
{
open_file("file.txt");
}
Don't use using namespace std , refer to this for more info Why is using namespace std bad?
For more information i recommend getting used to This site
I am trying to learn C++, however, the parameter to a method I have in my own class is misbehaving. When it uses a dataType of 'int', it works fine with no errors, but when I attempt to change it to a 'string' dataType, the program crashes with this error.
Error 1 error C2061: syntax error : identifier 'string' in temp.h ln
8 col 1
The classes I am using are as follows:
WORKING CODE
TesterClass.cpp // Entry Point
#include "stdafx.h"
#include "Temp.h"
int _tmain(int argc, _TCHAR* argv[])
{
Temp tmp;
tmp.doSomething(7);
return 0;
}
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(int blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(int blah)
{
std::cout << blah;
}
BROKEN CODE
Temp.h
#pragma once
class Temp
{
public:
Temp();
void doSomething(string blah);
};
Temp.cpp
#include "stdafx.h"
#include "Temp.h"
#include <iostream>
#include <string>
using std::string;
Temp::Temp()
{
std::cout << "Entry" << std::endl;
string hi;
std::cin >> hi;
std::cout << hi << std::endl;
}
void Temp::doSomething(string blah)
{
std::cout << blah;
}
When I adjust the parameter 'blah' to be a string, in both the .h and .cpp file, the problem occurs.
I have looked around, but none of the answers seem to solve my problem. I would greatly love help on this an I am out of ideas. I have tried reinstalling C++, messing with:
using namepace std;
using std::string;
std::string instead of string
etc.
If you know how to solve my problem I would love to hear from you. I am more than happy to provide more information.
C++ performs single-pass compilation, so std::string needs to be declared before you use it at all - including in the header file.
// Temp.h
#pragma once
#include <string>
class Temp
{
public:
Temp();
void doSomething(std::string blah);
};
I would encourage you to be specific in your header files when specifying classes like this, because you might easily come across another library that defines it's own string and then you would run into naming conflicts. Save the using import statements for your cpp files.
πάντα ῥεῖ had the write answer, thankyou!
They said to use std::string when needed, and to also #include <string> in the header file.
For my assignment, it says that I am to use the command line argument ./a.out user1.txt (the text file name can change) in my main.cpp.
I have the following in my main.cpp
int main(int argc, char* argv[]){
string name;
name = argv[1];
}
but don't know how I can get name into my BBoard setup function in BBoard cpp
#include "BBoard.h"
#include <fstream>
#include <algorithm>
using namespace std;
User user_l;
BBoard::BBoard(){
title = "Default BBoard";
vector<User> user_list;
User current_user;
vector<Message> message_list;
}
BBoard::BBoard(const string &ttl){
title = ttl;
}
void BBoard::setup(const string &input_file){
ifstream fin;;
fin.open(input_file);
while(!fin.eof()){
user_list.push_back(user_l);
}
}
with BBoard header here
#ifndef BBOARD_H
#define BBOARD_H
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class User
{
public:
User() { }
User(const std::string& _name, const std::string& _pass)
: name(_name), pass(_pass)
{ }
friend bool operator==(const User& lhs, const User& rhs)
{
return (lhs.name == rhs.name) &&
(lhs.pass == rhs.pass);
}
private:
std::string name;
std::string pass;
};
class Message{
};
class BBoard{
private:
string title;
vector<User> user_list;
User current_user;
vector<Message> message_list;
public:
BBoard();
BBoard(const string &ttl);
};
#endif
Edit: How do I use an object in main cpp to send over name to my BBoard function? When I try including main cpp into my board cpp, I get errors.
What about creating an BBoard and then calling the setup function:
if (argc > 1) {
string name = argv[1];
BBoard board;
board.setup(name);
}
You're so close. You simply need to edit your main() function to create a BBoard object, and pass the name to it the same way you pass argv[1] to std::string. You can then call functions on that object, or pass that object to other functions.
Style advice:
What should happen if somebody forgets to pass the filename to the program? As it stands, you crash. It's pretty easy to tell the user what's wrong and bail if argc is only 1:
if (argc == 1) {
cout << "usage: a.out file.txt\n";
return 1;
}
Not all programmers use using namespace std. There's nothing wrong with doing so in .cpp files, but I personally get upset when #include-ing a header file has the effect of calling using namespace XXX for me without my consent. As it is, your header file already fully qualifies things in the std namespace, so you can remove that line from your header without needing to make other changes. To keep me from getting upset when I use your header, you simply need to remove using namespace std from the header and use std::vector instead of simply vector.
I'm trying to write a test function for print. Everything compiles fine but why does it not print? what am i doing wrong? can someone please help me out? thanks
oneLine.cpp
#include "oneLine.h"
#include <iostream>
OneLine::OneLine() {
cout << "test";
}
OneLine::~OneLine() {
cout << "~test";
}
oneLine.h
#include <string>
using namespace std;
class OneLine {
OneLine();
~OneLine();
void breakLine();
void printReverse();
istream &readLine(istream& is);
string returnLine();
private:
string oneLine;
char **words;
int wordCount;
void resetLine();
};
main.cpp
#include "oneLine.h"
using namespace std;
int main () {
OneLine oLine();
return 0;
}
This is a function declaration:
OneLine oLine(); // declaration of a function returning a OneLine object
To default construct a OneLine object, you need
OneLine oLine;
or, in C++11, you can also use {}:
OneLine oLine{};
Next, as #POW points out in the comments, your default constructor and destructor have to be made public. Currently they are private.
As an aside, note that using namespace std is considered bad practice, especially in header files.
I have declared a class and instantiated a class in one and expected it to fire
~CLog();
But for some reason, it does not. Does anybody see any obvious errors why this could happen?
I declared the class within a void that ends, so it SHOULD fire, I think.
I do not destroy the class explicitely, but I simply expected it to go out of out scope automatically and terminate.
#pragma once
#include <fstream>
using namespace std;
class CLog
{
public:
CLog(wstring filename);
~CLog();
void WriteString(wstring uString);
private:
wofstream m_stream;
wstring m_sPath;
};
#include "log.h";
#include "strhelper.h"
#include <fstream>
#include <iostream>
#include <string>
#include <locale>
#include <codecvt>
wstring m_sText=L"";
wstring m_sPath=L"";
CLog::CLog(wstring uPath)
{
m_sPath=uPath;
}
void CLog::WriteString(wstring uString)
{
m_sText+=uString;
m_sText+=L"\n";
}
CLog::~CLog()
{
if (FileExists(m_sPath))
{
DeleteFile(m_sPath);
}
//open for appending
m_stream.imbue(std::locale(std::locale::empty(), new std::codecvt_utf8<wchar_t,0x10ffff,std::generate_header>));
m_stream.open(m_sPath,fstream::in | fstream::out | fstream::app);
m_stream << m_sText.c_str();
m_stream.close();
}
I am using Clog like this
void foo() {
wstring sLogPath;
sLogPath=GetSpecialFolderDesktop() + L"\\load.log";
CLog *pLog = new CLog(sLogPath);
pLog->WriteString(L"Something);
}
I am using VC2010.
You are instantiating dynamically the CLog. In that case, you need to delete it explicitly.
If you create it on the stack Clog log(sLogPath), the destructor will be called when the object goes out of scope.