So, I'm trying to create a text file that can be written to and used to seed a random number generator, but I need to be able to write to and call on the file in all of my functions. The file has to be named with the user's first and last name so I found some code that would create a text file and rename it, I'm just having trouble calling the file in my other functions. I included the beginning part of one of my other functions after the code for the text file creation. I'm going to need to send a time stamp to the file and use that time to seed my RNG.
int user_file_name()
{
string tstamp = get_timestamp();
//Creating input/output file using user's name
ofstream user_file;
string filename;
cout << "What is your first and last name?\n" << endl;
getline(cin, filename);
filename += ".txt";
cout << "Thank you, " << filename << "." << endl << endl;
user_file.open(filename.c_str());
user_file << tstamp;
user_file.close();
return 0;
}
int addition()
{
char DIFFICULTY;
difficulty_menu();
cin >> DIFFICULTY;
get_timestamp();
string tstamp = get_timestamp();
Why not just have user_file_name return the string filename instead of just an int? You can then store that filename somewhere so that other areas of your program can access it.
Related
I'll cut the the short of it. I'm attempting to understand the filesystem library but there's very little information I've been able to find. I managed to get it to compile and understand the filesystem::path type variable really well but don't seem to understand how to get filesystem::directory_iterator to work properly. I'm not sure if I'm using it for a purpose it wasn't design for. So here is what I'm attempting to do:
I wanted to create a program that opens every text file within a specified folder. For this I need to obtain the folder name and path but I want the program to be able to obtain this information on itself and dynamically so if I add or remove textFiles it'll have the logic to function.
I'm able to create a directory_iterator variable that it manages to hold the first file with me just giving it the directory like this:
const char address[]{ "C:\\Users\\c-jr8\\ProgramBranch\\PersonalPlatform\\log extruder\\logs" };
fs::directory_iterator myIterator(address);
When testing the code in the folder I have four textFiles called "attempt 1" to "attempt 4". When reading the information on:
https://learn.microsoft.com/en-us/cpp/standard-library/directory-iterator-class?view=vs-2019#op_star
It mentions two functions to move the path object within the iterator to the next file. The first is increment(), which is the intendent method for iterating through the files, and operation++().
Now increment() hasn't been able to work for me cause it takes in a erro_code type variable and I haven't been able to find much information about how to implement this with the filesystem_errorcode variable or however it's meant to be used. The operation++() works beautifully and provides me with the path to every file but I was having issues with managing the code to detect when the operate++() functions leads to no other files. Once it iterates through every file it sorts of crashes when it keeps moving to the next. Here's that piece of code:
string tempString;
for (int i = 0; i < 5; i++) { //Here the limiting is 5 so it'll iterate onces more than the numbers of files unpurpose to see how it responses.
tempString = myIterator.operator*().path().generic_string();
ifstream tempFile(tempString);
if (!tempFile.is_open()) {
cout << "Looking at file: " << i + 1 << "; failed to open." << endl << endl;
cin.get();
return 0;
}
{
//do things with file...
}
tempFile.close();
myIterator.operator++();
}
What I want if to find a way to stop the for loop once it the iterator goes off the final file.
whichever information about how the filesystem library works it would be very much appreciated.
std::directory_iterator is a classic iterator that allows for iterating over ranges, and those are usually designated by a pair of iterators, one indicating the beginning of a sequence and another representing the past-the-end iterator.
Some iterator types, like those providing access to streams, don't have an actual end location in memory. A similar situation applies to a directory iterator. In such a case, the idiomatic approach is to use a default-constructed iterator object that will serve as an end indicator.
Having said that, you could rewrite your loop to:
for (fs::directory_iterator myIterator(address), end{}; myIterator != end; ++myIterator) {
Alternatively, you can utilize a range-based for loop:
for (auto& p : fs::directory_iterator(address)) {
tempString = p.path().generic_string();
// ...
Also, note that iterators' interface is supposed to look/behave like a pointer, hence it uses operator overloading to allow for concise syntax. So instead of:
myIterator.operator++();
you should be using:
++myIterator;
Similarly, instead of:
myIterator.operator*().path().generic_string();
juse use:
(*myIterator).path().generic_string();
or:
myIterator->path().generic_string();
You should compare myIterator with a default constructed directory_iterator to check if the last file has been processed. You can also use a much simpler form to access the operators (shown in the code below):
string tempString;
// loop until myIterator == fs::directory_iterator{}
for(size_t i = 1; myIterator != fs::directory_iterator{}; ++i) {
// access path() through the iterators operator->
tempString = myIterator->path().generic_string();
ifstream tempFile(tempString);
if(!tempFile.is_open()) {
cout << "Looking at file: " << i << "; failed to open." << endl << endl;
cin.get();
return 0;
}
{
std::cout << tempString << " opened\n";
}
// tempFile.close(); // closes automatically when it goes out of scope
// simpler form to use myIterator.operator++():
++myIterator;
}
An even simpler approach would be to use a range-based for-loop:
for(const fs::directory_entry& dirent : fs::directory_iterator(address)) {
const fs::path& path = dirent.path();
ifstream tempFile(path);
if(!tempFile) {
cout << "Looking at file: " << path << "; failed to open.\n\n";
cin.get();
return 0;
}
std::cout << path << " opened\n";
}
This is part of my code, of course I have #include fstream>, and to basically run you throught what the code should do at this point is open a file I have name "flights.csv" and in a function called readFlights count how many flights are in the file and then return the size, but when ran it returns a size of 0 when there are 5 in the file, and gives me my error file open message.
int main()
{
ifstream in("flights.csv");
if(!in.is_open()) cout << "ERROR: File open" << endl;
Flights flightsList[100];
const int SIZE = readFlights(in, flightsList);
cout << "Size: " << SIZE <<endl;
here is the function for counting the size and putting it into an array.
int readFlights(ifstream& in, Flights flightsList[100]){
//Reads the file flights.csv and returns a size used for array.
string pdStore, dmStore;
int size= 0;
while(in.good()){
getline(in, flightsList[size].fromCity,',');
getline(in, flightsList[size].toCity,',');
getline(in, pdStore,',');
flightsList[size].priceDollars = stoi(pdStore);
getline(in, dmStore,'\n');
flightsList[size].distanceMiles = stoi(dmStore);
size++;
}
return size;
}
I thought it was interesting but I don't know if this is necessary but inside code blocks the .csv part of the file in ifstream in("flights.csv"); was underlined with the red jagged lines.
I found the answer, I had no clue I had to include the file into the project folder itself...
I need to read a single line of a given file word by word.
The file is a register of exam grades by different students. In particular, for any student there is a first
line formatted as follows:
-name- -surname-
Then, there is a second line reporting the grades of every exam using the following format:
-grade 1- -grade 2- -grade 3- [...] -grade n-
I created a class Student and want to put the grades in an array of int.
I know how to read the file word by word, but I have no idea how to stop once the grades from a student are over (since I don't know how may grades any given student has beforehand).
I thought to write a while repetition statement, but I have no idea what the condition could be.
Is there a way to read a line word by word and then stop reading once the line is over?
This is what I managed to write until now:
cout << "Inserisci il nome del file da analizzare: " << endl;
cin >> _filename;
fstream myfile;
myfile.open(_filename);
if (myfile.is_open())
{
myfile >> _name >> _surname >> ; //reading name and surname
}
Read a line of text using std::getline.
Read each token from the line by using a std::istringstream.
std::string line;
if ( ! std::getline(myfile, line) )
{
// Problem reading the line.
// Deal with error.
}
else
{
// Read line successfully.
std::istringstream str(line);
std::string token;
while ( str >> token )
{
// Use token.
}
}
When the user types in a word the program is searching for a match in an array, if there is a match the program prints out the string from the array.The code below does just that. My goal is to make it so when a word matches with a a word in the array the program should print an info not only the word. I thought I can fill the array with functions, but its not working. Is it even possible ?
I am using league of legend hero names because they are a lot and I know them and it doesent take me a lot of time to think of names that way :D
The idea is if the user types in voly, the program finds voly in the array and prints out(for example) his starting life, armor, mr and so on.
I tryed a lot of thing with functions but I cant make it work.
#include <iostream>
#include <string>
using namespace std;
string voly(string holder,string heroName);
int main(){
const int numH = 10;
string holder;
string heroName;
string heroList[numH] = {"voly", "teemo", "vladimir", "morgana", "jax", "ekko", "anivia", "nunu", "ashe", "tresh" };
cout << "Enter hero name.\n" << endl;
cin >> heroName;
for (int i = 0; i < numH; i++){
holder = heroList[i];
if (heroName == holder){
cout << holder << endl;
}
}
system("PAUSE");
return 0;
}
string voly(string holder, string heroName) {
cout << "Voly is the best" << endl;
}
Try to learn about structs. You can utilize them to encapsulate all hero information in a hero struct, as follows. This serves as kind of a prototype for each hero:
struct hero {
string name;
int hp;
int mana;
float mreg;
...
void printMe() { cout << 'hp: ' << hp << endl << 'mana: ' << mana << endl << ...; }
}
Using the printMe() function of that specific hero object, you can print its values.
Then, for each hero, create a struct object and add them to the array.
hero* pointer = new hero[numH];
pointer[0] = new hero {
name: "voly",
hp: 150
};
pointer[1] = new hero {
...
};
(Try considering some import function via a .CSV file or so.)
By using your for loop, compare the name:
for (int i = 0; i < numH; i++){
if (heroName == pointer[i].name){
pointer[i]->printMe();
}
}
Try using Google to find tutorials on that. Unfortunately, I'm not quite sure about the syntax in C++, maybe someone can help me out.
Good Luck!
I have created an XML file, but when I try to read it in I get an error about a duplicate key. When I opened the XML file I noticed that nothing was nested and everything was treated as it's own individual node. I was wondering if there was a way to properly create the XML file and be able to parse through it as well. Below I have included my code for writing an XML file out, my XML output, and what I would like it to be.
XML Code:
// Open up the file so that we can write to it
cv::FileStorage fs;
fs.open(szFileName, cv::FileStorage::WRITE);
// Check if we actually created the file
if(fs.isOpened())
{
fs << "Total_Images" << (int)vImages.size();
//cv::write(fs, "Total Images", (int)vImages.size());
for(int i = 0; i < (int)vImages.size(); i++)
{
char szSize[8];
string szNode;
szNode = "image_";
itoa(i, szSize, 10);
szNode += szSize;
fs << "Node" << szNode;
fs << "Width" << vImages[i]->GetWidth();
fs << "Height" << vImages[i]->GetHeight();
fs << "Keypoint_Size" << (int)vImages[i]->GetKeyPoints().size();
for(int j = 0; j < vImages[i]->GetKeyPoints().size(); j++)
{
char szSize[256];
fs << "Angle" << vImages[i]->GetKeyPoints()[j].angle;
fs << "Class" << vImages[i]->GetKeyPoints()[j].class_id;
fs << "Octave" << vImages[i]->GetKeyPoints()[j].octave;
fs << "Pt_X" << vImages[i]->GetKeyPoints()[j].pt.x;
fs << "Pt_Y" << vImages[i]->GetKeyPoints()[j].pt.y;
fs << "Response" << vImages[i]->GetKeyPoints()[j].response;
fs << "Size" << vImages[i]->GetKeyPoints()[j].size;
}
fs << "Descriptors" << vImages[i]->GetDescriptors();
}
fs.release();
}
XML Output:
<?xml version="1.0"?>
<opencv_storage>
<Total_Images>1</Total_Images>
<Node>image_0</Node>
<Width>500</Width>
<Height>266</Height>
<Keypoint_Size>268</Keypoint_Size>
<Angle>8.7332672119140625e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.7401049804687500e+002</Pt_X>
<Pt_Y>6.5084960937500000e+001</Pt_Y>
<Response>0.</Response>
<Size>1.1386331558227539e+001</Size>
<Angle>9.3859291076660156e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.7674386596679687e+002</Pt_X>
<Pt_Y>6.4983604431152344e+001</Pt_Y>
<Response>0.</Response>
<Size>1.2262072563171387e+001</Size>
<Angle>9.8604904174804688e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.1859683990478516e+002</Pt_X>
<Pt_Y>6.6855834960937500e+001</Pt_Y>
<Response>0.</Response>
<Size>1.1112688064575195e+001</Size>
...
Expected Output
<?xml version="1.0"?>
<opencv_storage>
<Total_Images>1</Total_Images>
<Node>image_0
<Width>500</Width>
<Height>266</Height>
<Keypoint_Size>268</Keypoint_Size>
<KeyPoint>
<Angle>8.7332672119140625e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.7401049804687500e+002</Pt_X>
<Pt_Y>6.5084960937500000e+001</Pt_Y>
<Response>0.</Response>
<Size>1.1386331558227539e+001</Size>
</KeyPoint>
<KeyPoint>
<Angle>9.3859291076660156e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.7674386596679687e+002</Pt_X>
<Pt_Y>6.4983604431152344e+001</Pt_Y>
<Response>0.</Response>
<Size>1.2262072563171387e+001</Size>
</KeyPoint>
<KeyPoint>
<Angle>9.8604904174804688e+001</Angle>
<Class>0</Class>
<Octave>-1</Octave>
<Pt_X>1.1859683990478516e+002</Pt_X>
<Pt_Y>6.6855834960937500e+001</Pt_Y>
<Response>0.</Response>
<Size>1.1112688064575195e+001</Size>
</KeyPoint>
...
</Node>
Thanks
You can embedd a node inside another node.
Do it like this (using "{" and "}" ):
FileStorage fs; // Open it and check that it is opened;
fs << "SimpleData" << 1;
fs << "Structure << "{";
fs << "firstField" << 1;
fs << "secondField" << 2;
fs << "}"; // End of structure node
fs << "SimpleData2" << 2;
Actually we can see the "Duplicated key" error in opencv/modules/core/src/persistence.cpp line 711
The key seems have sth to do with the node name in your xml file: once you have many nodes with the same name like below,it will has duplicated key.And even you haven't read but just open this file,it will occur.So, just modify your node names,how about add an index "node_1"?
<opencv_storage>
<mynodes>
<node>1</node>
<node>2</node>
<node>3</node>
<node>4</node>
...
<node>10</node>
</mynodes>
You can also try to use JSON which is more fash and simple to interpret
File storage is not designed for this type of use, however you can use a XML-parser/writter, the easiest, fastest and most fashion one is Xerces-C. You can download it here
Actually, the filestorage object is made to store simple datas like calibration from one runtime to the other... From what i know there is no way to structure the nodes between each others...
Julien,
EDIT :
READ DATA FROM A MAT
for(int i=0;i<img.rows;i++) {
unsigned char* row = img.data + i * img.step;
for(int j=0;j<img.cols;j++) {
unsigned char* channB = (row + 3*j);
unsigned char* channG = (row + 3*j + 1);
unsigned char* channR = (row + 3*j + 2);
}
}
WRITE WITH XERCES-C
The "DOMPrint" example shows how to write in a document with a serializer...
However, let me tell you that i think that OpenCV is enough for your example, is it a big problem that yuor datas are in nodes next to each other ? I'll try to find an easier way, let me think of it, i'll post back here (still, you sould try Xerces-C it's great and usefull)