gpt4 book ai didi

c++将文本文件读入结构数组不起作用

转载 作者:行者123 更新时间:2023-11-28 00:38:46 27 4
gpt4 key购买 nike

我已经为此工作了一段时间,但无法修复它。我对 C++ 很陌生。到目前为止,我可以将 10 个东西放入我的数组中,但输出不清晰,它只是一堆数字。我已阅读其他具有类似代码的帖子,但由于某种原因我的代码无法正常工作。

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

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

我的代码在这里:(我的输出在我的代码下面)

#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

如能评论修改我的代码要研究什么,我们将不胜感激。

最佳答案

根据 cmbasnett 的建议,ifstream in("textfile.exe") 读取可执行文件。如果您希望程序读取文本文件,将其更改为 ifstream in("textfile.txt") 应该可以。

关于c++将文本文件读入结构数组不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19869860/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com