Tag: 输入

读取C中的可变长度字符串用户输入

我试图读取可变长度的用户输入并执行一些操作(比如在字符串中搜索子字符串)。 问题是我不知道我的字符串有多大(文本很可能是3000-4000个字符)。 我附上了我尝试的示例代码和输出: char t[],p[]; int main(int argc, char** argv) { fflush(stdin); printf(” enter a string\n”); scanf(“%s”,t); printf(” enter a pattern\n”); scanf(“%s”,p); int m=strlen(t); int n =strlen(p); printf(” text is %s %d pattrn is %s %d \n”,t,m,p,n); return (EXIT_SUCCESS); } 输出是: enter a string bhavya enter a pattern av text is bav 3 pattrn is […]

使用fscanf()读取一行时遇到问题

我正在尝试使用以下代码读取一行: while(fscanf(f, “%[^\n\r]s”, cLine) != EOF ) { /* do something with cLine */ } 但不知怎的,我每次只得到第一行。 这是一条读线的坏方法吗? 我该怎么办才能让它按预期工作?

C getopt多值

我的论点是这样的 ./a.out -i file1 file2 file3 如何利用getopt()获取3个(或更多)输入文件? 我正在做这样的事情: while ((opt = getopt(argc, argv, “i:xyz..”))!= -1){ case ‘i’: input = optarg; break; … } 我只得到file1 ; 如何获取file2 , file3 ?

strstr不起作用

如果我输入“test”,为什么这段特殊的代码在strstr()上返回false? char input[100]; int main() { fgets(input, 100, stdin); printf(“%s”, input); if(strstr(“test message”, input)) { printf(“strstr true”); } } 我认为strstr在第一个参数中搜索了第二个参数的实例? 当我用一些文本替换输入或直接分配它时,它可以工作,但它似乎不适用于fgets。

在获取之前输入C. Scanf。 问题

我是C的新手,我在向程序输入数据时遇到了问题。 我的代码: #include #include #include int main(void) { int a; char b[20]; printf(“Input your ID: “); scanf(“%d”, &a); printf(“Input your name: “); gets(b); printf(“———“); printf(“Name: %s”, b); system(“pause”); return 0; } 它允许输入ID,但它只是跳过其余的输入。 如果我改变这样的顺序: printf(“Input your name: “); gets(b); printf(“Input your ID: “); scanf(“%d”, &a); 它会工作。 虽然,我不能改变秩序,我需要它原样。 有人能帮我吗 ? 也许我需要使用其他一些function。 谢谢!

如何使用scanf()扫描包含空格的字符串?

我想编写一个子程序,用户可以在其中输入注释。 我使用scanf(“%s”, X)并让它们输入注释,但它只能将字存储在字符串中的空格键之前。 如何将整个句子存储到字符串或文件中来解决此问题? 我的代码如下: FILE *fp; char comment[100]; fp=fopen(“comment.txt”,”a”); printf(“You can input your comment to our system or give opinion to the musics :\n”); scanf(“%s”,comment); fputs(comment,fp);

而((c = getc(file))!= EOF)循环不会停止执行

我无法弄清楚为什么我的while循环不起作用。 代码在没有它的情况下正常工作……代码的目的是在bin文件中查找秘密消息。 所以我得到了代码来找到这些字母,但现在当我试图让它循环到文件的末尾时,它不起作用。 我是新来的。 我究竟做错了什么? main(){ FILE* message; int i, start; long int size; char keep[1]; message = fopen(“c:\\myFiles\\Message.dat”, “rb”); if(message == NULL){ printf(“There was a problem reading the file. \n”); exit(-1); } //the first 4 bytes contain an int that tells how many subsequent bytes you can throw away fread(&start, sizeof(int), 1, message); printf(“%i […]

使用scanf()输入字符的问题

我正在尝试将一个角色输入一个链接列表,其中角色可以是’A’,’a’,’G’,’g’,’T’,’t’,’C’或’c’。 我还不熟悉C,我知道我搞砸了一些东西: do{ printf (“\nEnter a new nucleotide: \n”); scanf(“%c”,&newChar); /* Checking */ if(newChar == ‘A’ || newChar == ‘a’ || newChar == ‘G’ || newChar == ‘g’ || newChar == ‘T’ || newChar == ‘t’ || newChar == ‘C’ || newChar == ‘c’ ) { AddToSequence(newChar); size++; } else { printf (“\nBad Element”); } […]