缓冲区中的newline,strcspn无法正常工作?
我是C的新手,所以我可能不会实施有关删除拖尾newlines href =“位于这里正确,但是我已经尝试了大约一个小时,但我仍然感到困惑。
这是基本程序,它来自A c编程绝对初学者指南。
4 main()
5 {
6 int ctr;
7 char c;
8 struct bookInfo books[3]; // array of three structure variables
9
10 // get info about each book from user
11 for (ctr = 0; ctr < 3; ctr++)
12 {
13 printf("What is the name of book #%d?\n", (ctr+1));
14 fgets(books[ctr].title, sizeof(books[ctr].title), stdin);
15 books[ctr].title[strcspn(books[ctr].title, "\r\n")] = 0;
16
17 puts("Who is the author? ");
18 fgets(books[ctr].author, sizeof(books[ctr].title), stdin);
19 books[ctr].author[strcspn(books[ctr].author, "\r\n")] = 0;
20
21 puts("How much did the book cost? ");
22 scanf(" $%f", &books[ctr].price);
23
24 puts("How many pages in the book? ");
25 scanf(" %d", &books[ctr].pages);
26 }
该书对第14和18行使用get()
,但是我已经做了足够的阅读以知道这不是很好,并且已将其替换为fgets()
。我觉得我误解了strcspn()
的工作方式,或者scanf()
如何与上一个代码的结果进行交互。
我上面引用过的链接的顶级答案专门使用此strcspn()
技术:
books [ctr] .title [strcspn(books [ctr] .title,'\ r \ n“)]] = 0;
as buffer [strcspn(buffer,“ \ n”)] = 0;
,
但显然我没有正确实现此功能。我想知道这是否与结构有关,但我怀疑。
我还尝试以这种方式捕获缓冲区中的额外输入:
3 printf("What is the name of book #%d?\n", (ctr+1));
14 fgets(books[ctr].title, sizeof(books[ctr].title), stdin);
15 getchar();
但是我得到了相同的行为。
这是我的输出:
What is the name of book #1?
hobbit
Who is the author?
tolkien
How much did the book cost?
14.99 <-- scanf() captures the newlines
How many pages in the book? <-- this scanf() gets "skipped"
What is the name of book #2?
Who is the author?
I am new to C, so I am likely not implementing the advice regarding removing trailing newlines located here correctly, but I have experimented for about an hour now and I'm still confused.
Here's the basic program, it's from a C Programming Absolute Beginner's Guide.
4 main()
5 {
6 int ctr;
7 char c;
8 struct bookInfo books[3]; // array of three structure variables
9
10 // get info about each book from user
11 for (ctr = 0; ctr < 3; ctr++)
12 {
13 printf("What is the name of book #%d?\n", (ctr+1));
14 fgets(books[ctr].title, sizeof(books[ctr].title), stdin);
15 books[ctr].title[strcspn(books[ctr].title, "\r\n")] = 0;
16
17 puts("Who is the author? ");
18 fgets(books[ctr].author, sizeof(books[ctr].title), stdin);
19 books[ctr].author[strcspn(books[ctr].author, "\r\n")] = 0;
20
21 puts("How much did the book cost? ");
22 scanf(" $%f", &books[ctr].price);
23
24 puts("How many pages in the book? ");
25 scanf(" %d", &books[ctr].pages);
26 }
The book uses gets()
for lines 14 and 18, but I have done enough reading to know that this is not good and have replaced it with fgets()
. I feel like I am misunderstanding how either strcspn()
works, or how scanf()
interacts with the results of the previous code.
The top-voted answer to the link I've referenced above specifically uses this strcspn()
technique:
books[ctr].title[strcspn(books[ctr].title, "\r\n")] = 0;
asbuffer[strcspn(buffer, "\n")] = 0;
but clearly I am not implementing this correctly. I wondered if it had something to do with the Struct, but I doubt that.
I also tried just capturing the extra input in the buffer in this way:
3 printf("What is the name of book #%d?\n", (ctr+1));
14 fgets(books[ctr].title, sizeof(books[ctr].title), stdin);
15 getchar();
but I get the same behavior.
Here's my output:
What is the name of book #1?
hobbit
Who is the author?
tolkien
How much did the book cost?
14.99 <-- scanf() captures the newlines
How many pages in the book? <-- this scanf() gets "skipped"
What is the name of book #2?
Who is the author?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@Gerhardh在评论中间接解决了我的问题。在我的
scanf()
中,该格式旨在匹配领先的$
符号,而当我实际运行该程序时,我忽略了其中的内容。经典隧道视觉。
@Gerhardh solved my problem indirectly in their comment. In my
scanf()
the format was meant to match a leading$
symbol, and I neglected to include that when I was actually running the program.Classic tunnel vision.