繁体   English   中英

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

[英]Error reading char values from a .txt file into an array C++

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

在此处输入图片说明

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

您重新打开文件不正确。

它应该是

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

您必须转义\\字符

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

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

我建议使用注释和某种形式的错误捕获。 即使它仅显示一条消息,指出存在错误以及该错误是什么。 试试这个对我有用的:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM