How do you have the user input the folder name and have it created in the desktop (for mac)?
This is what I have so far.. (and extra code underneath)
#include <iostream>
#include <fstream>
#include <sys/stat.h>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
int main ()
{
char game_name [100];
cout << "Game Name: ";
cin >> game_name;
const char* homeDir = getenv ("Home");
char final [256];
sprintf (final, "%s/Desktop/%s",homeDir, game_name);
mkdir(final,0775);
other code....
....
...
..
return 0;
}
Environment variables are case sensitive, so you need to use getenv("HOME") instead of getenv("Home").
Use Boost Library (though there will be overhead of setting up boost on your system but its worth for doing many other stuffs in C++): boost::filesystem::create_directories()
#include <boost/filesystem.hpp>
// your code....
boost::filesystem::create_directories("/bla/a");
Related
Since a few days I was trying to get a C++ code that converts the Turkish I character to lowercase ı correctly on VS2022 on Windows.
As I understand, Turkish I has the same Unicode as regular Latin I, thus, I need to define the locale as Turkish before converting, I used the following code:
#include <clocale>
#include <cwctype>
#include <fstream>
#include <iostream>
#include <locale>
#include <string>
int main() {
std::wstring input_str = L"I";
std::setlocale(LC_ALL, "tr_TR.UTF-8"); // This should impact std::towlower
std::locale loc("tr_TR.UTF-8");
std::wofstream output_file("lowercase_turkish.txt");
output_file.imbue(loc);
for (wchar_t& c : input_str) {
c = std::towlower(c);
}
output_file << input_str << std::endl;
output_file.close();
}
It worked fine on Linux, outputing ı, but didn't work correctly on Windows and it outputed i inplace of ı.
After some research I think it is a bug in Windows unicode/ascii mapping, so I went to an alternative solution, using an external library called boost, here is my code:
#include <boost/algorithm/string.hpp>
#include <string>
#include <locale>
#include <iostream>
#include <fstream>
using namespace std;
using namespace boost::algorithm;
int main()
{
std::string s = "I";
std::locale::global(std::locale{ "Turkish" });
to_lower(s);
ofstream outfile("output.txt");
outfile << s << endl;
outfile.close();
return 0;
}
again, outputing i inplace of ı. also using to_lower_copy outputs the same.
I'm trying to output the symbol of Electric Light Bulb with code U+1F4A1 to Windows Terminal (experiment with Unicode). I can't realize how to do that. I tried to use wchar_t, wcout, to change console output code page, and with no result. Who made it. please tell how to do that.
#include <uchar.h>
#include <iostream>
#include <cstdlib>
#include <clocale>
#include "Windows.h"
#include <io.h>
#include <fcntl.h>
int main() {
SetConsoleCP(12000);
SetConsoleOutputCP(12000);
/*Alternative*/
system("chcp 65001");
std::cout << u8"\u1F4A1" << std::endl;
return 0;
}
Im new to coding c++ and i am trying to call a function from another file to check if the string containing the text file is made up of alphabetic characters, but for some reason it is not working.
I am getting errors in my ap.cpp file saying
Invalid arguments '
Candidates are:
bool is_alpha(?)
'
and
‘is_alpha’ cannot be used as a function
and also errors in my header file saying
Type 'string' could not be resolved
MY CODE:
AP.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "functions.h"
using namespace std;
int main () {
string line;
string textFile;
ifstream myfile ("encrypted_text");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
textFile += line;
}
myfile.close();
}
else cout << "Unable to open file";
bool check = is_alpha(textFile);
if (check){
cout << "true";
} else cout << "false";
return 0;
}
checkFunctions.cpp
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
#include "functions.h"
using namespace std;
bool is_alpha (string str) {
for(int i=0; i < str.size(); i++)
{
if( !isalpha(str[i]) || !isspace(str[i]))
{
return true;
}
}
return false;
}
functions.h
#ifndef FUNCTIONS_H_
#define FUNCTIONS_H_
#include <string>
bool is_alpha(string str);
#endif /* FUNCTIONS_H_ */
It'll be the best if you not use using namespace std; until you have a proper understanding about the language and its implications. If you were wondering what using namespace does is, it basically sets the content in the namespace and puts it to the global namespace (in which you don't need to specify where it comes from, in this its std and the way to call it is std::).
The error is because the compiler doesn't know where the string in bool is_alpha(string str); comes from. So to solve this, take my first advice to consideration and you can specify where it comes from like this: bool is_alpha(std::string str);.
Plus you don't need to add the libraries included in a header file again in the source file. This means that you can remove the #include <string> from AP.cpp and checkFunctions.cpp.
More about header files: https://stackoverflow.com/a/9224641/11228029
I'm having issue in the ifstream function, I have tried using the argv[1] as parameter but wont load the map, the map is located in the same folder of main code.
I'm stucked here and can not debug.
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <sstream>
using namespace std;
int main (int argc, char *argv[]){
int h;
int w;
int var;
string inputLine;
ifstream f;
f.open("map.pgm",ios::in);
if (!f){
cout << "error" << endl;
exit(1);
}
I'm using Visual Studio 2017
Change this line:
if (!f){
by this:
if (!f.is_open()){
BTW you can check current directory path with GetModuleFileName
I am working on an assignment and I need to create pipes so that other programs handle different functions. I am able to pipe through the command line no problem, thats easy. However using dup2 and execl have been tricky for me. At one point I was able to get output from one part of my program but it wasn't reading anything in from another part.
here is what i have:
pipeline.cpp
#include <sys/types.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <stdlib.h>
#include <cstdlib>
#include <iostream>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include<iostream>
#include<cstdlib>
#include<unistd.h>
#include<iomanip>
#include <sys/wait.h>
using namespace std;
int main(int argc, char *argv[])
{
int number = atoi(argv[1]);
int x2ypipe[2];
pipe(x2ypipe);
if(x2ypipe==0){
cout<<"ERROR:"<<errno<<endl;
}
pid_t xchild =fork();
if(xchild==0){
dup2(x2ypipe[1],STDOUT_FILENO);
close(x2ypipe[0]);
close(x2ypipe[1]);
execl("./part1.cpp","part1.cpp", (char *)NULL);
}
pid_t ychild =fork();
if(ychild==0){
dup2(x2ypipe[0],STDIN_FILENO);
close(x2ypipe[0]);
close(x2ypipe[1]);
execl("./part2.cpp", "part2.cpp", (char *)NULL);
}
close(x2ypipe[0]);
close(x2ypipe[1]);
wait(NULL);
wait(NULL);
part1.cpp
#include<iostream>
#include<cstdlib>
#include<unistd.h>
#include<iomanip>
using namespace std;
int main(int argc, char *argv[])
{
int number = atoi(argv[1]);
for (int k = 1; k <= 9; k++)
{
cout << k << " " << flush;
sleep(1);
}
return 0;
}
part2.cpp
#include <iostream>
#include <cstdlib>
#include <unistd.h>
#include <iomanip>
using namespace std;
int main()
{
int number;
while (cin >> number)
{
cout << 2 * number - 1 << " " << flush;
}
return 0;
}
Ok so pipeline.cpp : forks twice and creates a pipe between the two children. Then each use excel to replace its process with the programs part1 and part2. So my understanding is that part1 program would run and anything it outputs will be picked up by the second child which runs part2 and from there part two would output normally since it's output descriptor wasn't changed. Am I missing or misusing something here?
I noticed a couple of things:
You're not passing the number to the part1 process when you exec it
You're not checking for failure from execl() or any of the other OS functions
I think once you do these two things, you'll find out what the real problem is. I won't just tell you what the answer is, because it's worthwhile learning how to diagnose such problems yourself. (I was able to run your code successfully with only minor modifications. The problem does not lie in how you're handling the pipes and file descriptors.)
I think you need to return 0; after your exec calls. But I am even more lost than you it seems.