C ++ - 将文件复制到所有子目录(C++ - Copying a file to all sub directories)
我正在尝试创建一个用于教育目的的程序,该程序应该能够将
.exe
的同一目录中的一个文件(任何扩展名,文件名永远不会更改)复制到主目录中的所有目录。 因此,例如我有:
- Main_Directory
- Program.exe文件
- file.something
- directory1中
- Directory2
- directory3目录
在这种情况下,程序应该将file.something复制到directory1,2和3.通过使用此代码,我可以访问
.exe
文件夹的绝对路径:char basePath[255]=""; _fullpath(basePath,argv[0],sizeof(basePath));
理想情况下,要解决问题,可以实现两个功能:
第一个功能:
Input: (char[]) The .exe's folder if necessary Output: (int) Number of folders inside the .exe's folder.
第二功能:
input: (int) which folder to pick (int) total number of folders (char[]) the .exe's folder if necessary Output: (char[]) the name of the folder
有了这两个函数(它应该返回一个字符数组而不是字符串,因为我被迫使用那些数组来使代码工作)我能够做一个简单的循环,每次更改目标的文件夹并复制归档给它。
当然,问题是如何解决问题,如果存在更好的解决方案,那就这样吧。
最后一件事:如果你能添加每个块或命令所做的事情,我会非常高兴,因为我还在学习c ++,而且我现在是初学者。
I'm trying to create a program for educative purposes which should be able to copy one file (of any extension, the name of the file never changes) inside the same directory of the
.exe
to all the directories inside the main directory. Thus for example I have:
- Main_Directory
- Program.exe
- file.something
- Directory1
- Directory2
- Directory3
In that situation, the program should copy file.something into directory1, 2 and 3. Through the use of this code I have access to the absolute path of the
.exe
's folder:char basePath[255]=""; _fullpath(basePath,argv[0],sizeof(basePath));
Ideally, to solve the problem, two function could be made:
First function:
Input: (char[]) The .exe's folder if necessary Output: (int) Number of folders inside the .exe's folder.
Second function:
input: (int) which folder to pick (int) total number of folders (char[]) the .exe's folder if necessary Output: (char[]) the name of the folder
With those two functions (which should return an array of chars instead of a string as I was forced to use those arrays to make the code work) I'd be able to make a simple loop, changing the destination's folder every time and copying the file to it.
Of course it's just how the problem could be fixed, if a better solution exists, so be it.
One last thing: I'd be really happy if you could add what each block or command does as I'm still learning c++ and I'm currently a beginner.
原文:https://stackoverflow.com/questions/26147065
最满意答案
由于缓冲。 通常,
stdout
是行缓冲的,所以printf("x ");
不会立即将
"x "
写入终端,而是写入输出缓冲区。 当进程fork()
s被复制时,因此第二次迭代后的四个进程中的每一个在输出缓冲区中都有两个"x "
[一个来自父节点/在第一次迭代中分叉,一次来自第二次迭代]它退出并且完全打印八个xs。在
printf("x ");
后立即冲洗缓冲区printf("x ");
只有六个将被打印。Because of buffering. Usually,
stdout
is line-buffered, soprintf("x ");
doesn't immediately write the
"x "
to the terminal but to the output buffer. That is copied when the processfork()
s, so each of the four processes after the second iteration has two"x "
in the output buffer [one from the parent/before forking in the first iteration, one from the second iteration] when it exits and eightx
s are printed altogether.Flush the buffer immediately after the
printf("x ");
and only six will be printed.
相关问答
更多-
fork()和输出(fork() and output)[2022-01-20]
这不是你原来想的。 输出缓冲区不共享 - 执行fork时, 两个进程都获得同一个缓冲区的副本 。 所以,在fork之后,这两个进程最终都会刷新缓冲区,并打印内容以分开显示。 这只是因为cout是缓冲IO 。 如果你使用cerr,这是没有缓冲的,你应该只看到消息一次,pre-fork。 This isn't quite what you thought originally. The output buffer is not shared - when you execute the fork, both p ... -
由于缓冲。 通常, stdout是行缓冲的,所以 printf("x "); 不会立即将"x "写入终端,而是写入输出缓冲区。 当进程fork() s被复制时,因此第二次迭代后的四个进程中的每一个在输出缓冲区中都有两个"x " [一个来自父节点/在第一次迭代中分叉,一次来自第二次迭代]它退出并且完全打印八个xs。 在printf("x ");后立即冲洗缓冲区printf("x "); 只有六个将被打印。 Because of buffering. Usually, stdout is line-buffe ...
-
从初始化到完成第一个循环上下文的所有过程,从第一个完整迭代开始,将发生以下情况: 1.初始化程序 i从i = fork();初始化i = fork(); 进程父级 : i = pid(child1) 子进程child1 : i = 0 在每个过程中触发条件测试会发生一些有趣的事情 2.过程:父母 i < fork()将fork另一个子进程child2 。 如果返回的pid(child2)大于pid(child1) ,则符合条件测试,并且父进程继续到循环体 3.过程:child1 i从初始值设定为零 i < ...
-
fork()发生了什么?(What exactly happens with fork()?)[2024-04-27]
您有两个进程在同一时间从终端读取。 有人猜测哪个进程得到了输入。 如果父进程首先获取输入,它将退出并将控制权返回给shell。 (请注意,这实际上导致重复相同的情况,shell和子进程争夺输入。) 如果子进程首先获得输入,它将退出,但控制不会返回到shell,直到父进程退出。 如果您有两个进程从同一个终端读取,您不应该期望一致的行为。 You have two processes reading from the terminal at the same time. It is anybody's gues ... -
尝试绘制流程创建树并研究/记住以下几点: P1。 fork()将pid(更大的tan 0)返回到当前进程,并在子进程中返回0。 P2。 你需要知道表达式A() && B() || C() 评估A() && B() || C() ; 例如,如果A()返回0 (false)函数B不会被评估,因为0 && whatever总是为0 。 现在,让我们将调用标记为易于参考: Main() { Fork() /*[1]*/; Fork() /*[2]*/ && fork ()/*[3]*/ || for ...
-
Fork()和父/子进程(Fork() and parent/child processes)[2023-11-24]
编写的练习有点令人困惑,但在我看来,作者所得到的是: 你的程序包含一个“值”变量:让我们称之为val 。 在程序调用fork() ,子进程应将val设置为10,而父进程将其设置为3.这是因为子进程和父进程具有不同的地址空间; 即使它们都运行相同的代码,名称val指内存中的子进程和父进程的不同位置。 换句话说,你不需要期望fork()返回3或10.在运行for(...)循环的简短之后,你可以让父进程设置val = 3并让子进程设置val = 10 。 if (frk == 0) { ... v ... -
诀窍是使用fork的返回值(<0是一个错误,== 0是子进程,> 0是父进程),让第一个子进程没有调用fork来获得3个进程: //we start with 1 process if (fork() > 0) //now we have 2 processes fork(); //only the parent calls this fork because of the if, so we have 3 processes fork(); //all 3 processes calls this ...
-
行为解释1: 程序通常无法控制输出顺序。 这是并行过程的要点。 操作系统决定在任何时间点执行哪个进程,并且两个进程同时执行(对人眼)。 因此,输出通常是互补的。 行为解释2: 你猜对了。 父流程在分叉之前完成。 如果您想要父pid,则可以在父进程中使用waitpid(x, &status, 0) ,如果您需要父进程保持活动直到执行子进程。 这个链接可以帮到你。 Explanation of Behaviour 1: The order of output cannot be controlled by th ...
-
fork()和printf()(fork() and printf())[2022-02-08]
printf()不保证是原子的。 如果需要原子性,请使用带有字符串的write() ,如果需要,使用s*printf()等预先格式化。 即使这样,你应该使用write()的数据大小不是太大 : {PIPE_BUF}字节或更少字节的写请求不应与来自在同一管道上执行写操作的其他进程的数据交错。 无论是否设置了文件状态标志的O_NONBLOCK标志,大于{PIPE_BUF}字节的写入都可以在任意边界上进行数据交织,并通过其他进程进行写入。 printf() is not guaranteed to be atom ... -
fork()新进程并写入子进程和父进程的文件(Fork() new process and write to files for child and parent processes)[2023-08-31]
在C中,使用FILE结构的输入/输出操作在用户进程级别进行缓冲。 在您的情况下,您写入fp_parent的输出实际上并未写入磁盘,并且在fork时保存在本地缓冲区中。 fork创建整个过程的副本,包括包含Begin的缓冲区,这就是它在文件中出现两次的原因。 尝试把fflush(fp_parent); 在fork之前。 这将刷新缓冲区,脏线将从文件中消失。 In C, the input/output operations using FILE structure are buffered at the le ...