gpt4 book ai didi

c - 自动交互编程

转载 作者:太空宇宙 更新时间:2023-11-04 03:48:53 25 4
gpt4 key购买 nike

由于英语不是我的母语,对于任何拼写或语法错误,我提前道歉。

我是 HTML 的新手,我编写了一个 C 程序来自动执行重复编码(创建按钮剧透,询问隐藏内容以及每次在按钮上写的内容)

问题是我被强制转换所有我想用十六进制写的文本(char数组),因为它包含一些C语言的保留字符......

该程序运行流畅,符合我目前的需要,但我想知道除了将所有内容都转换为十六进制之外是否还有其他方法。

另外,gcc 给了我两次这个警告:

format ‘%s’ expects argument of type ‘char ’, but argument 2 has type ‘char ()[50]’ [-Wformat]

在这些行之后:

scanf("%s", &hidden_text);

(hidden_​​text 是一个 char 数组,初始化如下:

char hidden_text[50];)

如何修复该警告?将 %50s 传递给 scanf 无效(程序运行但此警告困扰我,如果有人告诉我解决方法,我将很高兴)

如果有人看到升级该程序的方法或有任何提示,请帮助我这个新程序员。(仅作记录,我使用的是 linux,Emacs 是我的 IDE,我使用 gcc 进行编译)

这是源代码(希望我不会因为发布太多文字而被举报):

#include<stdlib.h>
#include<stdio.h>
#include<string.h> /*strlen use this*/
int number_of_spoilers=10; /* the number of spoiler buttons you want to create (changed that value so you can test fast)*/
int a=0;
int spoiler=0; /*the spoiler Id I need to pass between each part*/
char part0[14]="\x3c\x68\x74\x6d\x6c\x3e\x0d\x0a\x3c\x62\x6f\x64\x79\x3e";
char part1[9]="\x3c\x64\x69\x76\x20\x69\x64\x3d\x22";
char part2[23]="\x22\x20\x73\x74\x79\x6c\x65\x3d\x22\x64\x69\x73\x70\x6c\x61\x79\x3a\x6e\x6f\x6e\x65\x22\x3e";
char hidden_text[50];
char part3[100]="\x3c\x2f\x64\x69\x76\x3e\x3c\x62\x75\x74\x74\x6f\x6e\x20\x74\x69\x74\x6c\x65\x3d\x22\x43\x6c\x69\x63\x6b\x20\x74\x6f\x20\x73\x68\x6f\x77\x2f\x68\x69\x64\x65\x20\x63\x6f\x6e\x74\x65\x6e\x74\x22\x20\x74\x79\x70\x65\x3d\x22\x62\x75\x74\x74\x6f\x6e\x22\x20\x6f\x6e\x63\x6c\x69\x63\x6b\x3d\x22\x69\x66\x28\x64\x6f\x63\x75\x6d\x65\x6e\x74\x2e\x67\x65\x74\x45\x6c\x65\x6d\x65\x6e\x74\x42\x79\x49\x64\x28\x27";
char part4[53]="\x27\x29\x20\x2e\x73\x74\x79\x6c\x65\x2e\x64\x69\x73\x70\x6c\x61\x79\x3d\x3d\x27\x6e\x6f\x6e\x65\x27\x29\x20\x7b\x64\x6f\x63\x75\x6d\x65\x6e\x74\x2e\x67\x65\x74\x45\x6c\x65\x6d\x65\x6e\x74\x42\x79\x49\x64\x28\x27";
char part5[51]="\x27\x29\x20\x2e\x73\x74\x79\x6c\x65\x2e\x64\x69\x73\x70\x6c\x61\x79\x3d\x27\x27\x7d\x65\x6c\x73\x65\x7b\x64\x6f\x63\x75\x6d\x65\x6e\x74\x2e\x67\x65\x74\x45\x6c\x65\x6d\x65\x6e\x74\x42\x79\x49\x64\x28\x27";
char part6[27]="\x27\x29\x20\x2e\x73\x74\x79\x6c\x65\x2e\x64\x69\x73\x70\x6c\x61\x79\x3d\x27\x6e\x6f\x6e\x65\x27\x7d\x22\x3e";
char button_text[50];
char part7[9]="\x3c\x2f\x62\x75\x74\x74\x6f\x6e\x3e";
char part_end[16]="\x3c\x2f\x68\x74\x6d\x6c\x3e\x0d\x0a\x3c\x2f\x62\x6f\x64\x79\x3e";
FILE *f=NULL;
main(FILE *f)
{
f=fopen("/home/Spoilers.html","a");
if(f==NULL)
printf("Can't open file");
else{
fwrite(part0, sizeof(char), 14,f);
while(a!=number_of_spoilers){
printf("%s","button text ? :");
scanf("%s", &button_text);

printf("%s", "hidden text ? :");
scanf("%s", &hidden_text);

fwrite(part1, sizeof(char), 9,f);
char hex[5];
sprintf(hex, "%x", spoiler); /*change the spoilerId value to hexa*/
fwrite(&hex, sizeof(char), strlen(hex),f);

fwrite(part2, sizeof(char), 23,f);
fwrite(hidden_text, sizeof(char), strlen(hidden_text),f);

fwrite(part3, sizeof(char), 100,f);
fwrite(&hex, sizeof(char), strlen(hex),f);

fwrite(part4, sizeof(char), 53,f);
fwrite(&hex, sizeof(char), strlen(hex),f);

fwrite(part5, sizeof(char), 51,f);
fwrite(&hex, sizeof(char), strlen(hex),f);

fwrite(part6, sizeof(char), 27,f);
fwrite(button_text, sizeof(char), strlen(button_text),f);

fwrite(part7, sizeof(char), 9, f);

a=a+1; spoiler=spoiler+1;
}
fwrite(part_end, sizeof(char), 16,f); /*contain "</body></html>"*/
fclose(f);
}
}

提前致谢,祝您度过愉快的一天(或一晚)!!

最佳答案

如果您使用 scanf( "%s", ... ),expcected 参数是一个 char * 因为它只需要将输入复制到的地址.如果声明 char hidden_​​text[50];,则 hidden_​​text 已经是指向该数组的(常量)指针。所以你必须打电话

scanf( "%s", hidden_text );

不是

scanf( "%s", &hidden_text );

更好的方法是调用

scanf( "%49s", hidden_text );

防止程序在输入太长时崩溃(注意格式字符串中的长度不包括终止符 '\0')。

关于c - 自动交互编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22229895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com