C 如何编写程序,将输入文件中的字符与命令行中指定的字符交换?
我正试图编写一个程序来交换一个字符,我将在命令行上指定一个命令行参数和输入文本文件中的一个字符。第一个命令行参数是我想要更改的字符,第二个参数是我想要替换旧字符的字符,第三个参数是输入文件 当我这样做时,我的程序应该生成一个名为:translation.txt的输出文件。我知道我的程序的问题在if语句/fprintf语句中,但我不确定如何解决这个问题。我想分别读取输入文件中的每个字符,然后,我想使用if语句来确定是否替换该字符C 如何编写程序,将输入文件中的字符与命令行中指定的字符交换?,c,file,C,File,我正试图编写一个程序来交换一个字符,我将在命令行上指定一个命令行参数和输入文本文件中的一个字符。第一个命令行参数是我想要更改的字符,第二个参数是我想要替换旧字符的字符,第三个参数是输入文件 当我这样做时,我的程序应该生成一个名为:translation.txt的输出文件。我知道我的程序的问题在if语句/fprintf语句中,但我不确定如何解决这个问题。我想分别读取输入文件中的每个字符,然后,我想使用if语句来确定是否替换该字符 void replace_character(int arg_lis
void replace_character(int arg_list, char *arguments[])
{
FILE *input, *output;
input = fopen(arguments[3], "r");
output = fopen("translation.txt", "w");
if (input == NULL)
{
perror("Error: file cannot be opened\n");
}
for (int i = 0; i != EOF; i++)
{
if (input[i] == arguments[1])
{
fprintf(output, "%c\n", arguments[2]);
}
else
{
fprintf(output, "%c\n", arguments[1]);
}
}
}
int main(int argc, char *argv[])
{
if (argc < 5)
{
perror("Error!\n");
}
replace_character(argc, argv);
}
好的,我想这会有帮助:
#include <stdio.h>
int main(int argc, char** argv)
{
if (argc < 4) return -1; /* quit if argument list not there */
FILE* handle = fopen(argv[3], "r+"); /* open the file for reading and updating */
if (handle == NULL) return -1; /* if file not found quit */
char current_char = 0;
char to_replace = argv[1][0]; /* get the character to be replaced */
char replacement = argv[2][0]; /* get the replacing character */
while ((current_char = fgetc(handle)) != EOF) /* while it's not the end-of-file */
{ /* read a character at a time */
if (current_char == to_replace) /* if we've found our character */
{
fseek(handle, ftell(handle) - 1, SEEK_SET); /* set the position of the stream
one character back, this is done by
getting the current position using
ftell, subtracting one from it and
using fseek to set a new position */
fprintf(handle, "%c", replacement); /* write the new character at the new position */
}
}
fclose(handle); /* it's important to close the file_handle
when you're done with it to avoid memory leaks */
return 0;
}
给定指定为第一个参数的输入,它将查找要替换的字符,然后用替换中存储的字符替换它。试试看,如果不行就告诉我。我是这样运行的:
./a.out l a输入\ u trans.txt
我的文件只有字符串“你好,世界!”。运行此命令后,它将更改为“heao,Worad!”
仔细阅读和,因为它们是你需要做的关键
编辑:忘记添加在程序末尾关闭文件句柄的fclose语句。修正 好的,我想这会有帮助:
#include <stdio.h>
int main(int argc, char** argv)
{
if (argc < 4) return -1; /* quit if argument list not there */
FILE* handle = fopen(argv[3], "r+"); /* open the file for reading and updating */
if (handle == NULL) return -1; /* if file not found quit */
char current_char = 0;
char to_replace = argv[1][0]; /* get the character to be replaced */
char replacement = argv[2][0]; /* get the replacing character */
while ((current_char = fgetc(handle)) != EOF) /* while it's not the end-of-file */
{ /* read a character at a time */
if (current_char == to_replace) /* if we've found our character */
{
fseek(handle, ftell(handle) - 1, SEEK_SET); /* set the position of the stream
one character back, this is done by
getting the current position using
ftell, subtracting one from it and
using fseek to set a new position */
fprintf(handle, "%c", replacement); /* write the new character at the new position */
}
}
fclose(handle); /* it's important to close the file_handle
when you're done with it to avoid memory leaks */
return 0;
}
给定指定为第一个参数的输入,它将查找要替换的字符,然后用替换中存储的字符替换它。试试看,如果不行就告诉我。我是这样运行的:
./a.out l a输入\ u trans.txt
我的文件只有字符串“你好,世界!”。运行此命令后,它将更改为“heao,Worad!”
仔细阅读和,因为它们是你需要做的关键
编辑:忘记添加在程序末尾关闭文件句柄的fclose语句。修正 这不应该编译。无法为文件指针结构编制索引。是的,这是我遇到的问题之一:我想将每个字符与我的命令行参数进行比较,但我在如何进行比较方面遇到了问题。你实际上不知道如何读取C语言中的输入,是吗?我对处理文件非常陌生,因此,到目前为止,我所学到的只是打开文件进行读/写,在无法打开文件时打印错误消息,还有像这样简单的事情mmap,对吗?这似乎是最直接的方法。。一旦你开始工作:这很难。。但一旦成功,就非常容易了!这不应该编译。无法为文件指针结构编制索引。是的,这是我遇到的问题之一:我想将每个字符与我的命令行参数进行比较,但我在如何进行比较方面遇到了问题。你实际上不知道如何读取C语言中的输入,是吗?我对处理文件非常陌生,因此,到目前为止,我所学到的只是打开文件进行读/写,在无法打开文件时打印错误消息,还有像这样简单的事情mmap,对吗?这似乎是最直接的方法。。一旦你开始工作:这很难。。但一旦成功,就非常容易了!如果上面的代码中有不清楚的地方,我将非常乐意解释。非常感谢!不过有一件事,有没有办法不用ftell和fseek就可以做到这一点?我不知道,一旦你找到了你的角色,当你使用fprintf时,它会写入下一个角色,所以我们需要回溯一个角色来替换正确的角色。还有fgetpos和fsetpos,但我对它们不太熟悉,因为它们做的是相同的事情。对不起,还有一个问题,但如果我把它放在另一个函数中,而不是把所有内容都放在main中,我是否需要一个else语句来解释必须保持不变的字符?1。如果它是一个void函数,则不需要返回任何内容。无效退货与so-退货一样使用;只有当您需要比预期更早地退出函数时,例如,您发现了一个错误,无需继续。在您的情况下,我建议您将验证部分留给main,然后将其余部分放在void函数中。另外,您的意思可能是返回0;,不返回“/0”;由于这返回一个字符,并且它也是无效的,您正在考虑\0终止字符。如果上面的代码中有不清楚的地方,我将非常乐意解释。非常感谢!不过有一件事,有没有办法不用ftell和fseek就可以做到这一点?我不知道,一旦你找到了你的角色,当你使用fprintf时,它会写入下一个角色,所以我们需要回溯一个角色来替换正确的角色。还有fgetpos和fsetpos,但我对它们不太熟悉,因为它们做同样的事情。对不起,还有一个问题,但是如果我把它放在另一个函数中,而不是把所有的东西都放在main中,我是否需要一个else语句来解释必须保留的字符
e一样吗。如果它是一个void函数,则不需要返回任何内容。无效退货与so-退货一样使用;只有当您需要比预期更早地退出函数时,例如,您发现了一个错误,无需继续。在您的情况下,我建议您将验证部分留给main,然后将其余部分放在void函数中。另外,您的意思可能是返回0;,不返回“/0”;由于这将返回一个字符,而且它也是无效的,因此您认为\0是终止字符。