Author Topic: fstream issues  (Read 3016 times)

Offline slightlyeskew

  • Single posting newcomer
  • *
  • Posts: 5
fstream issues
« 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.

Offline Biplab

  • Developer
  • Lives here!
  • *****
  • Posts: 1874
    • Biplab's Blog
Re: fstream issues
« Reply #1 on: June 16, 2007, 01:06:38 pm »
Change the following line:
Code: [Select]
newName.open("C:\blah.txt");
To:
Code: [Select]
newName.open("C:\\blah.txt");
Be a part of the solution, not a part of the problem.

Offline Roman

  • Multiple posting newcomer
  • *
  • Posts: 78
Re: fstream issues
« Reply #2 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
CB LSI (C::B as a Little Secret Initiative)

Offline slightlyeskew

  • Single posting newcomer
  • *
  • Posts: 5
Re: fstream issues
« Reply #3 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.

Offline darthdespotism

  • Almost regular
  • **
  • Posts: 163
    • Coder's Nemesis
Re: fstream issues
« Reply #4 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

Offline Roman

  • Multiple posting newcomer
  • *
  • Posts: 78
Re: fstream issues
« Reply #5 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
CB LSI (C::B as a Little Secret Initiative)