首页 \ 问答 \ C ++ - 将文件复制到所有子目录(C++ - Copying a file to all sub directories)

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
更新时间:2023-12-25 12:12

最满意答案

由于缓冲。 通常, stdout是行缓冲的,所以

printf("x ");

不会立即将"x "写入终端,而是写入输出缓冲区。 当进程fork() s被复制时,因此第二次迭代后的四个进程中的每一个在输出缓冲区中都有两个"x " [一个来自父节点/在第一次迭代中分叉,一次来自第二次迭代]它退出并且完全打印八个xs。

printf("x ");后立即冲洗缓冲区printf("x "); 只有六个将被打印。


Because of buffering. Usually, stdout is line-buffered, so

printf("x ");

doesn't immediately write the "x " to the terminal but to the output buffer. That is copied when the process fork()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 eight xs are printed altogether.

Flush the buffer immediately after the printf("x "); and only six will be printed.

相关问答

更多
  • 这不是你原来想的。 输出缓冲区不共享 - 执行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 < ...
  • 您有两个进程在同一时间从终端读取。 有人猜测哪个进程得到了输入。 如果父进程首先获取输入,它将退出并将控制权返回给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 ...
  • 编写的练习有点令人困惑,但在我看来,作者所得到的是: 你的程序包含一个“值”变量:让我们称之为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 ...
  • printf()不保证是原子的。 如果需要原子性,请使用带有字符串的write() ,如果需要,使用s*printf()等预先格式化。 即使这样,你应该使用write()的数据大小不是太大 : {PIPE_BUF}字节或更少字节的写请求不应与来自在同一管道上执行写操作的其他进程的数据交错。 无论是否设置了文件状态标志的O_NONBLOCK标志,大于{PIPE_BUF}字节的写入都可以在任意边界上进行数据交织,并通过其他进程进行写入。 printf() is not guaranteed to be atom ...
  • 在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 ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)