.txt传递到ifstream的文件将包含的数据变成垃圾字符



我正试图为comp sci类制作一个简单的程序,该程序遍历文本文件中的数据列表,并使用指针表示法将其分配给两个不同的数组,但我遇到了一个问题,即我正在读取的文件在运行程序后会损坏,但即使程序终止并重新启动,它似乎仍然能理解数据,即使当我在记事本之类的文本阅读器中打开它时,它只显示为垃圾unicode/日语字符?我不确定这是否是我的IDE的问题,因为在读取文件后,我没有任何声明可以输出到文件中。

以下是运行程序前的文本文件:https://pastebin.com/raw/JYww96RV

这是运行后的样子:https://pastebin.com/raw/yLzDaAtj

这就是我的代码:

#include <iostream>
#include <iomanip>
#include <cstdlib>
using namespace std;
int readFile(int* &id, int* &group);
void sortArrays (int *userDataArray, int *identifierDataArray, int arraySize);
int binarySearch (int *userDataArray, int *identifierDataArray, int arraySize, int searchValue);
int main()
{
int *ids;
int *groups;
int sizes;

sizes = readFile(ids, groups);
for (int i = 0; i < sizes; i++)
{
cout << *(groups + i) << " " << *(ids + i) << endl;
}
cout << endl;
delete[] ids;
delete[] groups;
return 0;
}
int readFile(int* &id, int* &group)
{
ifstream userData; // We're going to start by declaring our data stream 'userData'
userData.open("data.txt"); // Our data stream is now going to open and associate itself with the 'data.txt' file
if (!userData) // This is a simple check if the file was properly found, if it wasn't, the error message below will be displayed
{
cout << "Error reading file! Make sure your data file is named 'data.txt'";
exit(1);
}
int sizes;
userData >> sizes;
id = new int[sizes];
group = new int[sizes];
for (int i = 0; i < sizes; i++) userData >> *(id + i) >> *(group + i);
userData.close();
return sizes;
}

如果我没有正确解释这个问题,我很抱歉,但我有点纠结于从这里开始该怎么做,或者如何在网上正确找到解决方案,因为我是语言的新手

这似乎根本不是代码的问题,而是windows记事本如何首先以UTF-8读取文件,并在运行程序后切换到UTF-16 LE的问题,正如Avi在评论中提到的那样。

最新更新