怎么采取多个字符串? [英] How to take multiple string ?

查看:112
本文介绍了怎么采取多个字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个数据库基础项目

考虑一个结构



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.


这篇关于怎么采取多个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆