M
M
Maxim Siomin2020-07-20 10:52:38
C++ / C#
Maxim Siomin, 2020-07-20 10:52:38

Why is the file not being read correctly?

class-header.h

spoiler
#include <string>
#include <vector>
#include <fstream>

using namespace std;

typedef string str;

class User
{
public: / /в private хранятся переменные, которые никак не относятся где делу, поэтому я опустил их
vector<str> readFile()
  {
    vector<str> array{};
    array.resize(10);

    for (short x = 0; x < 10; ++x)
    {
      str y;

      switch (x)
      {
      case (0):
        y = "0";
        break;
      case (1):
        y = "1";
        break;
      case (2):
        y = "2";
        break;
      case (3):
        y = "3";
        break;
      case (4):
        y = "4";
        break;
      case (5):
        y = "5";
        break;
      case (6):
        y = "6";
        break;
      case (7):
        y = "7";
        break;
      case (8):
        y = "8";
        break;
      case (9):
        y = "9";
        break;
      }

      str path = "Saves/Users/" + m_strID + y + ".txt";
      fstream file(path);
      getline(file, array[x]);
      cout << array[x];
      file.close();
    }
    
    return array;
  }

main.cpp
spoiler
#include <iostream>
#include <vector>
#include <string>
#include "class-header.h"

int main()
{
vector<string> v = user1.readFile()
for (string elem : v)
    cout << elem;
}

The program starts, but nothing happens, nothing at all. Just starts and ends

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
maaGames, 2020-07-20
@maaGames

int main
{
     ...

    cin.get();
    return 0;
}

And most importantly, there is no check for the existence of the file, no check that the file has opened. And, in general, there is no error handling. When launched for debugging, the application directory and the working directory may differ, and when a relative path is specified, the file simply cannot be found.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question