[英]how to write a command line to a file in c
I'm having issues with writing this command line to a file and it's suppose to output to the screen. 我在将此命令行写入文件时遇到问题,并且应该输出到屏幕上。 To me, my code looks like it should work but I'm at a complete loss (this is my first time programming in C)
对我来说,我的代码看起来应该可以工作,但是我完全不知所措(这是我第一次使用C语言编程)
Print one line describing your program 打印一行描述您的程序
Open the first parameter as a file for writing. 打开第一个参数作为要写入的文件。 If no parameter is provided, write to the stdout handle
如果未提供任何参数,则写入标准输出句柄
Using a loop, save the contents of the array of string pointers passed as a parameter to the main function into the file open for writing. 使用循环,将作为参数传递给主函数的字符串指针数组的内容保存到打开的写入文件中。 This is usually the variable named argv.
这通常是名为argv的变量。
int main(int argc, char *argv[])
{
FILE *fp;
int i;
printf("Output supplying 'multiple arguments' to this program");
fp = fopen(argv[1], "w"); //Write to file
if(fp==NULL)
{
fp = stdout;
}
for(i=0;i<argc;i++)
{
fprintf(fp, argv[i]);
}
printf("The number of arguments printed %d", argc);
return 0;
Any help provided would be greatly appreciated! 提供的任何帮助将不胜感激!
Don't ever use dynamic format strings in C. This opens you up to an extensive set of bugs, several of them security-sensitive. 永远不要在C中使用动态格式字符串。这会给您带来大量的错误,其中一些对安全性敏感。 Instead, pass a format string that indicates your intent, like so:
而是传递表示您意图的格式字符串,如下所示:
for(i=0;i<argc;i++)
{
fprintf(fp, "%s\n", argv[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.