1

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

在此处输入图像描述

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

3 回答 3

2

您重新打开文件不正确。

它应该是

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

你必须逃避\角色

于 2013-03-07T19:49:57.503 回答
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;
}
于 2013-05-16T15:23:20.373 回答
0

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

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

于 2013-03-07T19:51:05.243 回答