create all folder in a big path (with subfolders) [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 1 year ago.
Improve this question
Im trying to create all folder in a path that reach to a file, for that by a little search I found out about std::filesystem::create_directories its ok but the problem of using it is looks like its can't handle long paths with so many subfolders, for small path its working without any problem, but when I tested it with a path that have 14 subfolder and near 185 character it failed and give me the The filename or extension is too long. error
here is the code Im using so far:
void create_dirs(std::filesystem::path path)
{
if (!std::filesystem::exists(path))
{
path.remove_filename(); // remove file name
try
{
std::filesystem::create_directories(path); // create dirs
}
catch (std::filesystem::filesystem_error& e)
{
std::cerr << e.what() << std::endl;
}
}
}
so what can I do for this? and what is the best solution?
My OS: Windows

Related

How to get available space on a network drive 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.
Improve this question
I tried using std::filesystem::space(dir) but with no luck -> it cannot determine available disk space, sets it to uintmax.
auto info = std::filesystem::space("K:\\Dir");
if(info.available == static_cast<uintmax_t>(-1))
{
std::cout << "Error occurred!\n";
}
K:\Dir does indeed exist. And this snippet prints Error occurred for my drive mounted on K:.
If you want to get the free space you need to try
const std::filesystem::space_info spaceInfo = std::filesystem::space(dir);
cout << static_cast<std::intmax_t>(spaceInfo.free) << endl;
Here dir = "/path/to/dir/";
Refer cppreference

ERROR: Thread 1: EXC_BAD_ACCESS (code=1, address=0x68) FIX: PLACE THE IN AND OUT FILES IN THE WORKING DIRECTORY OF THE PROJECT [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 2 years ago.
Improve this question
I get this error:
Thread 1: EXC_BAD_ACCESS (code=1, address=0x68)
on the following line:
while(fscanf(f,"%d", &a[x])==1)
1st time trying to do my class homework in Xcode, used code blocks before and managed to run this same code on it, any help is much appreciated!
#include <cstdio>
int a[1001];
int main()
{
int aux1,aux2,aux3,x=0;
FILE *f,*g;
f=fopen("1.in","r");
g=fopen("2.out","w");
while(fscanf(f,"%d", &a[x])==1) //HERE
{
aux1=a[x];
aux2=0;
aux3=0;
while(a[x]!=0)
{
aux2=aux2*10+a[x]%10;
a[x]=a[x]/10;
aux3=aux3*10+9;
}
if(aux3-aux2==aux1)
fprintf(g,"1 ");
else fprintf(g,"0 ");
x++;
}
fclose(f);
fclose(g);
return 0;
}
To find out if you put the in and out files in the working directory you must try to print the read numbers in a terminal, if it doesn't print anything then I your .in and .out files are outside the working directory.
Debug:
After this line :
fscanf(f,"%d", &n);
do:
printf("%d", n);
if it prints the number well, guess your files are in the working directory, just make sure .out and .in are in the same directory and that's it.
Make sure that you actually opened 1.in file by checking what open() had returned. Make sure that you do not go outside of array bounds, if x reach 1001, something bad may happen, it's an Undefined Behaviour.
The problem was that I placed 1.in and 1.out outside the working directory, to fix this had to go to Product>Scheme>Edit Scheme>Run>Options and set a custom "Working Directory" where I placed the 1.in and 1.out files. Thanks for the help guys.

object is not being saved even after fstream.clear() in 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 5 years ago.
Improve this question
I am using C++ to write a music memorize game for my school project, here the Player objects are not being saved in the PLAYER_DATA.DAT file even i tried clear() function
here is the peace of code (as the whole code is 600+ lines)
Player p_dat,plyr;
plyr.getData();
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary);
while(P_file.read((char*)&p_dat, sizeof(p_dat)))
{
if(nameEqual(plyr,p_dat))
{
P_file.clear();
gotoxy(1,10);
delline();
textcolor(RED);
cout<<"\t\t EXIXTING PLAYER PROFILE FOUND!\n";
int ch = playPanel("It's me", "Change Name");
if(ch == 0)
{
P_file.seekp(P_file.tellg() - sizeof(Player));
GameStarted = 1;
if(c == 1)
Campaign(p_dat,P_file);
else
Endless(p_dat,P_file);
return;
}
else
{
startGame(c);
return;
}
}
}
P_file.clear();
P_file.seekp(0,ios::end);
P_file.write((char*)&plyr,sizeof(plyr));
Just to make it short, The last line of the code is not doing anything the file already exists and of size 0kb
however,
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary)
P_file.write((char*)&plyr,sizeof(plyr));
is saving the file. Please help me.
EDIT 1.1
finally found this line is problematic
P_file.seekp(0,ios::end);
Its working for code, i.e correctly saving objects
P_file.clear();
P_file.write((char*)&plyr,sizeof(plyr));
P_file.seekp(0,ios::end);
while removing
P_file.seekp(0,ios::end);
making the code look like,
P_file.clear();
P_file.write((char*)&plyr,sizeof(plyr));
after the while loop, is not saving the file
again does not save the object
this line is making problem, are there any alternatives or solutions?
Compile with all warnings and debug info (g++ -Wall -Wextra -g with GCC) then use the debugger (e.g. gdb).
Read carefully documentation of C++ IO functions.
Check that your file has been opened correctly; after:
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary);
add
if (!P_file) { std::cerr << "failed to open PLAYER_DATA.DAT" << std::endl; };
On some systems (e.g. Linux), you might also display strerror(errno).
After some intermediate call to write, consider using flush.
Be sure that your program is started in the correct working directory.
On Linux, you might also use strace(1) to understand the system calls done by your program.
At last, did you consider using some simple database, e.g. with sqlite, or some indexed file, e.g. with gdbm ?

Why file is not created in /etc/ directory [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 6 years ago.
Improve this question
Please find the code sample
void createFile(const std::string& FileName, const std::string& Content)
{
ofstream of(FileName.c_str());
of<<Content;
of.close();
}
const std::string testFile = "/etc/testFile";
const std::string EmptyContent = "";
createFile(testFile, EmptyContent);
File is not creating at /etc/ directory. I think this is related to permissions. What extra I have to add in the code to work.
There's nothing extra that you can add to this program to "make it work". If an arbitrary program can write to /etc, this would toss the traditional POSIX security model out the window.
In order to be able to write to /etc, your program must be executed as root.
It seems to be a permission issue. Try to run your program using sudo:
sudo yourprogram

ifstream returning true for files that don't exist [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
What I am trying to do?
I'm creating a c++ program where I am checking as of the file exists for the execution of command in CWD (Current working directory) if not then bin and if not there then sbin folder.
What I am doing?
I am checking the existence of application using ifstream. If the object is created I consider it as valid. otherwise i don't. Here is a code sample of what I have done
string pathforCWD = argv[1];
ifstream checkFileCWD(pathforCWD.c_str());
if (!checkFileCWD) {
cout << "\nCommand not Found in working directory\n";
cout<<endl<<"";
string pathforBin = "/bin/",argv[1];
ifstream checkFileBin(pathforBin.c_str());
if(!checkFileBin)
{
cout<<"\nCommand also not found in Bin\n";
string pathforSbin = "/sbin/",argv[1];
ifstream checkFileSbin(pathforSbin.c_str());
if(!checkFileBin)
{
cout<<"\nFile also not found in sBin\n";
}
else
{
cout<<"\nCommand found in SBin directory.. Executing it."<<endl;
int response = system((orignalCommand).c_str());
programExecution(response);
}
}
else
{
cout<<"\nCommand found in Bin directory.. Executing it."<<endl;
int response = system((orignalCommand).c_str());
programExecution(response);
}
} else {
cout<<"\nCommand found in working directory.. Executing it."<<endl;
int response = system(("./" + orignalCommand).c_str());
programExecution(response);
}
What Issue I am facing?
If I pass an invalid program name argument to the program which does exist. The current working directory ifstram object is not created which means that file does not exist there. How ever for the /bin/. It says that the object exists each time. even if it doesn't. What I am doing wrong?
The following definition doesn't do what you think it does:
string pathforBin = "/bin/",argv[1];
Hint: you're declaring two names here, pathforBin and argv.