2016-11-04 9 views
-2

これまでのところ、ファイルを読み込み、その内容を配列に保存するだけのプログラムを作ろうとしています。 coutは単語が配列に保存されるかどうかを調べるテストでしたが、うまくいきませんでした。実行されると、空白を画面に表示し、最後にファイルの名前を表示します。ファイルからの配列の読み込みC++

#include <string.h> 
#include <stdlib.h> 
#include <stdio.h> 
#include <string> 
#include <iostream> 
#include <fstream> 
#include <streambuf> 
#include <ctime> 
#include <time.h> 
#define MAX 10000 

void readFile(fstream& wordFile, string words[], int &wordarrayLength) 
{ 
    string word; 
    int i=0; 

     while(i < MAX) 
     { 
      getline(wordFile,word); 
      words[i] = word; 
      i++; 
      cout << words[i] << endl; 
     } 

    wordarrayLength = i; 

    wordFile.close(); 
} 

int main() 
{ 

    string words[MAX]; 
    int arraylength; 
    fstream file ("words.txt", ios::in); 

    readFile(file,words,arraylength); 

} 
+1

注意:各行の行を 'words [i]'に保存し、 'i'をインクリメントして' words [i] 'の内容を出力します。あなたのバグを見つけ出すまで、前の文章を読んでください。 –

+0

なぜベクトルを使用しないのですか – Danh

+0

@SamVarshavchikコンソール行の出力を 'cout << word << endl;'に編集した結果、同じですが、ファイルパラメータを渡した方法だと思っていましたが、うまくいくようです。 – SaltyCode

答えて

-1

このような何か試してみてください:

//since you are updating the array pass it in by reference as well. 
void readFile(fstream& wordFile, string &words[], int &wordarrayLength) 
{ 
    int i=0; 

     while(i < MAX) 
     { 
      //each item in the array has to be it's own string 
      //the previous code simply reused the same string each time 

      string word; 
      getline(wordFile, word); 
      words[i] = word; 
      cout << words[i] << endl; 
      i++; 
     } 

    wordarrayLength = i; 

    wordFile.close(); 
} 

私はそれをデバッグすることはできませんので、残念ながら、私はあなたのファイルやコンパイラを表示できません。

0

コンパイラが探していたファイルがありませんでした。このコードは正常に動作します。

関連する問題