Gremlin92
@Gremlin92
Целеустремленный

Чтение файла в переменные?

Доброго вечера! Пытаюсь считать файл такого формата:
content//welcome.jpg
1
-1
1
-1
content//logo.png
0.8
-0.8
0.8
0.4

такой вот код:
struct Image
{
	float**VertexCoordinats;
	unsigned int*IndexTexture;
	std::string *Name;
	int *number;
};

std::ifstream in("welcome coordinats.txt");
	Image *im = new Image();
	if (in.is_open())
	{
		while ((in >> im->Name) && (in>>im->VertexCoordinats[0][0])&& (in>>im->VertexCoordinats[0][1])&& (in>>im->VertexCoordinats[0][2])&& (in>>im->VertexCoordinats[0][3]))
		{
		}
	}
	in.close();

Ошибка в строке while (in >> im->Name) Ошибка C2679 бинарный «>>»: не найден оператор, принимающий правый операнд типа «std::string *» (или приемлемое преобразование отсутствует)
  • Вопрос задан
  • 114 просмотров
Пригласить эксперта
Ответы на вопрос 1
sheerluck
@sheerluck
У меня вот так заработало:
#include <iostream>
#include <fstream>
#include <array>
#include <vector>

using Four = std::array<float, 4>;

struct Image
{
  std::array<Four, 2> VertexCoordinats;
  unsigned int IndexTexture;
  std::string Name;
  int number;
};


int main()
{

    auto fin = std::ifstream{"welcome coordinats.txt"};
    auto images = std::vector<Image>{};
    if (fin.is_open())
    {
        auto im = Image{};
        while (    (fin >> im.Name)
                && (fin >> im.VertexCoordinats[0][0])
                && (fin >> im.VertexCoordinats[0][1])
                && (fin >> im.VertexCoordinats[0][2])
                && (fin >> im.VertexCoordinats[0][3]))
        {
            images.push_back(im);
        }
    }
    for (const auto& im : images)
    {
        std::cout << im.Name << '\n';
        for (const auto& e : im.VertexCoordinats[0])
        {
            std::cout << e << ", ";
        }
        std::cout << std::endl;
    }
    fin.close();
}

# печатает
content//welcome.jpg
1, -1, 1, -1,
content//logo.png
0.8, -0.8, 0.8, 0.4,
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы