Search code examples
c++pointersfilestreamifstream

.txt File passed into ifstream turns contained data into garbage characters?


I'm trying to make a simple program for a comp sci class that goes through a list of data in a text file, and assigns it to two different arrays using pointer notation, but I'm running into an issue where the file I'm reading will be corrupted after running the program, but even when the program is terminated and restarted, it seems to still understand the data even though it only shows up as junk unicode/japanese characters when I open it up in a text reader like notepad after the fact? I'm not sure if this is an issue with my IDE or not, as I don't have any declarations to output to the file after it's read.

Here's what the text file looks like before running the program: https://pastebin.com/raw/JYww96RV

This is what it looks like after being ran: https://pastebin.com/raw/yLzDaAtj

This is what I have for code:

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

I apologize if I didn't do a justice to explaining this problem properly, but I'm a little bit stuck on where to go from here or how to properly find a fix online as I'm new to the language


Solution

  • It seems like it wasn't an issue with the code at all, but an issue with how windows notepad would first read the file in UTF-8, and after running the program, would switch over to UTF-16 LE as Avi alluded to in the comments.