将.txt文件中的char值读入数组C ++时出错

时间:2013-03-07 19:47:17

标签: c++ arrays fstream

我必须编写一个程序,将.txt文件中的单个char值读入数组。当我运行代码时,它会显示一堆奇怪的符号。

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;
}

3 个答案:

答案 0 :(得分:2)

您错误地打开了文件。

应该是

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

您必须转义\字符

答案 1 :(得分:0)

有些东西告诉我correctAnswers.open失败了,因为你使用的路径是错误的。请记住,在C和C ++中,您必须通过在字符串中键入\来转义\\字符。

这里有一个重要的教训:始终检查操作是否成功。 ifstream::is_open存在,并返回一个值的原因。 :)

答案 2 :(得分: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;
}