Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: I can't use std::ofstream  (Read 2826 times)

0 Members and 1 Guest are viewing this topic.

GMahir

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
I can't use std::ofstream
« on: November 05, 2015, 12:09:08 am »
I'm trying to make an animation of moving arrows and I need to keep record of time, in a text file, how much arrows stays visible on the screen. But when I write(and not even use that variable):
std::ofstream record;
It compiles without an error but screen shows up and closes immediately. It shows one frame but never gets into main loop. But when I delete only that line everything else works fine.
I suspected namespace members overlapping somehow but that's not the issue.I tried on Windows 7 x64 with CodeBlocks+mingw(GCC 4.8.1) and on Ubuntu 15.04 x64 with gcc 5.1.0.
I tried with a simple program (one that is here bottom of the page : http://www.sfml-dev.org/tutorials/2.3/start-cb.php) and added that the lines that I'm trying to use to write to a text file,only with a random string, and it works.
There are a little too much code so I didn't paste it all here and couldn't simplify it but I'm adding the files as an attachment.
This is how I compile if it's relevant:
g++ -std=c++11 -Wall -Wextra -Werror main.cpp -lm arrow.hpp -lsfml-graphics -lsfml-window -lsfml-system

I really need to keep the records and if there is another way of doing it, I'd like to know.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: I can't use std::ofstream
« Reply #1 on: November 05, 2015, 01:51:46 am »
I didn't really look over your code too closely, but I did try uncommenting your record variable lines, compiling, and running your program on a Linux machine (gcc 4.9.3). Everything seemed to work fine as far as I could tell. I saw arrows flying across the screen and files got generated in the Records directory.

You're seeing the exact same issue on both Windows and Linux? Is anything being printed to the terminal? If you haven't already, you could try using a debugger to get more clues on what's going wrong for you.

GMahir

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: I can't use std::ofstream
« Reply #2 on: November 05, 2015, 02:18:47 am »
I've simplify it trying(deactivating-activating) line by line. And I got this:
#include<SFML/Graphics.hpp>
#include<fstream>

int main()
{
        std::ofstream record( "Records/file.txt" );
    sf::RenderWindow mainWindow;
    sf::Event event;
    sf::Texture BGTexture;
    sf::Sprite Background;
    sf::Clock clock;

    mainWindow.create( sf::VideoMode::getDesktopMode(), "Moving Arrows" );
    mainWindow.setVerticalSyncEnabled(true);

    [b]BGTexture.loadFromFile("Background.jpg");[/b]
    Background.setTexture( BGTexture );

    if( mainWindow.isOpen() )
    {
        mainWindow.pollEvent( event );
        mainWindow.clear( sf::Color::Black );
        mainWindow.display();
        clock.restart();

        while( event.type != sf::Event::Closed )
        {
                        mainWindow.pollEvent( event );

                        mainWindow.clear( sf::Color::Black );
                        mainWindow.draw(Background);
                        mainWindow.display();
                        record<<"X";
                }

                mainWindow.close();
                record.close();
        }
        return 0;
}
 
This works on Windows, but when try it on Ubuntu it acts same as before. The problem is with sf::Texture::loadFromFile() function. When I delete it(or either std::ofstream) it works as it suppossed to be.
Of course shows a black screen.
On the other hand, I tried deleting same function from the main file(and from the arrow.hpp) and same problem kept occuring on Ubuntu.
I guess I will be working with Windows for now, when I have time I'll try to solve that problem with Ubuntu.

Arcade

  • Full Member
  • ***
  • Posts: 230
    • View Profile
Re: I can't use std::ofstream
« Reply #3 on: November 05, 2015, 05:15:08 am »
Is sf::Texture::loadFromFile() failing? Have you tried checking the return value? Make sure you are launching your application from the same directory as Background.jpg so that it can find the image. Also, is SFML printing any errors to the terminal?

GMahir

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: I can't use std::ofstream
« Reply #4 on: November 05, 2015, 10:31:28 pm »
No, as I said there are no compile errors no runtime errors and nothing on the terminal. And I checked sf::Texture::loadFromFile() does it's job, but program terminates before main loop somehow.

AlejandroCoria

  • Jr. Member
  • **
  • Posts: 68
    • View Profile
    • alejandrocoria.games
    • Email
Re: I can't use std::ofstream
« Reply #5 on: November 06, 2015, 05:59:30 pm »
pollEvent leave parameter unmodified if no event. You should not read the type of event if pollEvent returns false.