在c中使用命令行中的标志读取和写入文件
我的程序需要解析命令行参数,然后读取一个文件的内容和/或将这些内容写入另一个文件。用户必须使用标志输入命令,其中i代表读取文件,o代表写入,如下所示:在c中使用命令行中的标志读取和写入文件,c,C,我的程序需要解析命令行参数,然后读取一个文件的内容和/或将这些内容写入另一个文件。用户必须使用标志输入命令,其中i代表读取文件,o代表写入,如下所示: ./program -isample -ofile 错误: warning: passing argument 1 of ‘fgets’ from incompatible pointer type warning: passing argument 1 of ‘fputc’ makes integer from pointer without
./program -isample -ofile
错误:
warning: passing argument 1 of ‘fgets’ from incompatible pointer type
warning: passing argument 1 of ‘fputc’ makes integer from pointer without a cast
任何帮助都将不胜感激
#include <stdio.h>
#include <unistd.h>
int main (int argc, char *argv[])
{
int opt = 0;
FILE *file = NULL;
FILE *text = NULL;
char ch[200];
while ((opt = getopt(argc, argv, "i:o:")) != -1)
{
switch(opt)
{
case 'i':
{
file = fopen(argv[1], "r");
while(fgets(ch, 200, file) != EOF)
{
printf("%s\n",ch);
}
fclose(file);
}
break;
case 'o':
{
file = fopen(argv[1], "r");
text = fopen(argv[2], "w");
do
{
ch = fgets(file);
fputc(ch, text);
}while (ch != EOF);
fclose(file);
fclose(text);
}
break;
case '?':
{
if (optopt == 'i')
{
printf("\nMissing mandatory input option");
}
else if (optopt == 'o')
{
printf("\nMissing mandatory output option");
}
else
{
printf("\nInvalid option received");
}
}
break;
}
}
printf("\n");
return 0;
}
#包括
#包括
int main(int argc,char*argv[])
{
int opt=0;
FILE*FILE=NULL;
文件*text=NULL;
char-ch[200];
while((opt=getopt(argc,argv,“i:o:”)!=-1)
{
开关(opt)
{
案例“i”:
{
file=fopen(argv[1],“r”);
while(fgets(ch,200,file)!=EOF)
{
printf(“%s\n”,ch);
}
fclose(文件);
}
打破
案例“o”:
{
file=fopen(argv[1],“r”);
text=fopen(argv[2],“w”);
做
{
ch=fgets(文件);
fputc(ch,text);
}while(ch!=EOF);
fclose(文件);
fclose(文本);
}
打破
案例“?”:
{
如果(optopt='i')
{
printf(“\n缺少强制输入选项”);
}
否则如果(optopt='o')
{
printf(“\n缺少强制输出选项”);
}
其他的
{
printf(“\n收到无效选项”);
}
}
打破
}
}
printf(“\n”);
返回0;
}
您的程序有几个问题:
- 您应该在复制之前解析所有参数:在
选项之后可能有一个-i
选项-o
- 文件名应为
optarg
在文件末尾返回fgets
NULL
将复制printf
,您应该使用\n
fputs
不占用缓冲区fputc
接受3个参数fgets
#include <stdio.h>
#include <unistd.h>
int main(int argc, char *argv[]) {
char *infile = NULL;
char *outfile = NULL;
FILE *file = stdin;
FILE *text = stdout;
char ch[200];
while ((opt = getopt(argc, argv, "i:o:")) != -1) {
switch (opt) {
case 'i':
infile = optarg;
break;
case 'o':
outfile = optarg;
break;
case '?':
if (optopt == 'i') {
fprintf(stderr, "Missing mandatory input option\n");
} else
if (optopt == 'o') {
fprintf(stderr, "Missing mandatory output option\n");
} else {
fprintf(stderr, "Invalid option received\n");
}
break;
}
}
if (infile) {
file = fopen(infile, "r");
if (file == NULL) {
fprintf(stderr, "cannot open input file %s: %s\n",
infile, strerror(errno));
exit(1);
}
}
if (outfile) {
text = fopen(outfile, "r");
if (text == NULL) {
fprintf(stderr, "cannot open output file %s: %s\n",
outfile, strerror(errno));
exit(1);
}
}
while (fgets(ch, 200, file) != NULL) {
fputs(ch, text);
}
fclose(file);
fclose(text);
return 0;
}
#包括
#包括
int main(int argc,char*argv[]){
char*infle=NULL;
char*outfile=NULL;
FILE*FILE=stdin;
文件*text=stdout;
char-ch[200];
while((opt=getopt(argc,argv,“i:o:”)!=-1){
开关(opt){
案例“i”:
infle=optarg;
打破
案例“o”:
输出文件=optarg;
打破
案例“?”:
如果(optopt='i'){
fprintf(stderr,“缺少强制输入选项”);
}否则
如果(optopt='o'){
fprintf(stderr,“缺少强制输出选项”);
}否则{
fprintf(stderr,“接收到无效选项\n”);
}
打破
}
}
如果(填充){
文件=fopen(填充“r”);
if(file==NULL){
fprintf(stderr,“无法打开输入文件%s:%s\n”,
因维、斯特罗(埃尔诺));
出口(1);
}
}
如果(输出文件){
text=fopen(输出文件,“r”);
if(text==NULL){
fprintf(stderr,“无法打开输出文件%s:%s\n”,
输出文件,strerror(errno));
出口(1);
}
}
while(fgets(ch,200,file)!=NULL){
fput(ch,text);
}
fclose(文件);
fclose(文本);
返回0;
}
您不认为应该告诉我们错误到底是什么吗?请包含准确的错误消息。下次要记住这一点。但您的错误是因为您将char*
传递给fputc
而不是char
。您也不能执行ch=fgets(文件)代码>ch
是一个数组,其值(与其内容相反)无法修改。请勿在getopts
循环中读取和写入文件。使用getopts
设置输入和输出的文件名变量。然后在你处理完这些论点后,打开两个文件,然后使用循环从一个文件读写另一个文件。“我感觉不舒服。我去看医生,他告诉我一些胡言乱语。你能给我一些我需要的药来改善病情吗?”@kaylum错误是:从不兼容的指针类型中传递参数“fgets”的1,并且传递参数“fputc”的1使指针中的整数不带强制转换。对不起,下次我会把错误放在这里。谢谢你的回复@Barmar让getop循环获取文件,打开它们,然后循环读取和写入它们。谢谢