怎么采取多个字符串?

编程入门 行业动态 更新时间:2024-10-24 10:23:29
本文介绍了怎么采取多个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在开发一个数据库基础项目 考虑一个结构

i am working on a database basic project consider a structure

struct student { int rollno; char full_name[20]; char address[50]; char birthmark[50]; };

如何获取此输入 rollno 6 名字john snow 地址winterfell of north 胎记剑全部伤害 什么我试过了: i尝试过scanf(%s,stringname),scanf(%[^ \ n] s,stringname ),获取(stringname), fgets(缓冲区,大小,标准差)与fflush stdin。 总有一些错误。 so far fgets工作得很好但是我读过ffush(stdin)是一种错误的做法。 所以应该同时输入字符串(带空格)的方法在另一个之后

how to take this input rollno 6 name john snow address winterfell of north birthmark swords wound all over What I have tried: i have tried scanf("%s",stringname),scanf("%[^\n]s",stringname),gets(stringname), fgets(buffer,size,stdin) with fflush stdin. there are always some error. so far fgets work perfectly but i have read ffush(stdin) is a wrong practice . so what should be the approach to take input of string (with spaces) simultaneously one after the another

推荐答案

这取决于你将如何读取它。显而易见的选择是一系列提示,然后使用进行读取获取/与fgets 。将每个字符串读入一般工作缓冲区,以便在将值存储到结构中之前检查有效输入并进行任何必要的转换。 In您在上面显示的示例输入,您可以以相同的方式读取每一行(使用 gets / fgets )然后将其拆分为第一个空格字符以分隔变量名称来自实际数据。 It depends how you are going to read it in. The obvious choice would be a series of prompts followed by reads using gets/fgets. Read each string into a general work buffer so you can then check for valid input and do any conversions that may be necessary, before storing the values into your structure. In the sample input that you show above, you can just read each line the same way (using gets/fgets) and then split it at the first space character to separate the variable name from the actual data.

更多推荐

怎么采取多个字符串?

本文发布于:2023-08-07 13:54:51,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   字符串

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!