Code::Blocks Forums

User forums => General (but related to Code::Blocks) => Topic started by: slightlyeskew on June 16, 2007, 12:11:41 pm

Title: fstream issues
Post by: slightlyeskew on June 16, 2007, 12:11:41 pm
Been working on this for a while...trying to input a text file...using windows XP
ifstream newName;

                ifstream newName;
               
                newName.open("C:\blah.txt");
                if (newName.is_open())
               
                       {cout << "for the love of god";}

                else
                        {cout << "boggle";}

                newName.close();

It ALWAYS evaluates to boggle. I was using

if (newName.fail())
{cout << "hrm";}

if (newName.good())
{cout << "sweet";}

else
{cout << "boggle";}

which still evaluated to boggle. I would really appreciate help with this.

I've included both fstream and iostream...I've consulted multiple books and websites about context for ifstream and what not, to no avail. If additional info is needed to diagnose this pls let me know, I've tried both absolute path and relative path for the open...Thanks for your time.
Title: Re: fstream issues
Post by: Biplab on June 16, 2007, 01:06:38 pm
Change the following line:
Code
newName.open("C:\blah.txt");

To:
Code
newName.open("C:\\blah.txt");
Title: Re: fstream issues
Post by: Roman on June 16, 2007, 02:13:49 pm
Damn :)  This simple thing broke many keyboards )

As far as I know You can use '/' instead of '\' on win32 too.

Regards
Roman
Title: Re: fstream issues
Post by: slightlyeskew on June 16, 2007, 11:33:32 pm
First of all, I love you guys...worked fine after I changed the path...

Is there a way to use the relatiave path...if my text file was stored at C://blah.txt Is there a way to use just "blah.txt" when calling .open(...) ???

Thanks again for the help.
Title: Re: fstream issues
Post by: darthdespotism on June 17, 2007, 12:02:35 am
Yes relative Paths are supported but be careful: The path is normally relative to the place from where you started the programm and not to the place the executeable lies in.

As far as I know You can use '/' instead of '\' on win32 too.
That's correct
Title: Re: fstream issues
Post by: Roman on June 17, 2007, 12:14:06 am
*nix chdir(const char *) function
http://www.delorie.com/gnu/docs/glibc/libc_268.html

MS analog - _chdir(const char *) in <direct.h>

It seems also that BOOST::filesystem provides something portable

Regards
Roman