[solved]problems trying to read from a file

Get answers to all your basic programming questions. No Ogre questions, please!
vblanco
Kobold
Posts: 26
Joined: Thu Jan 26, 2012 8:36 pm

[solved]problems trying to read from a file

Post by vblanco »

Hello, im trying to learn how to read/write files, so im trying to make a simple .OBJ loader (i dont want to just copypaste, i want do to it myself to learn) the problem is that the file is opened , myfile.is_open() returns true correctly, but then, when i try to getline, it doesnt work, and if i search in debug mode, the Ifstream object says "error reading characters of string" . im learning to handle files, but that gets in my way so i cant do anything, if someone knows what error im making, please help me ( i searched in google for the error, but got no useful answer)
the code is

Code: Select all

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

void LoadOBJ(const char* file,bool Log)
{
	printf(file);
	string line;
	ifstream myfile (file);
	if (myfile.is_open())
	{
		while ( true)
		{
			if(getline (myfile,line)  )
			{
			cout << line << endl; // currently i print the lines to the screen for testing, later ill add the parsisng code
			}
			else
			{
				break;
			}
		}
    
	}
	else
	{
		printf("error in opening file");
	}
	myfile.close();
}


int main()
{
	

	LoadOBJ("SimpleCube.obj",true);

	while(true)
	{
	}
}
that while(true) at the end is a hack to force the command window to not auto close, i know its really lame.
the SimpleCube.obj file its on the same folder as the executable and its the default cube exported from blender with the obj exporter.
Last edited by vblanco on Mon Sep 16, 2013 9:18 pm, edited 1 time in total.
User avatar
Faranwath
Halfling
Posts: 93
Joined: Mon Jul 09, 2012 2:19 pm
Location: Cuba
x 7

Re: problems trying to read from a file

Post by Faranwath »

Binary files have no concept of line, I would say. All they contain is a bunch of bytes that a text editor interprets as characters, so you cannot say "Give me the first line of this binary file".
Try this out:

Code: Select all

#include <fstream>
#include <string>
#include <algorithm>
#include <iterator>
/* ... */
std::string file_contents;
std::ifstream stream{file_name, std::ios::binary};
if (!stream.is_open())
    /* handle error */
else
{
    std::copy(
        std::istreambuf_iterator<char>{stream},
        std::istreambuf_iterator<char>{},
        std::back_inserter(file_contents)
        );
}
vblanco
Kobold
Posts: 26
Joined: Thu Jan 26, 2012 8:36 pm

Re: problems trying to read from a file

Post by vblanco »

But the .obj is a text based file format, you can open it with notepad++ just like any text file and its nicely formatted in lines, and normal characters. i know the difference beetween binary and text files., in fact, the OBJ im testing with is this

Code: Select all

# Blender v2.66 (sub 1) OBJ File: ''
# http://www.blender.org
mtllib SimpleCube.mtl
o Cube
v 1.000000 -1.000000 -1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 -1.000000 1.000000
v -1.000000 -1.000000 -1.000000
v 1.000000 1.000000 -0.999999
v 0.999999 1.000000 1.000001
v -1.000000 1.000000 1.000000
v -1.000000 1.000000 -1.000000
usemtl Material
s off
f 1 2 3 4
f 5 8 7 6
f 1 5 6 2
f 2 6 7 3
f 3 7 8 4
f 5 1 4 8
Last edited by vblanco on Mon Sep 16, 2013 7:29 pm, edited 1 time in total.
User avatar
Faranwath
Halfling
Posts: 93
Joined: Mon Jul 09, 2012 2:19 pm
Location: Cuba
x 7

Re: problems trying to read from a file

Post by Faranwath »

Oh well, my bad... I'm not familiar at all with the format, the name suggested me it was binary-based. In that case, your code should work :roll: Try reading a different file, or a subset of the one you're working with (e.g. the first two lines). If that fails, look for an error in a more broader context. Just to be sure, is the name of the file correctly spelled?
vblanco
Kobold
Posts: 26
Joined: Thu Jan 26, 2012 8:36 pm

Re: problems trying to read from a file

Post by vblanco »

the name is correct, if i put it wrong, to a file that doesnt exists, the if (myfile.is_open()) returns false, and closes the program. but it returns true, but cant read the file, its a bit like WTF
edit: got fixed by just copying the file (?????????) and renaming it
User avatar
Faranwath
Halfling
Posts: 93
Joined: Mon Jul 09, 2012 2:19 pm
Location: Cuba
x 7

Re: problems trying to read from a file

Post by Faranwath »

Well, in that case I don't really know what to say. Maybe the file has some weird attributes (i.e. "thou shalt not read me!").
Let's see, you said you were learning, so create a text file containing a bunch of lines, and do the following:

Code: Select all

#include <iostream>
#include <fstream>
#include <string>

int main()
{
    std::ifstream file{"my_file.txt"};
    if (!file.is_open())
    {
        std::cerr << "oops!\n";
        return 1;
    }

    std::string line;
    while (std::getline(file, line))
        std::cout << line << std::endl;
}
This has to work, no questions.

edit: We'll never know then :lol:
vblanco
Kobold
Posts: 26
Joined: Thu Jan 26, 2012 8:36 pm

Re: problems trying to read from a file

Post by vblanco »

ill mark it as solved, anyway, i MADE MY OBJ LOADER, i feel so proud of myself.