String 在C脚本Unix中将'date'命令的输出读入变量
我必须将String 在C脚本Unix中将'date'命令的输出读入变量,string,date,unix,types,pipe,String,Date,Unix,Types,Pipe,我必须将datecommand(Unix)输出的内容作为字符串存储到变量中,然后将其写入子进程中的管道。最后,通过使用管道在父进程中输出它。这就是我迄今为止所尝试的: #include <string.h> #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <sys/types.h> #include <unistd.h> int m
date
command(Unix)输出的内容作为字符串存储到变量中,然后将其写入子进程中的管道。最后,通过使用管道在父进程中输出它。这就是我迄今为止所尝试的:
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
int main()
{
int channel[2];
if(pipe(channel) == -1) { printf("Eroare la crearea pipe ului!\n"); return 1; }
pid_t pid = fork();
if(pid < 0) { printf("Eroare la crearea procesului\n"); }
else if(pid > 0)
{
wait(NULL); // wait for the child to write to the pipe
close(channel[1]); // close not-needed side of pipe
char *aux;
read(channel[0], aux, sizeof(channel[0]));
printf("Sirul citit este: '%s'\n", aux);
close(channel[0]);
// exit(0);
}
else
{
close(channel[0]);
// char *data_acum = (char*)system("date");
char *data_acum = system("date");
printf("variabila `data_acum` are valoarea '%s'\n", data_acum);
// printf(typeof(data_acum));
write( channel[1], data_acum, sizeof(data_acum) );
// write( channel[1], system("date"), sizeof(system("date")) );
// exit(0);
}
return 0;
}
更具体地说,我的问题是:
我试图弄明白为什么这一行char*data\u acum=system(“date”)
或注释的那一行//char*data\u acum=(char*)system(“date”)
不起作用;通过“working”,我希望存储date
将显示的字符串,如果我直接在命令行中使用它的话。这个系统(“日期”)
是否不返回一个字符串,我可以将它存储在类型为const char*
的变量中
另外,我不能100%确定警告想要说什么:警告:初始化从整数生成指针,而无需强制转换[-Wint转换]
。我知道类型之间存在一些不兼容,但是system(“date”)
是否实际返回int
?因此,我不正确地试图以某种方式将其转换为常量char*
?我记得看到一些关于日期实际上是十六进制数的东西
对不起,也许,问了太多问题!最后,总结一下我的问题:系统(“日期”)返回了什么?如何解决我的问题?
预期产出:
Thu Apr 30 02:05:39 EEST 2020
variabila `data_acum` are valoarea '(null)'
Sirul citit este: '1I^HHPTLz'
Thu Apr 30 02:05:39 EEST 2020
variabila `data_acum` are valoarea 'Thu Apr 30 02:05:39 EEST 2020'
Sirul citit este: 'Thu Apr 30 02:05:39 EEST 2020'
注意事项:
1) 我必须使用日期
2) 我没有故意检查孩子的返回状态
3) 我知道使用
wait(NULL)
是不好的做法,我已经设法解决了我的问题。我在寻找类似的东西:
#include <stdlib.h>
#include <stdio.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <unistd.h>
#include <string.h>
#define BUFFER_SIZE 105
int main()
{
int channel[2];
if(pipe(channel) == -1) { printf("Eroare la crearea pipe ului!\n"); return 1; }
pid_t pid = fork();
if(pid < 0) { printf("Eroare la crearea procesului\n"); }
else if(pid > 0)
{
close(channel[1]); // close pipe's writing end
// wait(NULL); // wait for children to finish
int status;
waitpid(pid, &status, 0); // wait for children to finish
int return_value = WEXITSTATUS(status);
char aux[BUFFER_SIZE];
read(channel[0], aux, BUFFER_SIZE);
close(channel[0]); // close pipe's reading end
printf("%s\n", aux);
//exit(0);
}
else
{
close(channel[0]); // inchid capatul de citire al pipe ului (in copil voi scrie in pipe)
system("date >> temp.txt"); /* I have eventually used an auxiliary file to get the output of `date`
command into it, then read it using f.
*/
FILE * fptr = fopen("temp.txt", "r");
if(fptr == NULL) { printf("eroare la crearea fisierului auxiliar\n"); exit(EXIT_FAILURE); }
char buffer[BUFFER_SIZE];
fgets(buffer, BUFFER_SIZE, fptr); // read date
fclose(fptr); // close file
remove("temp.txt"); // delete temporary file
buffer[strlen(buffer) - 1] = '\0'; // delete '\n' character from the end of read string (might need it without the '\n' later)
write(channel[1], buffer, sizeof(buffer)); // scriu in capatul de scriere al pipe ului data citita
exit(0); // succesfully leave child
}
return 0;
}
#包括
#包括
#包括
#包括
#包括
#包括
#定义缓冲区大小105
int main()
{
int通道[2];
如果(管道(通道)=-1){printf(“Eroare la crearea pipe ului!\n”);返回1;}
pid_t pid=fork();
如果(pid<0){printf(“Eroare la crearea procesului\n”);}
否则,如果(pid>0)
{
关闭(通道[1]);//关闭管道的写入端
//等待(NULL);//等待子项完成
智力状态;
waitpid(pid,&状态,0);//等待子项完成
int return_value=WEXITSTATUS(状态);
字符辅助[缓冲区大小];
读取(通道[0],辅助,缓冲区大小);
关闭(通道[0]);//关闭管道的读取端
printf(“%s\n”,aux);
//出口(0);
}
其他的
{
关闭(通道[0]);//inchid capatul de citire al pipe ului(管道中的副本)
系统(“date>>temp.txt”);/*我最终使用了一个辅助文件来获取“date”的输出`
命令,然后使用f读取它。
*/
文件*fptr=fopen(“temp.txt”、“r”);
如果(fptr==NULL){printf(“eroare la crearea fisierului auxiliar\n”);退出(退出失败)}
字符缓冲区[缓冲区大小];
fgets(缓冲区,缓冲区大小,fptr);//读取日期
fclose(fptr);//关闭文件
删除(“temp.txt”);//删除临时文件
buffer[strlen(buffer)-1]='\0';//从读取字符串的末尾删除'\n'字符(以后可能需要不带'\n'的字符)
写入(通道[1],缓冲区,大小(缓冲区));//在capatul de scriere al pipe ului data citita中写入
退出(0);//成功离开子级
}
返回0;
}
现在,我将使用
popen
,尝试实现可能更简单的方法;约瑟夫·西布尔也建议莫妮卡复职。你在找什么?嗨!谢谢你的评论!不,现在不行。我只能使用日期
。在我用date
解决它之后,我还将尝试使用popen
。有什么建议吗?你不能只使用C中的date
命令。你需要一些函数来启动它。你真的是说system
可以,但popen
不行吗?不,我不是这个意思。那你为什么在你问题的代码中使用system
?