2016-11-25 6 views
-2

私はデータファイルをHondaという構造体配列に読み込む必要があるプロジェクトを持っています。これはデータの10行分のサイズです。テキストファイルの読み込みに問題があります。ここで私はこれまで持っているコード:C++の配列構造体を読み取る

#include "stdafx.h" 
#include <iostream> 
#include <string> 
#include <fstream> 
#include <iomanip> 

using namespace std; 

struct Honda { 
    int id; 
    int year; 
    string model; 
    string trim; 
    string color; 
    int engine; 

}; 

const int SIZE = 10; 

void openInputFile(ifstream &, string); 

int main() 
{ 
    Honda arr[SIZE]; 

    ifstream inFile; 
    string inFileName = "C:\\Users\\Michael\\Documents\\inventory.txt"; 

    openInputFile(inFile, inFileName); 

    for (int count = 0; count < SIZE; count++) { 
     inFile >> arr[count].id >> arr[count].year >> arr[count].trim >> arr[count].color >> arr[count].engine; 
    } 

    inFile.close(); 
    return 0; 
} 

void openInputFile(ifstream &inFile, string inFileName) 
{ 
    //Open the file 
    inFile.open(inFileName); 

    //Input validation 
    if (!inFile) 
    { 
     cout << "Error to open file." << endl; 
     cout << endl; 
     return; 
    } 
} 

テキストファイル:

1001 2014 Civic LX Red 4 
1002 2014 Accord LX Blue 4 
1005 2014 Accord EX Gold 6 
1006 2014 Civic EX Black 4 
1007 2014 Civic LX White 4 
1010 2015 Accord EX White 6 
1011 2015 Accord LX Black 4 
1013 2015 Civic EX Red 4 
1014 2015 Civic LX Beige 4 
1015 2015 Accord EX Beige 6 
+3

Cスタイルの配列を使用しないようにして、代わりに 'std :: vector'などを使用してください。あなたの魔法の限界は非常に恣意的です。 – tadman

+0

インターネットで「stackoverflow C++ read file space separated」を検索します。既に類似した投稿が多数あります。 –

+0

示されたコードは 'arr [count] .model'を読み込むことを忘れています。 –

答えて

1

inventory.txtあなただけの行毎にファイルを読み込むためのコードを欠落していました。

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

using namespace std; 

struct Honda { 
    int id; 
    int year; 
    string model; 
    string trim; 
    string color; 
    int engine; 

}; 

const int SIZE = 10; 

void openInputFile(ifstream &, string); 

int main() 
{ 
    Honda arr[SIZE]; 

    ifstream inFile; 
    string inFileName = "C:\\temp\\1.txt"; 

    openInputFile(inFile, inFileName); 

    int count = 0; 
    string line; 
    while(inFile.good() && (getline(inFile, line))) 
    { 
     istringstream iss(line); 
     iss >> arr[count].id >> arr[count].year >> arr[count].model >> arr[count].trim >> arr[count].color >> arr[count].engine; 

     count++; 
    } 



    for (int i=0; i < 10; i++) 
    { 
     std::cout << arr[i].id << " " << arr[i].year << " " << arr[i].model << " " << arr[i].trim << " " << arr[i].color << " " << arr[i].engine << "\n"; 

    } 

    inFile.close(); 
    return 0; 
} 

void openInputFile(ifstream &inFile, string inFileName) 
{ 
    //Open the file 
    inFile.open(inFileName); 

    //Input validation 
    if (!inFile) 
    { 
     cout << "Error to open file." << endl; 
     cout << endl; 
     return; 
    } 
} 
+0

@rafelgonzalez thxあなたがコードを実行したときにあなたの助けを借りてinventory.txtを取得しましたか? – jmike

+0

投稿したサンプルデータを使用していて、正しく表示されています。 –

+0

@rafelgonzalezなぜコンパイルしたらidkなぜファイルを開くのにエラーが出るのかわからない – jmike

関連する問題