Newbie here. I'm trying to learn Xcode and I've written a simple code here that takes a string input from an Input file and is meant to print it out to an Output file. Easy stuff, if it wasn't that I cannot figure out why Xcode isn't letting be edit Input and Output files from within the app. Here's the code:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ifstream Input;
ofstream Output;
Input.open("Input.txt");
if(!Input)
{
cout << "Input file not found" << endl;
return 1;
}
Output.open("Output.txt");
if(!Output)
{
cout << "Output file not found" << endl;
return 3;
}
string prova;
Input >> prova;
Output << prova;
return 0;
}
To make absolutely clear what I mean the editor doesn't let me edit Input files, I've uploaded a video here, hope it helps. Thank you.
Specs: Xcode Version 11.4 -> can't update it to the last version because my Mac is too old / Mac OS Catilina Version 10.15.7
Related
I tried a example on Auto for variable initialization and STL in C++. For normal variable, type was printed using : typeid(var_name).name() to print i (integer) / d(float) / pi(pointer) which works fine.
But while working on STL,
`#include <iostream>
#include <vector>
using namespace std;
int main()
{
vector<string> st;
st.push_back("geeks");
st.push_back("for");
for (auto it = st.begin(); it != st.end(); it++)
cout << typeid(it).name() << "\n";
return 0;
}
`
which gives output like,
`N9__gnu_cxx17__normal_iteratorIPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIS6_SaIS6_EEEE
N9__gnu_cxx17__normal_iteratorIPNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESt6vectorIS6_SaIS6_EEEE`
and I am unable to understand the output logic behind it, can anyone explain why it is giving output like this? and thanks in advance
That's the "name mangled" version of the name of the type of it. typeinfo::name() is not required by the standard to return a name in human-readable format (a shortcoming IMHO) and GCC doesn't do so.
To get the actual, human-readable name, you need to call the abi::__cxa_demangle() function provided by GCC, but note that this is non-portable so if your project needs to work on different compilers you'll need to wrap it appropriately.
To learn/understand the various concepts of modern C++, I tried to write similar program like "ls -R /" which would recursively lists sub directories. To achieve this I am using the future C++ TS filesystem library so that program could be portable. So far I am able to write the below program to achieve this.
#include<filesystem>
//Other herader files
// Below typedef is for VS2013
using fspath = std::tr2::sys::path;
using dir_iterator = std::tr2::sys::directory_iterator;
using namespace std::tr2::sys;
struct directory {
std::vector<fspath> files;
std::vector<fspath> operator()(const fspath& input) {
std::cout << "Input Directory Name: " << input.string() << std::endl;
dir_iterator bgnitr(input);
dir_iterator enditr;
for (dir_iterator itr = bgnitr; itr != enditr; ++itr) {
// Only store the directory from input directory,
// otherwise display the name
fspath tmp = *itr;
if (is_directory(tmp)) {
files.push_back(tmp);
}
else {
tmp = tmp.filename();
std::cout << tmp.string() << std::endl;
}
}
return files;
}
};
int main(int argc, const char** argv) {
fspath input{argv[1]};
directory dir;
auto files = dir(input);
std::sort(std::begin(files), std::end(files));
std::for_each(std::begin(files), std::end(files), directory());
return 0;
}
The above program works fine and produce the expected result if my input directory has one level of sub-directory. I could have used the "recursive_directory_iterator", but it gives the list of all files within all directory inside the input directory.
It does not handle the scenario where actual input directory contains the sub-directory which itself contains sub-directory and files. Basically these level can be upto any level which gets handled by UNIX "ls -R " utility.
Question
I would like to know that whats could be next approach to handle the n level of hierarchy in the directory?
In general whats sort of approaches we should follow when we need to model/design similar things where "part-whole hierarchies(recursive)" needs to model. I am little bit aware about the "composite design pattern" which can be used to model such stuff. Could this pattern be applied in this particular problem?. If yes, could someone provide explanation/comment?
My main intention over here is to understand general guideline to handle such problem using modern C++ concepts/library/design concepts.Kindly let me know in case someone require any information on this.
I would rename your directory class, it doesn't model a directory, it is a function that prints the contents of a directory.
You can use a range-based for loop with a directory_iterator to make the syntax simpler:
for (auto f : fs::directory_iterator{dir})
Your program assumes it will only be called with a single argument that refers to a directory, whereas ls -R can be called with zero or more arguments that are files or directories.
I would do it like this, although this could probably be improved to simplify the logic in main and incorporate it into the ls function:
#include <utility>
#include <vector>
#include <string>
#include <algorithm>
#include <iostream>
#include <experimental/filesystem>
namespace fs = std::experimental::filesystem;
void ls(std::ostream& out, const fs::path& dir)
{
std::vector<std::pair<std::string, bool>> files;
for (auto f : fs::directory_iterator{dir})
files.emplace_back(f.path().filename(), is_directory(f));
std::sort(files.begin(), files.end());
// print directory contents
out << dir.string() << ":\n";
for (auto& f : files)
out << f.first << '\n';
out << std::endl;
// recurse into directories
for (auto& f : files)
if (f.second)
ls(out, dir / f.first);
}
int main(int argc, char** argv)
{
if (argc < 2)
ls(std::cout, ".");
else
{
std::vector<std::string> files;
std::vector<std::string> dirs;
for (int i = 1; i < argc; ++i)
if (fs::is_directory(argv[i]))
dirs.push_back(argv[i]);
else
files.push_back(argv[i]);
std::sort(files.begin(), files.end());
for (auto& f : files)
std::cout << f << '\n';
std::cout << '\n';
std::sort(dirs.begin(), dirs.end());
for (auto& d : dirs)
ls(std::cout, d);
}
}
I am currently trying to create a file after trying to open it, if it does not exist.
I would like to do this without the use of ios::app because with this, I would not be able to use the seek function in the future.
My includes:
#include <string>
#include <errno.h>
#include <fstream>
#inlcude <iostream>
using namespace std;
My main:
string str;
cout << "Enter a string: " << endl;
cin >> str;
fstream fstr;
fstr.open("test.txt", ios::in | ios::out | ios::binary | ios::ate );
if (fstr.fail()) {
cerr << strerror(errno) << endl;
fstr.open("test.txt", ios::out);
fstr.close();
fstr.open("test.txt", ios::in | ios::out | ios::binary | ios::ate );
} // if file does not exist, create by using "ios::out",close it, then re-open for my purpose
if (fstr.is_open()) { // if the file opens/exists
fstr << str << endl; // str goes into fstr
fstr.close(); // close
}
The code above seems to be working fine, but I just wanted to be open for any other advice, other suggestions or alternate methods of achieving the same goal. Thanks!
As an alternative, you could use the stat() function, which is available on Unix based systems and also Windows. Then you could write a very simple function that test if a filename exists and names a file:
#include <sys/stat.h>
bool FileExistAndIsFile(const std::string & filePath)
{
int result;
struct stat statBuf;
result = stat(filePath.c_str(), &statBuf);
return ((result == 0) && S_ISREG(statBuf.st_mode)) ? true : false;
}
I'm going for absolute minimalism here. (It's been a while since I've worked with the Lua C API.)
#include <lua.hpp>
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char** argv)
{
lua_State* state = luaL_newstate();
luaL_openlibs(state);
string input;
while (getline(cin, input))
{
auto error = luaL_dostring(state, input.c_str());
if (error)
{
cerr << "Lua Error: " << lua_tostring(state, -1) << '\n';
lua_pop(state, 1);
}
}
lua_close(state);
return 0;
}
This program works fine as long as I feed it perfect Lua. However, if I enter something bad (such as asdf()), the program crashes! Why is it not handling my error gracefully?
I've tried breaking out the calls before. It crashes on the call to lua_pcall itself. I never make it past that line.
The binary download (5.2.1 I believe) has a bug that was corrected in 5.2.3. I rebuilt the library from source, and now my program works fine.
While I was working on a project, I noticed that I am not able to load images. Here is the simple code that I used to check:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/objdetect/objdetect.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main( int argc, char** argv )
{
if( argc != 2)
{
cout <<" Usage: display_image ImageToLoadAndDisplay" << endl;
return -1;
}
Mat image;
image = imread(argv[1], CV_LOAD_IMAGE_COLOR); // Read the file
if(! image.data ) // Check for invalid input
{
cout << "Could not open or find the image" << std::endl ;
return -1;
}
Mat gray_image;
cv::cvtColor(image, gray_image, CV_RGB2GRAY);
imwrite("Gray_Image.jpg",gray_image);
return 0;
}
Here is its output when I execute it:
root#beaglebone:~# ./tryit lena.jpg
Could not open or find the image
I tried to directly use the address of the image ("/home/root/lena.jpg") instead of argv[1] but nothing changed.
What can be the problem?
ps: I am cross-compiling this OpenCV program and then running it on my BeagleBone which has Angstrom Linux installed on it. Can this problem related to it?
I solved my problem by deleting existing OpenCV libraries on my angstrom image and replacing them with the working ones.
Try saving the image with a png extension and then opening it. For some reason, files with a png extension work better than other extensions like jpg/gif.