How to change filename alongwith ext in C/CPP [closed] - c++

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have hundreds of files in a folder like:
mp_12345.dat
mp_23455.dat
mp_12323.dat
mp_44445.dat
.
.
.
I want to rename all file to another folder:
Ind_somecircle_mp_12345.mbin
Ind_somecircle_mp_23455.mbin
Ind_somecircle_mp_12323.mbin
Ind_somecircle_mp_44445.mbin
.
.
And so on.
source folder: /home/dir1/foo/
destination folder: /home/dir2/foo/
I am looking for C or C++ code to do so.
Thanks in advance.

Actually this would be a better job for a shell script, but if you insist on C then rename(src, tgt); is what you are looking for.
If you want to read the filenames from the directory, you must use opendir and readdir to loop through the files.
A short sample:
void main(int c,char **args)
{
DIR *dir;
struct dirent *dent;
dir = opendir("mydir);
if(dir!=NULL)
{
while((dent=readdir(dir))!=NULL)
printf(dent->d_name); <-- rename
}
close(dir);
}

If you insist on using C/C++ for the job you need to use
opendir - http://linux.die.net/man/3/opendir, readdir - http://linux.die.net/man/3/readdir to get the list of files
That use rename - http://linux.die.net/man/2/rename to change the filename of a file

Related

create all folder in a big path (with subfolders) [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
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

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.

Packing a DLL into an exe and deleting it after running [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 4 years ago.
Improve this question
I want to pack a dynamic link library (D L L) into an executable and when I run it I want it to be deleted from the hard drive.
so the first question i need help with is: How to PROPERLY add a D L L into my project? so when i build it, the D L L is in the executable?
and the second question is: once the D L L is properly included in the project & the project is build into an executable, i want the D L L to be injected to another process from the executable using the following code:
pastebin.com/zsYVMqvs
and now to the third question: once the D L L is injected & it has to be extracted somewhere right? so i would like to delete the extracted D L L instantly after injecting so it does not stay there.
Putting the DLL into the resources of your .exe is a possibility. You then need to extract the DLL from the resources to a dll file on the local hard drive, probably into the temp directory. Then use it. Before the program quits, delete the dll created beforehand.
To extract use FindResource, LoadResource, LockResource and SizeofResource .
Flow of operations:
Extract the DLL to the TEMP directory (use GetTempPath)
run program doing whatever stuff you want to do with the DLL
befor the program quit make sure the DLL isn't used any more
delete the dll created under 1.

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

How to use graphviz neato from within C++ program [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
Contination of this question
#include <boost/graph/graphviz.hpp>
#include <boost/graph/grid_graph.hpp>
typedef boost::grid_graph<2> Grid;
int main()
{
boost::array<std::size_t, 2> lengths = { { 3, 5 } };
Grid grid(lengths);
std::ofstream gout;
gout.open("test.dot");
boost::write_graphviz(gout, grid);
}
I run
system('neato -Tpng overlap=false test.dot > test.png');
from a c++ program. It is not working.i.e png file is not created
When I run the same command from a console prompt, it does work as expected.
If redirection doesn't work on your system's shell, use the option:
system("neato -Tpng overlap=false test.dot -o test.png");
Also be aware of your working directory. Make sure your input is in the current working directory, and also check that you are looking for the output (test.png) in that same directory.
Alternatively, spell out the paths
system("neato -Tpng overlap=false /path/to/dir/test.dot -o /path/to/dir/test.png");
CAVEAT: of course, in C++ strings backslashes need to be escaped, if your paths contains them