如何将用逗号分隔的字符串中的字段解析为C中的整数数组? [英] How do I parse out the fields in a comma separated string into a intger array in C?

查看:137
本文介绍了如何将用逗号分隔的字符串中的字段解析为C中的整数数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将用逗号分隔的字符串中的字段解析为C中的Intiger数组?

输入文件的日期如下-

6
1,2,77
1,5,32
1,3,54
2,4,12
4,5,52
4,3,56
6,2,8
6,5,30
3,6,44

我需要将那些数据存储在数组[u] [v]中. u =第1列,v =第2列.该数组的值将成为每一行的第3个项目.请帮帮我

How do I parse out the fields in a comma separated string into a intiger array in C?

input file date is like below-

6
1,2,77
1,5,32
1,3,54
2,4,12
4,5,52
4,3,56
6,2,8
6,5,30
3,6,44

I need those data will be stored in a array[u][v]. u= 1st column, v = 2nd column. and value of this array will be 3rd item of each row. Please help me out

推荐答案

这实际上比听起来要难:您将不得不至少处理两次文件.:第一次发现您将需要的行数和列数,以及分配和填充它们的秒数.
但是,这有点作业的味道,所以没有代码!

解析非常简单:读取一行,转换数字,跳过逗号,转换数字,跳过逗号,转换数字.
然后,根据您的最大值检查那些值,并在最后分配数组.将数组预设为安全的空值(也许为-1).
再次重复做同样的事情,这次填补它.
That''s actually harder than it sounds: you are going to have to process the file twice at least.: First time to find out the number of rows and columns you will need, and the second to allocate and fill them.
But, this smells of homework, so no code!

Parsing is pretty easy: read a line, convert number, skip comma, convert number, skip comma, convert number.
You then check those against your maxima and allocate the array at the end. Preset the array with safe empty value (-1 perhaps).
The do the same thing all over again, and fill it this time.


这篇关于如何将用逗号分隔的字符串中的字段解析为C中的整数数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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