0

I absolutely cannot understand where the extra characters at the beginning of the read line come from.

Main:

int main(){
    DynamicMatrix Matrix1;
    ifstream f1("matrix.txt", ios::in);
    f1 >> Matrix1;
    cout << Matrix1;
    f1.close();
    return 0;
}

overloaded operator in the class:

ifstream& operator>> (ifstream& ifs, DynamicMatrix &matrix){
try{

    size_t *OldRows = new size_t;
    *OldRows = matrix.rows;
    
    if (!ifs.is_open()) throw DynMatrixException("Unable to read this file.");

    char* loaded = new char[1000];

    size_t temp_rows = 0;
    size_t temp_columns = 0;
    int n = 0;
    while (!ifs.eof()) ifs.read(&loaded[n++], sizeof(char));
    loaded[n]='\0';
    //other code
}
catch...

1st screenshot

text file

2nd screenshot

result line

kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • What does `valgrind` say? – Andrej Podzimek Nov 12 '21 at 05:46
  • 1
    Side note: `while (!ifs.eof()) ...` [is an antipattern](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – Andrej Podzimek Nov 12 '21 at 05:49
  • We do not know the implementation of this DynamicMatrix? For that its hard to say why there are magic numbers. Could it be the these numbers are just line numbers from the output window? Which environment do you use? – MiniMik Nov 12 '21 at 06:43
  • I'm using VS Code. I am not sure if it could be line numbers because it's a txt file without these numbers. – bluzord Nov 12 '21 at 08:16

1 Answers1

0

You don't need to use pointers to store rows:

size_t *OldRows = new size_t;
*OldRows = matrix.rows;

Simply, this would work:

size_t oldRows = matrix.rows;

For reading through file, you can use std::stringstream and std::getline to read line by line and store it.

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    ifstream in("sample.txt");
    if (!in.good())
    {
        cout << "Could not open file\n";
        return 1;
    }
    
    string line;
    stringstream ss;
    
    while (getline(in, line))
    {
        ss << line << endl;
    }
    
    string whole_file = ss.str();
    cout << whole_file << endl;

    return 0;
}

If you want to use C-style character strings, then you can use istream::get to read character by character and store in the character array buffer:

#include <iostream>
#include <fstream>
#include <sstream>

using namespace std;

int main()
{
    ifstream in("sample.txt");
    if (!in.good())
    {
        cout << "Could not open file\n";
        return 1;
    }
    
    char* buffer = new char[1024];
    int count = 0;
    
    while (in.get(buffer[count]))
    {
        count++;
    }
    
    for (int i = 0; i < count; i++)
    {
        cout << static_cast<int>(buffer[i]) << "|" << buffer[i] << " ";
    }
    cout << endl;

    delete[] buffer;
    return 0;
}
kiner_shah
  • 3,939
  • 7
  • 23
  • 37
  • I've tried your method, but the problem with extra characters remained – bluzord Nov 16 '21 at 11:53
  • Did you re-compile the project? Simply running the project after making the changes may not reflect the changes. For re-compiling you may need to first Build it (using Build button) and then Run it (using Run button). – kiner_shah Nov 16 '21 at 13:11