popen问题

jhams 2018-03-05 06:09:26
使用popen函数执行命令的时候,fput一次没问题,连续fput两次以上就会不成功,用fwrite也不行,是什么原因呢?
代码如下:
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{
FILE *write_fp;

write_fp = popen("cryptsetup --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 10000 luksFormat luks.vol","w");
if(write_fp != NULL)
{
fputs("YES\r",write_fp);
fputs("1022\r",write_fp);
fputs("1022\r",write_fp);
pclose(write_fp);
//exit(EXIT_SUCCESS);
}
else
{
printf("luksFormat failed\n");
exit(EXIT_FAILURE);
}
exit(EXIT_SUCCESS);
}
...全文
414 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
jhams 2018-06-29
  • 打赏
  • 举报
回复
又忘了怎么弄了,在这里找到答案了
jhams 2018-03-06
  • 打赏
  • 举报
回复
这样就对了,奇怪 write_fp = popen("cryptsetup --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 10000 luksFormat luks.vol","w"); if(write_fp != NULL) { sprintf(buffer,"1234\n"); fwrite(buffer,sizeof(char),strlen(buffer),write_fp); pclose(write_fp); //exit(EXIT_SUCCESS); } 实际手动输入cryptsetup --cipher aes-xts-plain64 --key-size 512 --hash sha512 --iter-time 10000 luksFormat luks.vol命令的时候,是需要输入YES和两次密码的
sevilcry 2018-03-06
  • 打赏
  • 举报
回复
linux下没有\r,用\n
zhxianbin 2018-03-06
  • 打赏
  • 举报
回复
三行 用 \n 拼成一行试试
jklinux 2018-03-05
  • 打赏
  • 举报
回复
可能cryptsetup进程不会从标准输入接收多次信息

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧