如何将每行文件读入数组 [英] How to read every line of file into array

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

问题描述

此程序显示名称。如果您键入的名称与要求输入的名称匹配,程序会告诉您名称的电话号码。问题是它只列出文件中的姓氏。如何让程序从文件中的第一个名称开始,输入正确后,显示名称编号并移到下一个名称?所有的帮助都会很优雅!!

This program display a names. If the name you type match the one asked to type, the program tells you the phone number of the name. The problem is it only list the last name in the file. How to get the program to start at the first name in the file, and after you type it correct, show name number and move to the next name? All help would be graceful!!

展开 | 选择 | Wrap | 行号

推荐答案

首先,总是在发布代码时使用代码标签。


添加这两个pragma并修复他们指出的问题。
First, ALWAYS use the code tags when posting your code.

Add these 2 pragmas and fix the problems they point out.
展开 | 选择 | Wrap | 行号


我相信你的文件正在进入你的数组,但是当你循环它时,你只是在保存最后一行。


循环将行拆分为罚款,但在值终止之前不对值进行任何操作。此时,值设置为在最后一行中找到的值。


顺便说一下 - 您不必将文件读入数组 - 您可以遍历您的< ;文件>这样:
I believe your file is getting into your array ok, but when you are looping through it, you are only saving the last row.

The loop splits the row fine, but does nothing with the values until it terminates. At that point the values are set to whatever was found in the last row.

Incidentally - you don''t have to read the file into an array - you can loop across your <file> this way:
展开 | 选择 | Wrap | 行号



顺便说一下 - 你没有将文件读入数组 - 您可以遍历< file>这样:


打开FILE," profile.txt" ||死不能打开File.txt:
Incidentally - you don''t have to read the file into an array - you can loop across your <file> this way:

open FILE, "profile.txt" || die "Can''t open File.txt:


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

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