C++ ifstream issue

Can't read file

Page 1 of 1

14 Replies - 3592 Views - Last Post: 23 September 2009 - 06:38 AM Rate Topic: -----

#1 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

C++ ifstream issue

Posted 22 September 2009 - 05:24 AM

#include <iostream>
#include <fstream>
#include <string>
using namespace std;
string header;

int main (int argc, char * const argv[]) {
	// insert code here...

	ifstream infile("model_input.txt");
	if (infile.fail())
	{
		cout<< "Could not open model input.txt" << endl;
	}
	
	//read in the headers
	getline (infile,header);
	
	infile.close();
	return 0;
}


If the file: model_input.txt exists, I get this error:

Quote

[Switching to process 257]Running…
test(368) malloc: *** error for object 0x10000a700: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
(gdb)

If the file does not exist, I get this:

Quote

run
[Switching to process 345]
Running…
Could not open model input.txt
(gdb)


I think it's a permissions problem with the file, but can't see why:

-rwxrwxrwx  1 CTphpnwb  staff	 12 Sep 21 23:28 model_input.txt



This is using C++ Command Line Tool in Xcode 3.2 on Snow Leopard (10.6.1).

Any ideas?

Is This A Good Question/Topic? 0
  • +

Replies To: C++ ifstream issue

#2 seeP+   User is offline

  • D.I.C Addict

Reputation: 55
  • View blog
  • Posts: 601
  • Joined: 20-July 09

Re: C++ ifstream issue

Posted 22 September 2009 - 05:52 AM

ifstream infile("model_input.txt")

You need to make sure the 'infile', in this case 'model_input.txt' is in the same location as the .cpp, .exe files. Unless you have specified the path in the #include "". Also on another note. 'ifstream' does not create the file if it does not exist unline 'ofstream' which does. Hope this helps!

This post has been edited by seeP+: 22 September 2009 - 05:54 AM

Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: C++ ifstream issue

Posted 22 September 2009 - 06:28 AM

True, but it's seeing the file, which is why the error is different if it's in the directory than if it's not!
Was This Post Helpful? 0
  • +
  • -

#4 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: C++ ifstream issue

Posted 22 September 2009 - 06:39 AM

You didn't omit anything did you? I can compile and run that code fine with or without the file existing.
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: C++ ifstream issue

Posted 22 September 2009 - 06:47 AM

No, that's why I think it's a permissions issue with Xcode. The code runs fine on another machine too.

The problem is, where do you go from: -rwxrwxrwx ?

Oops! It's going to:
[email protected] 1 CTphpnwb staff 39 Sep 21 23:09 model_input.txt

on me, so something in Xcode must be changing it.
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: C++ ifstream issue

Posted 22 September 2009 - 09:04 AM

Perhaps Xcode is using a different working directory?
Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: C++ ifstream issue

Posted 22 September 2009 - 09:19 AM

I don't think so. If that were the case, nothing would change when I remove the text file from that directory. What happens is that if the file is not there, then infile.fail() is true, so I get the "Could not open model input.txt" message. If I put the file where Xcode expects it, then I get the error:

test(368) malloc: *** error for object 0x10000a700: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Program received signal: “SIGABRT”.
(gdb)
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: C++ ifstream issue

Posted 22 September 2009 - 09:31 AM

More likely the problem lies in the code you're not showing.
Was This Post Helpful? 0
  • +
  • -

#9 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: C++ ifstream issue

Posted 22 September 2009 - 10:18 AM

That's everything. I took this from a larger project, but this should work by itself.
Was This Post Helpful? 0
  • +
  • -

#10 carltech   User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: C++ ifstream issue

Posted 22 September 2009 - 10:30 AM

If you change the permissions back manually does Xcode change them,or do the stay set at 777?
Was This Post Helpful? 0
  • +
  • -

#11 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: C++ ifstream issue

Posted 22 September 2009 - 10:31 AM

I can't recreate your environment unfortunately. Typically if I have an IDE/compiler doing weird things, I start a fresh project just to see if it is some weird project setting that I missed.
Was This Post Helpful? 0
  • +
  • -

#12 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: C++ ifstream issue

Posted 22 September 2009 - 10:34 AM

View PostCTphpnwb, on 22 Sep, 2009 - 12:18 PM, said:

That's everything. I took this from a larger project, but this should work by itself.


Famous last words, dude. You're right, the code you've shown should work by itself. And it does by all accounts, until you add the code we're not seeing into the equation. Or am I missing something?
Was This Post Helpful? 0
  • +
  • -

#13 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: C++ ifstream issue

Posted 22 September 2009 - 10:56 AM

If you are, so am I! Last I checked though, select-all, copy-paste seemed to work on my system. ;)

Oh, and I've tried a fresh start several times. In fact, this shortened version is a fresh start!

As for the permissions, I haven't confirmed it absolutely, but it seems that Xcode alters them.

This post has been edited by CTphpnwb: 22 September 2009 - 10:58 AM

Was This Post Helpful? 0
  • +
  • -

#14 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3872
  • View blog
  • Posts: 14,211
  • Joined: 08-August 08

Re: C++ ifstream issue

Posted 23 September 2009 - 06:33 AM

Hmm, compiler bug:
http://discussions.a...sageID=10244508
The work around is effective!
Was This Post Helpful? 0
  • +
  • -

#15 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6260
  • View blog
  • Posts: 24,030
  • Joined: 23-August 08

Re: C++ ifstream issue

Posted 23 September 2009 - 06:38 AM

Excellent, well for once someone can actually say it's the compiler's fault and be absolutely correct! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1