将信息放入列表中
我正在用C语言编程... 我有一个问题,
我有这个输入: LIS MAD 4 TP001 TP002 TP003 TP004
我需要做的是扫描所有信息并将其放入列表中...
问题是,TP 的数量是可变的,它可以从 1 到 1000...
请帮助我...我不知道如何做到这一点..
也许一个循环会有所帮助...我不知道。
我的问题仍然是 TP 的数量可变。其余的,我知道...
谢谢!!
I'm programming in C language...
I'm having a problem
I have this input:
LIS MAD 4 TP001 TP002 TP003 TP004
and what I need to do is to scan all of the information and put it in a list...
The thing is, the number of TP is variable, it can go from 1 to 1000...
Please help me... I have no ideia how to do this..
Maybe a cycle for will help... I don't know.
My problem remains with the variable number of TP's. The rest, I know...
Thanks!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要解释一下你在做什么。
这个输入法在哪里?
你只需要循环遍历
参数
尝试阅读:
You need to explain what you are doing.
Where is this input?
You simply need to loop through the
arguments
Trying reading:
根据您对 gskpurs 的回答的评论,看来您有一个变量许多行上以空格分隔的字符串的数量?
您需要首先使用
fgets
获取一行,然后使用sscanf
将每个单词获取为一一次,直到结束字符串(即行尾)。在进一步的评论中,您提到 LIS/MAD 之后的标记描述了它后面有多少个单词。好的,您的目标是执行以下操作:
fgets
读取一行。sscanf
)sscanf
读取一个整数,即要跟随的单词数。 (我们将此称为n
来进行讨论。)malloc
分配一个字符串数组(类型为char **
),元素数量为n
。n
次。如果您需要澄清,请告诉我。
这是一个快速示例:
Per your comment for gskspurs' answer, it seems you have a variable number of space-delimited strings on many lines?
You need to start by using
fgets
to get one line, and then use eithersscanf
to get each word one at a time, until the end of the string (i.e., the end of the line).In a further comment, you mentioned that the token after LIS/MAD describes how many words follow it. Okay, so your goal is to do the following:
fgets
.sscanf
)sscanf
to read into an integer, the number of words to follow. (Let's call thisn
for this discussion.)malloc
to allocate an array of strings (typechar **
), the number of elements beingn
.n
times.Let me know if you need clarification.
Here's a quick sample: