如何读取文件并存储在数组中 [英] How can I read a file and store in a array

查看:87
本文介绍了如何读取文件并存储在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用以下定义如何读取文件并存储在数组中?

Using the below definition how can I read a file and store in an array?

int StoreDate(wchar_t* inFile, int num[], int maxSizeofArr)

我必须使用上面的签名来读取文件并存储在数组中。该文件将存储整数,例如以下示例:

I have to use the above signature to read a file and store in an array. The file will store integers like the example below:

12

41

74

45

65

14

74

我该怎么做?

请帮助我陷入困境。提前谢谢你:)

Please help I am stuck at this point. Thank You in Advance:)




推荐答案

第1步是打开你在参数inFile中收到名字的文件。 如果打开失败,则产生错误消息并向调用函数返回错误指示符。

Step 1 would be to open the file whose name you receive in parameter inFile.  If the open failed, produce an error message and return an error indicator to the calling function.

步骤2将重复执行以下以num [0]开头,直到您拥有迭代maxSizeofArr次:

    将inFile中的下一个值读入当前条目数量
    如果读取失败(IO错误或文件结束)

         过早退出此循环

    增加数组索引以使用num的下一个条目。

Step 2 would be to repeatedly perform the following starting with num[0] until you have iterated maxSizeofArr times:
     Read the next value from inFile into the current entry of num
     If the read failed (either IO error or end-of-file)
          Exit this loop prematurely
     Increment the array index to use the next entry of num.

步骤3将关闭文件。

步骤4将返回一个成功指标 (例如填充的数组元素的数量)到调用函数。

Step 4 would be to return a success indicator (such as the number of array elements populated) to the calling function.


这篇关于如何读取文件并存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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