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

Author Topic: Loading a file from a folder in the current directory.  (Read 5978 times)

0 Members and 1 Guest are viewing this topic.

notpedro

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Loading a file from a folder in the current directory.
« on: February 15, 2015, 09:37:33 am »
I would like to include an image that is in a folder in the current folder without including the full path using the loadFromFile() function.

How can I achieve this?
Thank you.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Loading a file from a folder in the current directory.
« Reply #1 on: February 15, 2015, 10:00:48 am »
Use a relative path like "./filename".
« Last Edit: February 15, 2015, 05:02:37 pm by Jesper Juhl »

excentio

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Loading a file from a folder in the current directory.
« Reply #2 on: February 15, 2015, 09:23:39 pm »
On windows, you can use this

#include <windows.h>

string getPath() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    return string( buffer ).substr( 0, pos);
}
 

then write

Image img;
img.loadFromFile(getPath() + "\\myImg.png");
 

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Loading a file from a folder in the current directory.
« Reply #3 on: February 15, 2015, 09:26:41 pm »
Note that '/' works as a path seperator on both Windows and Unix and does not need to be escaped as "\\" (not to mention that '\' does not work in Unix, so annoying in platform agnostic code)...
Also, if you know that the resources are in your cwd (current working directory) (as returned by the standard getcwd()), then just using "./filename" is a lot simpler.
« Last Edit: February 15, 2015, 09:30:09 pm by Jesper Juhl »

excentio

  • Newbie
  • *
  • Posts: 21
    • View Profile
Re: Loading a file from a folder in the current directory.
« Reply #4 on: February 15, 2015, 09:28:56 pm »
Note that '/' works as a path seperator on both Windows and Unix and does not need to be escaped as "\\"...

works too, but there's no sense in this, because this code is windows-only, he'll need to search another piece of code for another OS  :)
btw, that's strange, but on Visual Studio 12 thing like "./myImg.png" doesn't work, just tested this
« Last Edit: February 15, 2015, 09:32:22 pm by excentio »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
AW: Loading a file from a folder in the current directory.
« Reply #5 on: February 16, 2015, 01:56:07 am »
It does work, it's just that VS default working directory is set to the directory that contains the project file.

Also "./" itself is not needed, "myimg.png" will work just as well.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

notpedro

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Loading a file from a folder in the current directory.
« Reply #6 on: February 22, 2015, 02:26:41 am »
Use a relative path like "./filename".

This does work for me unfortunately.

notpedro

  • Newbie
  • *
  • Posts: 9
    • View Profile
    • Email
Re: Loading a file from a folder in the current directory.
« Reply #7 on: February 22, 2015, 02:27:41 am »
On windows, you can use this

#include <windows.h>

string getPath() {
    char buffer[MAX_PATH];
    GetModuleFileName( NULL, buffer, MAX_PATH );
    string::size_type pos = string( buffer ).find_last_of( "\\/" );
    return string( buffer ).substr( 0, pos);
}
 

then write

Image img;
img.loadFromFile(getPath() + "\\myImg.png");
 

Thanks for this answer, although Im slightly hesitant as I would have thought there was a more concise way of doing it.

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: Loading a file from a folder in the current directory.
« Reply #8 on: February 22, 2015, 09:19:52 am »
You could do
 
chdir("dir/holding/resources");
img.loadFromFild("myfile.png");