2013-03-07 50 views
1

我必須編寫一個程序,它將.txt文件中的單個char值讀入數組中。當我運行代碼時,它會顯示一堆奇怪的符號。將.txt文件中的char值讀入數組時遇到錯誤C++

enter image description here

#include <iostream> 
#include <fstream> 
using namespace std; 

int main() 
{ 
const int NUM_ANS = 10; 
char answers[NUM_ANS], student[NUM_ANS]; 


ifstream correctAnswers; 
correctAnswers.open("C:\\Users\\RCLRC115\\Desktop\\student.txt"); 
int count = 0; 
while (count < NUM_ANS && correctAnswers >> answers[count]) 
    count++; 

for (int i = 0; i < NUM_ANS; i++) { 
    cout << answers[i] << endl; 
} 

cin.get(); 
return 0; 
} 
+0

可能不是錯誤,但你的第二個循環應該有條件'我 molbdnilo 2013-03-07 20:28:57

+0

不,因爲它不是嵌套循環,所以在循環計數和NUM_ANS都保持10作爲它們的值,並且我更喜歡使用NUM_ANS,因爲它將是我的程序中的一個常數。 – 2013-03-07 20:32:58

+0

如果文件找不到,或者包含垃圾,或者少於10個字符(您*意味着只讀10個字符,而不是10行?),那麼您的代碼將打印垃圾字符。更改第二個循環以使用'count'意味着您將只打印您讀入的內容。 – Roddy 2013-03-07 20:36:40

回答

2

你再錯誤地打開該文件。

應該

correctAnswers.open("C:\\Users\\RCLRC115\\Desktop\\student.txt"); 

你必須逃離\字符

+0

或更好,但只需使用正斜槓 – 2013-03-07 19:50:58

+0

+1。 Bless C++ 11及其原始字符串 – StoryTeller 2013-03-07 19:51:23

+0

我現在掃描所有\\字符,但仍顯示隨機符號 – 2013-03-07 19:57:30

0

有個聲音告訴我,correctAnswers.open因爲您使用的路徑是錯誤的失敗。請記住,在C和C++中,如果在字符串內部輸入\\,則必須跳過\字符。

這裏有一個重要的教訓:總是檢查操作是否成功。 ifstream::is_open存在並返回值的原因。 :)

+0

我用if(correctAnswers.is_open()),它顯示它沒有打開文件,我只是不明白爲什麼。我什至嘗試更改student.txt的路徑在項目文件夾內,這樣我就不必寫所有的路徑,但它仍然不打開文件:/ – 2013-03-07 20:15:00

0

我建議使用註釋和某種形式的錯誤捕獲。即使它只是顯示有錯誤的消息,以及錯誤是什麼。試試這對我有效:

#include <iostream> 
#include <fstream> 
using namespace std; 

int main() 
{ 
    const int NUM_ANS = 10; 
    char answers[NUM_ANS], student[NUM_ANS]; 

    // would recommend: 'ifstream fin;' because its shorter 
    ifstream correctAnswers; 

    // open the txt file 
    // ifstream.open(const char* Filename, std::ios_base::open_mode _Mode); 
    // use 'ios_base::in' for input, and 'ios_base::out' for output 
    // it tells the ifstream how to file should be opened. In this case for input. 
    correctAnswers.open("C:/Users/RCLRC115/Desktop/student.txt", ios_base::in); 

    // check that the last ifstream operation didn't fail. 
    if (correctAnswers.fail()) 
    { 
     // print error message 
     cout << "ERROR: Could not open file!"; 
     cin.get(); 

     // Return a value other than 0 so you'll know there was an error by looking 
     // at the output window 
     // The program '[#] programName.exe: Native' has exited with code 1 
     return 1; 
    } 
    else 
    { 
     // print success message 
     cout << "SUCCESS: File Opened! Reading File..." << '\n'; 
    } 

    int count = 0; 

    // 'while (count < NUM_ANS && correctAnswers << answers[count])' is where one error was 
    // 'while (count < NUM_ANS)' is correct. correctAnswers << answers[count] is telling it to store 
    // the read characters in answers[count], not checking if the character was read, and valid. 

    // Basically saying: 
    // 'while (count < NUM_ANS)' AND 'while (ifstream.good())' 
    // meaning the input file stream read a character and not the end of file. 
    while (count < NUM_ANS && correctAnswers.good()) 
    { 
     // this is where the other error was, it probably wasn't storing anything in answers[count] 
     // store the values read in answers[count] 
     correctAnswers >> answers[count]; 
     // increment count so loop will end 
     count++; 
    } 

    for (int i = 0; i < NUM_ANS; i++) 
    { 
     // print the value in the command window 
     cout << answers[i] << endl; 
    } 

    // Make sure to close files open with ifstream, ofstream, and iofstream 
    correctAnswers.close(); 

    // Wait to get input so window doesn't close 
    cin.get(); 

    // Return 0, program exited normally 
    return 0; 
}