如何通过命令行参数传递参数将文本替换为文件的特定位置

我的目的是读取第一行中的第二个元素,并将其替换为我们作为命令行参数传递的值,并将其替换为input.txt文件中的值

输入文件:logic.txt

一个= 1234

2 = 3456

我希望在编译代码后我的文件可以像这样更改。

./a.out 1567

目前我正在获得这样的输出

./filelogic 1567

1567 = 1234

2 = 5678

在编译之后,应该像这样修改预期的输出文件

logic.txt

一个= 1567

2 = 5678

char buf[MAXSIZE] = {}; int num = 0; int i = 0; num = atoi(argv[1]); printf("%d",num); FILE *fp1; fp1 = fopen("logic.txt","r");//currently reading the file. if(fp1 != NULL) { fseek(fp1,3,0);//Need to Move the pointer to the 3rd position where i need to replace the num(fseek(fp1,??,0))-->how we can achieve that. //Using which method i can replace the num value into a file (means need to replace 1234 inplace of 1567) //Once the changes are done need to replace in the same file. fread(buf, 1, MAXSIZE, fp1); printf("%s\n",buf); fclose(fp1); }else { printf("Cannot open file""); exit(1); } 

有人可以指导我解决这个问题吗?先谢谢

可以 就地替换文件,但实际上不应该这样做。 如果您尝试替换字符并且未对文件中已存在的字符进行精确的一对一替换,则可能会损坏文件。

要安全地更改文件的内容,请将整个文件内容读入内存,进行所需的更改,然后截断当前文件并将新内容写入截断的文件。 (如果文件对于内存操作来说太大,那么使用临时文件)

您不希望使用atoi将字符串"1567"转换为整数。 您正在替换文件中的字符,而不是二进制文件中的整数值,因此请使用字符。

只想在第一个'='符号后面替换文本,您的项目很复杂。 这可能是也可能不在文件的第一行,因此您需要一些标志来指示何时找到第一个'='并进行替换。 (一旦完成替换,您可以简单地中断读取循环并关闭文件 – 但为了方便起见,在示例输出下面所有行)

每次在写入文件后关闭文件时,都应validationfclose返回以捕获任何流错误,或者在上一次写入时发生的错误,这些错误在下一次文件操作之前是不明显的。

考虑到这些注意事项和警告,您可以执行类似以下操作:

 #include  #include  #define MAXSIZE 64 /* max line/buffer size */ #define FNAME "logic.txt" /* default file name */ #define REPLACE "1567" /* default replacement text */ int main (int argc, char **argv) { char buf[MAXSIZE] = ""; /* line buffer */ const char *str = argc > 1 ? argv[1] : REPLACE; /* replacement string */ int replaced = 0; /* flag indicating replacement made */ FILE *fp = fopen (FNAME, "r+"); /* open file reading/writing */ if (!fp) { /* validate file open for reading/writing */ perror ("fopen-FNAME"); return 1; } while (fgets (buf, MAXSIZE, fp)) { /* read each line in file */ if (!replaced) { /* if replacement not yet made */ char *p = strchr (buf, '='); /* search for '=' in line */ if (p) { /* if found */ size_t plen = 0; /* var for length to end */ p++; /* advance past '=' sign */ plen = strlen (p); /* get length to end */ if (plen < strlen (str)) { /* check avail length */ fprintf (stderr, "error: not enough space in line.\n"); return 1; } strcpy (p, str); /* copy str to p */ if (fseek (fp, -plen, SEEK_CUR)) { /* backup plen chars */ perror ("fseek(fp)"); return 1; } fputs (p, fp); /* overwite contents with replacement */ replaced = 1; /* set flag indicating replacement */ } /* (you can break, and remove output */ } /* here if not writing to stdout) */ fputs (buf, stdout); /* output lines to stdout (optional) */ } if (fclose (fp) == EOF) /* always validate close-after-write */ perror ("fclose-FNAME"); return 0; } 

使用您的文件logic.txt作为示例输入,并命名可执行文件filelogic ,上面代码的使用和操作产生:

logic.txt文件之前

 $ cat logic.txt one=1234 two=3456 

示例使用/输出

 $ ./filelogic one=1567 two=3456 

logic.txt文件之后

 $ cat logic.txt one=1567 two=3456 

同样,这对于学习努力是好的,但在实践中,避免对文件进行就地更改,因为无意中文件损坏的风险远远超过使用更改编写新文件。