将输入字符串传递给 C 中的函数
Passing input string to a function in C
我想实现以下目标:
我必须在具有以下结构的文件(例如 txt)中管理数据库:X,X,X,X,X
在这个数据库中,我希望能够修改一行,当我在代码中编写要更改的行时,它可以完美地工作。
但是我希望能够在 运行 程序期间编写该行。
重要提示:当您写入一行值时,您必须放置一个 .到最后不过如此。不会进入文件。
另外,您不知道输入的时间有多长。
这有效:
在 Main 中,值是预先给定的:
char file[50];
scanf("%s",file);
char tmp[]="temp.txt";
char a[]="2,2,2,2,2";
char b[]="9,9,9,9,9";
javitas(file,tmp,a,b); // this will modify the line a to b in the file
程序:
#define sorhossz 500
void javitas(char filename1[], char filename2[], const char *str, const char *replace) {
FILE *in = fopen(filename1,"r+");
FILE *out = fopen(filename2,"w+");
size_t strl;
char line[sorhossz];
strl = strlen(str);
while (fgets(line, sizeof(line), in)) {
char *y, *x = line;
while ((y = strstr(x, str))) {
fwrite(x, 1, y - x, out);
fputs(replace, out);
x = y + strl;
}
fputs(x, out);
}
rewind(out);
rewind(in);
while (fgets(line, sizeof(line), out))
{
char *x = line;
fputs(x, in);
}
fclose(in);
fclose(out);
}
但我想在 运行 期间给 'a' 和 'b' 以某种方式在 Main:
char a[50]; // 50 is a random number I was triing with
char b[50];
printf("Give the line you want to modify\n");
scanf("%[^.]%*c",a);
printf("Give the line you want to modify to\n");
scanf("%[^.]%*c",b);
javitas(file,tmp,a,b);
当我尝试从输入中给出值时,我写“2,2,2,2,2”。对于 a 和“9,9,9,9,9”。对于 b。不要忘记 .没有进入字符串。之后,我可以在过程中打印这些字符串,所以我猜过程理解输入值,但它不会将文件中的 'a' 行修改为 'b' 行。
你能帮忙吗?
这是我正在测试的文件:(我将其命名为 qqq.txt 但没关系)
9,9,9,9,9
hel,az,de,5,k
0
ennyi,annyi,amannyi,helo,ot 22
1,2,3,4,5
1,2,2,2,3
ez,az vegre, sikerult, 3 sort illeszteni, a fileba
peti,egy,kiraly,helo,5om, 5 helo 5, 5 ezaz
egy,ketto,harom,negy,ot
helo,helo,lusta,vagyok,irni
meg,egyszer,hatha,most,sikerul
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
1,1,1,1,1
2,2,2,3,3
helo,belo,ize,de,4
如果文本文件不存在,它将在菜单之前创建。
很抱歉到处都是匈牙利语,但我希望主要的事情是可以理解的。
when i try to give the values from the input, i write "2,2,2,2,2."
for a
评论scanf("%[^.]%*c",a);
这和 之前的 scanf()
调用不消耗输入行的尾随 '\n'
,因此在这种情况下 a
变为 "\n2,2,2,2,2"
.
一个简单的修复方法是 scanf(" %[^.]%*c",a);
(注意 space)消耗前导白色-spaces,如 '\n'
.
更好的代码会使用宽度限制,扫描 '.'
并检查结果,也许还有 '\n'
char a[50];
if (scanf(" %49[^.].%*1[\n]",a) == 1) Success(); else Fail();
更健壮的代码根本不会使用 scanf()
,而是使用 fgets()
读取用户输入 line,然后处理 string .
可能存在其他问题。
我想实现以下目标: 我必须在具有以下结构的文件(例如 txt)中管理数据库:X,X,X,X,X 在这个数据库中,我希望能够修改一行,当我在代码中编写要更改的行时,它可以完美地工作。 但是我希望能够在 运行 程序期间编写该行。 重要提示:当您写入一行值时,您必须放置一个 .到最后不过如此。不会进入文件。 另外,您不知道输入的时间有多长。 这有效: 在 Main 中,值是预先给定的:
char file[50];
scanf("%s",file);
char tmp[]="temp.txt";
char a[]="2,2,2,2,2";
char b[]="9,9,9,9,9";
javitas(file,tmp,a,b); // this will modify the line a to b in the file
程序:
#define sorhossz 500
void javitas(char filename1[], char filename2[], const char *str, const char *replace) {
FILE *in = fopen(filename1,"r+");
FILE *out = fopen(filename2,"w+");
size_t strl;
char line[sorhossz];
strl = strlen(str);
while (fgets(line, sizeof(line), in)) {
char *y, *x = line;
while ((y = strstr(x, str))) {
fwrite(x, 1, y - x, out);
fputs(replace, out);
x = y + strl;
}
fputs(x, out);
}
rewind(out);
rewind(in);
while (fgets(line, sizeof(line), out))
{
char *x = line;
fputs(x, in);
}
fclose(in);
fclose(out);
}
但我想在 运行 期间给 'a' 和 'b' 以某种方式在 Main:
char a[50]; // 50 is a random number I was triing with
char b[50];
printf("Give the line you want to modify\n");
scanf("%[^.]%*c",a);
printf("Give the line you want to modify to\n");
scanf("%[^.]%*c",b);
javitas(file,tmp,a,b);
当我尝试从输入中给出值时,我写“2,2,2,2,2”。对于 a 和“9,9,9,9,9”。对于 b。不要忘记 .没有进入字符串。之后,我可以在过程中打印这些字符串,所以我猜过程理解输入值,但它不会将文件中的 'a' 行修改为 'b' 行。 你能帮忙吗?
这是我正在测试的文件:(我将其命名为 qqq.txt 但没关系)
9,9,9,9,9
hel,az,de,5,k
0
ennyi,annyi,amannyi,helo,ot 22
1,2,3,4,5
1,2,2,2,3
ez,az vegre, sikerult, 3 sort illeszteni, a fileba
peti,egy,kiraly,helo,5om, 5 helo 5, 5 ezaz
egy,ketto,harom,negy,ot
helo,helo,lusta,vagyok,irni
meg,egyszer,hatha,most,sikerul
1,2,3,4,5
1,2,3,4,5
1,2,3,4,5
1,1,1,1,1
2,2,2,3,3
helo,belo,ize,de,4
如果文本文件不存在,它将在菜单之前创建。 很抱歉到处都是匈牙利语,但我希望主要的事情是可以理解的。
when i try to give the values from the input, i write
"2,2,2,2,2."
fora
评论scanf("%[^.]%*c",a);
这和 之前的 scanf()
调用不消耗输入行的尾随 '\n'
,因此在这种情况下 a
变为 "\n2,2,2,2,2"
.
一个简单的修复方法是 scanf(" %[^.]%*c",a);
(注意 space)消耗前导白色-spaces,如 '\n'
.
更好的代码会使用宽度限制,扫描 '.'
并检查结果,也许还有 '\n'
char a[50];
if (scanf(" %49[^.].%*1[\n]",a) == 1) Success(); else Fail();
更健壮的代码根本不会使用 scanf()
,而是使用 fgets()
读取用户输入 line,然后处理 string .
可能存在其他问题。