【问题标题】:Weird symbols when reading data from a file?从文件中读取数据时出现奇怪的符号?
【发布时间】:2013-08-27 05:30:14
【问题描述】:

我有一个程序旨在从 2 个单独的文本文件中获取数据,将它们放在数组中,将它们相互比较,然后输出某些结果。但是,当我从文件中读取数据并尝试显示数据时,在它最终显示文本文件中的所有数据之前,我得到了很多奇怪的符号。这是代码。

// Ch07-Exam Grader.cpp : Defines the entry point for the console application.
//

//Libraries
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;

//Prototypes
void initialization(void);
void proccess(void);
void eoj(void);
void writeIt(void);
void readIt(void);
void calculate(void);

//Global Variables
ifstream student;
ifstream correct;


int main()
{
initialization();

return 0;
}

//This function opens the files and calls the function to send the data into the array
void initialization (void){
correct.open("CorrectAnswers.txt");
readIt();

student.open("StudentAnswers.txt");
readIt();

}
void proccess (char c[], char s[], int length){

int correctCount = 0;
int incorrectCount = 0;

for (int i = 0; i< length; i++){
    if (s[i] == c[i]){
        correctCount = correctCount + 1;
    } else {
        incorrectCount = incorrectCount + 1;
    }
}

}
void eoj (void){

}
void writeIt (void){

}

//This function will take the data and place it into seperate arrays
void readIt (void){
char studentArray[20]; //Array to hold the student answers
char correctArray[20]; //Array to hold the correct answers

//Loops to place data to seperate arrays
for (int i = 0; !correct.eof(); i++){
    correct >> correctArray[i];
}
for (int j = 0; !student.eof(); j++){
    student >> studentArray[j];
}
    for (int i = 0; i < 20; i++){
    cout << studentArray[i] <<endl;
}
proccess(correctArray, studentArray, 20);

}
void calculate (void){

}

结果如下:

只有字母是文本文件的一部分。

【问题讨论】:

  • 显示至少一部分输入文件....请编译所有警告并学习使用调试器。
  • readIt()correctstudent 读取,但您在打开 student 之前调用它。
  • 多么简单的错误。非常感谢您的帮助。我已经修好了。

标签: c++ arrays text output


【解决方案1】:

为什么在initialization()函数中两次调用readIt()?看起来 readIt() 期望两个文件都打开,但是您打开第一个文件,调用 readIt(),打开第二个文件,再次调用 readIt()。可能是这个缺陷问题的原因?

【讨论】:

  • 是的。我已经修好了。非常感谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-18
  • 1970-01-01
  • 2019-10-24
  • 1970-01-01
相关资源
最近更新 更多