简体   繁体   English

将 .txt 文件加载到 C 并将数据放入数组

[英]Loading a .txt file into C and putting data into an array

So I need to load a .txt file similar to what's written below, but could possibly be thousands long, then place them into a corresponding array.因此,我需要加载一个类似于下面所写的.txt文件,但可能长达数千,然后将它们放入相应的数组中。 I can load the file just fine, I'm just unsure how to select what line goes to what array.我可以很好地加载文件,我只是不确定如何选择哪一行进入什么数组。 (I should make it clear that the order after a command isn't consistent) (我应该明确命令后的顺序不一致)

1    
abd     
324   
1:is a command,
abd:is a name,
324:is an id

Any Help Would be Appreciated.任何帮助,将不胜感激。

A simple way is:一个简单的方法是:

  • Setup 2D array设置二维阵列
  • Loop through file until the end循环遍历文件直到结束
  • When you see the first line you add into command which will be array index 0当您看到添加到命令中的第一行时,该行将是数组索引 0
  • When you see the second line you add into name which will be array index 1当您看到第二行时,您将添加到名称中,该名称将是数组索引 1
  • When you see the third line you add into ID which will be array index 2当您看到第三行时,您将添加到 ID 中,这将是数组索引 2
  • Then you keep doing this until the end of the file然后你继续这样做直到文件结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM