请问这个要怎么解决?编写一个 C 程序,它接受 3 个参数:一个文件名,整数范围的开始,以及整数范围的结尾;并创建一个包含指定整数的具有此名称的文件。如果给定的参数数量错误,或者无法创建文件,程序应该打印适当的错误消息。下面有机翻,还有一个示例供参考。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
FILE *output_stream = fopen("hello.txt", "w");
if (output_stream == NULL) {
perror("hello.txt");
return 1;
}
fprintf(output_stream, "Hello, Andrew!\n");
// fclose will flush data to file
// best to close file ASAP
// but doesn't matter as file autoamtically closed on exit
fclose(output_stream);
return 0;
}
代码如下,如有帮助,请帮忙采纳一下,谢谢。
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
int i,start,end;
char buf[20];
if(argc != 4)
{
printf("参数不满足要求"); //C语言参数包含程序名
return 0;
}
FILE *fp = fopen(argv[1],"w");
if (fp == 0)
{
printf("%s文件打开失败\n",argv[1]);
return 0;
}
start = atoi(argv[2]);
end = atoi(argv[3]);
for (i=start;i<=end;i++)
{
fprintf(fp,"%d\n",i);
}
fclose(fp);
return 0;
}