How to implement file reading and creating into a structure [closed] - c++

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I don’t know how to implement reading data and put it into the structure
There is a config.txt file, it stores data like
someBigText=ASDSDdasdsa (can be more +1000 simbol)
isOkay=true
myAge=24
struct Config {
std::string Name;
std::string StringValue;
};

assuming your config is splitted in sections like:
[myImportantSection]
someBigText = "foo tha can be more +1000 simbols"
isOkay = true
myAge = 24
and assuming you are using boost:
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/ini_parser.hpp>
boost::property_tree::ptree myPtree;
boost::property_tree::ini_parser::read_ini("config.txt", myPtree);
auto text{myPtree.get<std::string>("myImportantSection.someBigText")};
auto isOk{myPtree.get<bool>("myImportantSection.isOkay")};
auto age{myPtree.get<int>("myImportantSection.myAge")};
struct Config
{
std::string text{};
bool ok{false};
int age{0};
};
Config myConfig;
myConfig.text = text;
myConfig.ok = isOk;
myConfig.age = age;

Related

Passing data between variables inside a class in C++ [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I don't fully understand nor did i find anything online about this topic but this code(cut down so it wont be 500 lines of code):
class Character{
public:
int MaxHP;
int currentHP;
currentHP = MaxHP;
int getHP()
{
return CurrentHP;
}
Character(int h){
maxHP=h
};
~Character(){};
};
int main()
{
Character warrior(300)
cout<<getHP();
return 0;
};
maxHP = 300
CurrentHP = -875000
now trying to acces the value of currentHP i get -87878....
My question is what is the problem?
If you wish to initialize a private variable it's better done in a constructor:
class Character{
private:
int MaxHP;
int currentHP;
public:
Character() : MaxHP(0), currentHP(MaxHP){}
}

C++ single-function variable placement [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 3 years ago.
Improve this question
I'm writing a class that reads data from a file. The project is still in development, and it's likely that I'll change the file name or path later on, so I've stored it in a std::string for quicker editing.
Given that the file name is going to be used several times in a function, but is only going to be used in one function, is there a canonical cpp rule about where I should define the variable?
//don't know where I'll define this
std::string file_name = "path/to/file.foo";
//a.h file
class A {
public:
void fileFunc();
private:
//do i define it here?
};
//a.cpp file
A::fileFunc() {
//or do i define it here?
std::ifstream in(file_name);
if(in) {
//do things
}
else {
std::cerr << "couldn't open " << file_name;
}
}
Keeps all information close to thiers use.
It will help the readability and the performance. See: https://en.wikipedia.org/wiki/Locality_of_reference
So
A::fileFunc() {
const std::string file_name = "path/to/file.foo"; // pls use const when you can
...
or
A::fileFunc(const std::string& file_name) {
...
BTW, I think this should be on https://codereview.stackexchange.com/, not stackoverflow.

Load big file asynchronously using c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hi I dnt have any knowledge on Multithreading or Parallel programing.
I need to load multiple file for an application, in which the load time does not affect the application or response to user.
I used CreateThread with that I cannot able to load data to a class variable.
Any guidance of how to do this in VC++ will be a great help.
Thanks in Advance !!
For example,
My application is streaming content meantime I need to load a big image to to class variable (Bitmap), which should not affect the streaming i.e without pause.
Modern C++ allows you to use hi level abstract features such as std::future:
struct Data {
// file name just for info
std::string file_name;
// here is data from file ...
static Data load(const std::string& name) {
Data data{ name };
// todo load from file
return std::move(data);
}
};
std::vector<std::string> names = { "file1.txt", "file2.txt", "file3.txt" };
std::vector<std::future<Data>> results;
for (const auto& name : names) {
// load from the name file asynchronously
auto future = std::async(std::launch::async, &Data::load, std::ref(name));
results.emplace_back(future);
}
// gather result
for (auto& future : results) {
Data& data = future.get();
// todo use data from the file object
}

c++ using scope resolution operator in function call parameter [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
Can someone please point me to an explanation what e.g. QIODevice::WriteOnly actually does?
full line of code:
file.open(stderr, QIODevice::WriteOnly);
from that link
thanks
According to the documentation for the QIODevice class, WriteOnly is as enum constant with value 2. It indicates that the device is open for writing.
I believe that the following example for enum hack will be useful to you.
class MyClass1 {
public:
enum { SIZE=10 };
};
class MyClass2 {
public:
enum { SIZE=20 };
};
int main() {
cout << MyClass1::SIZE << "\t" << MyClass2::SIZE << endl;
}
QIODevice::WriteOnly is just a flag, you're saying that you want to open the file only for writing.
If you would want only to read the file, QIODevice::ReadOnly would be the necessary flag to use.
And to read and write use flag: QIODevice::ReadWrite:
file.open(stderr, QIODevice::ReadWrite);

PAWN to C++ global variable [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
How do i write it in c++ ??
// in PAWN ( Procedural Language )
enum e_player_data
{
id, // Integer
username[24] // string
};
new PlayerData[50][e_player_data];
/*
so i can access it like PlayerData[1][id] = 1; or PlayerData[1][username] = "Firstname_Lastname";
*/
Can someone code it to c++ for me?
You can use a std::map:
typedef std::map<int,std::string> Player;
Player p;
// add a player:
int id = 0;
p[id] = "Max Power";
As tobi303 wrote in the comment, you can use a std::map to accomplish this.
struct e_player_data
{
int id,
std::string username
};
std::map<int,e_player_data> PlayerData;
PlayerData[1].id = 1;
PlayerData[2].username = "Firstname_Lastname";