C ++读取文本文件到结构的数组不工作 [英] c++ Reading text file into array of structs not working

查看:112
本文介绍了C ++读取文本文件到结构的数组不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在这一段时间,不能修复它。我是很新的C ++。到目前为止,我可以得到10的东西变成我的数组,但输出不清晰,它只是一堆数字。我已阅读其他职位有类似code,但由于某种原因,我的不工作。

I have been working on this for a while and can't fix it. I am very new to C++. So far I can get 10 things into my array but the output is not legible, it's just a bunch of numbers. I have read other posts with similar code but for some reason mine isn't working.

输入文本文件是10行假数据是这样的:

The input text file is 10 lines of fake data like this:

56790喜剧2012辛普森一家18.99 1结果
  56791恐怖2003午夜凶铃11.99 7

56790 "Comedy" 2012 "Simpsons" 18.99 1
56791 "Horror" 2003 "The Ring" 11.99 7

我的code是在这里:
(我的输出低于我的code)

My code is here: (My output is below my code)

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

struct DVD {
    int barcode;
    string type;
    int releaseDate;
    string name;
    float purchaseprice;
    int rentaltime;
    void printsize();
};

int main () {

ifstream in("textfile.exe");

DVD c[10]; 
int i; 

for (int i=0; i < 10; i++){
in >> c[i].barcode >> c[i].type >> c[i].releaseDate >>
c[i].name >> c[i].purchaseprice >> c[i].rentaltime;
}

for (int i=0;i< 10;i++) {
            cout << c[i].barcode<<" ";
            cout << c[i].type<<" ";
            cout << c[i].releaseDate<<" ";
            cout << c[i].name << " ";
      cout << c[i].purchaseprice << " ";
      cout << c[i].rentaltime << "\n";
        }
return 0;
}

我的输出类似于垃圾,但也有10行它像我的数组:
-876919876 -2144609536 -2.45e7 2046

My output looks similar to garbage, but there are 10 lines of it like my array: -876919876 -2144609536 -2.45e7 2046

在学什么修改我的code将AP preciated注释。

A comment on what to study to modify my code would be appreciated.

推荐答案

作为建议的cmbasnett,在(textfile.exe) ifstream的在可执行文件中读取。如果你与程序在一个文本文件中读取,在将其更改为 ifstream的(TextFile.txt的)应该工作。

As suggested by cmbasnett, ifstream in("textfile.exe") reads in an executable file. If you with for the program to read in a text file, changing it to ifstream in("textfile.txt") should work.

这篇关于C ++读取文本文件到结构的数组不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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