简体   繁体   English

for循环,读取和写入文件的困难

[英]difficulty with for loops, reading from and writing to files

I am currently working on a project for my 210 class that involves a file from which I need to extract a full name, flight type, passenger type, membership level, number of bags, and the size/weight of those bags. 我目前正在为我210班的一个项目工作,该项目涉及一个文件,我需要从该文件中提取全名,航班类型,乘客类型,会员级别,行李数量以及这些行李的大小/重量。 It gets very complex with military rewards and mileage clubs, free bags and oversize bags, passenger type, etc... et... 军事奖励和里程俱乐部,免费行李箱和超大行李箱,乘客类型等变得非常复杂...等...

The file looks exactly like this: 该文件看起来完全像这样:

Mark Spitz E RP NO 2 21.5 24.2 18 6 30 26 20.5 7.5 
Michael Jordan B RP M4 3 53.7 14.1 9.2 15.0 24.2 5.2 9.3 16.2 109.2 12.1 9.6 23.0
Dorothy Hamill E RP NO 2 55.8 27.1 17.2 18.6 15.0 35.2 21.3 9.2

This indicates that Mark Spitz is a regular passenger traveling economy class and is not a member of the mileage club. 这表明马克·斯皮兹(Mark Spitz)是普通的旅客旅行经济舱,不是里程俱乐部的成员。 He is checking two bags: the first weighs 21.5 lbs., and is 24.2 in. long, 18 in. wide and 6 in. high; 他正在检查两个袋子:第一个袋子重21.5磅,长24.2英寸,宽18英寸,高6英寸。 the second weighs 30 lbs., and is 26 in. long, 20.5 in. wide and 7.5 in. high. 第二个重30磅,长26英寸,宽20.5英寸,高7.5英寸。

I have used for loops to read from infiles before, but never such as this. 我以前曾经使用过for循环来读取infile文件,但是从来没有像这样。 I have come to a complete stop and need some serious help with this issue, as I can't seem to find much on it. 我已经完全站住了,并且在此问题上需要一些认真的帮助,因为我似乎在此问题上找不到很多。 Any and all help is greatly appreciated! 任何帮助都将不胜感激! Here is my code as current: 这是我当前的代码:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <cmath>

using namespace std;

int main ()
{
int newline, firstname, lastname, flighttype, status, membership, numberofbags=0, regularpass, militarypass, militaryorder, member1, member2, member3, member4, nomember;

ifstream infile;
infile.open("data3.txt");

ofstream outfile;
outfile.open("charges.txt");

        infile >> newline;
        for(int a=0; a<newline; a++)
        {
            infile >> firstname  >> lastname;
            outfile <<  firstname  <<" " << setw(10) << left << lastname;
        }

return 0;
}

As stated before the wrong type is being used. 如前所述,使用了错误的类型。 You are reading strings into integer variables and that is not going to work. 您正在将字符串读取为整数变量,但这将无法正常工作。

for(int a=0; a<newline; a++)

The way you are looping thru the file does not make sense. 您遍历文件的方式没有意义。 See the reference below. 请参阅下面的参考。

ifstream reference ifstream参考

Also...You are not reading in all of the fields before trying to move to the next record. 另外...在尝试移至下一条记录之前,您并未阅读所有字段。 Your data shows some records with more fields than others. 您的数据显示的某些记录具有比其他记录更多的字段。 That would cause your technique to not work. 那会导致您的技术无效。 If the exercise is to read records of the same length than this is the correct code but if it is to read records with variable lengths than this code would not work. 如果练习要读取的长度与该长度相同的记录是正确的代码,但是如果要读取长度可变的记录,则此代码将不起作用。 You will need to use getline to do variable length records. 您将需要使用getline进行可变长度记录。

After seeing John's comment above I can see why the record lengths are variable. 看了上面约翰的评论后,我可以看到为什么记录长度是可变的。 You need to provide logic that will decide how many fields should be read. 您需要提供将决定应读取多少个字段的逻辑。 I think I can recall now this type of exercise in my 200 level cs classes. 我想我现在可以回想起我200级CS课中的这种练习。

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

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