File I/O problem

  • (2 Pages)
  • +
  • 1
  • 2

17 Replies - 916 Views - Last Post: 21 September 2009 - 01:09 PM Rate Topic: -----

#1 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

File I/O problem

Posted 20 September 2009 - 03:15 PM

Hi guys. I'm doing the Traveling salesman problem and I'm having a problem reading in data that contains the costs from city to city.

I've done similar I/O on Linux and Windows so the only thing I can think of is that it could be a Mac specific problem, but I don't know. I thought it'd be similar to Linux.

Here's the code in question:
	ifstream data;
	data.open("tsp.txt",ifstream::in);
	if(data.eof())
	{
		cout << "data.eof()"<<"\n";
		exit(1);
	}
	if(data.fail())
	{
		cout << "data.fail()" << "\n";
		exit(2);
	}


I keep getting
"Program loaded.
run
[Switching to process 568]
Running…
data.fail()

Debugger stopped.
Program exited with status value:2.(gdb) "

It is in the same directory as the program.

My friend compiled it in Windows and it crashes in another location. I figured it might, but I can't debug it here on my Mac to see why because it fails at the above code, which is before the crash he got on Windows.

Is This A Good Question/Topic? 0
  • +

Replies To: File I/O problem

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 5954
  • View blog
  • Posts: 23,222
  • Joined: 23-August 08

Re: File I/O problem

Posted 20 September 2009 - 05:28 PM

Probably stomping memory somewhere else in your program, if it's crashing at random points.
Was This Post Helpful? 0
  • +
  • -

#3 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

Re: File I/O problem

Posted 20 September 2009 - 06:17 PM

No, it fails at the same point everytime. However, that point is different on two different OSs.
Was This Post Helpful? 0
  • +
  • -

#4 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: File I/O problem

Posted 20 September 2009 - 06:48 PM

Here
 data.open("tsp.txt",ifstream::in);


shouldnt it be
 data.open("tsp.txt", ios::in);

Hope that helps
Was This Post Helpful? 0
  • +
  • -

#5 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

Re: File I/O problem

Posted 20 September 2009 - 06:53 PM

Tried it but it was to no avail.
Was This Post Helpful? 0
  • +
  • -

#6 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: File I/O problem

Posted 20 September 2009 - 06:58 PM

Ok questions
1. What are you trying to do?
2. Is the file your opening in the same folder as your .cpp?


Just in case your just trying to check if the file was opened then its
if(myfile.is_open)


Read this for more information ;)
http://www.cplusplus...tutorial/files/

This post has been edited by poncho4all: 20 September 2009 - 07:03 PM

Was This Post Helpful? 0
  • +
  • -

#7 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

Re: File I/O problem

Posted 20 September 2009 - 07:02 PM

Like I said, I'm trying to open a file that contains the costs from city to city in my traveling salesman program.

Yes, the file is in the same directory.
Was This Post Helpful? 0
  • +
  • -

#8 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: File I/O problem

Posted 20 September 2009 - 07:09 PM

Well check the EDIT i made on my previous post.

A dum question but since it seems like the file is failing i would have to ask it. Is the format of your file really .txt?

fail()
Returns true in the same cases as bad(), but also in the case that a format error happens, like when an alphabetical character is extracted when we are trying to read an integer number.
Was This Post Helpful? 0
  • +
  • -

#9 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

Re: File I/O problem

Posted 20 September 2009 - 07:16 PM

Originally, it was a .dat file, but I changed it because it gave me the same problem and I'm trying everything I can. I thought about checking if it's open or not, but, I don't think it'd matter if the fail bit keeps getting set.
Was This Post Helpful? 0
  • +
  • -

#10 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: File I/O problem

Posted 20 September 2009 - 07:19 PM

Could you post or attach the file you are trying to open?
Was This Post Helpful? 0
  • +
  • -

#11 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

Re: File I/O problem

Posted 20 September 2009 - 07:28 PM

Attached File  tsp.txt (393bytes)
Number of downloads: 56
Was This Post Helpful? 0
  • +
  • -

#12 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: File I/O problem

Posted 20 September 2009 - 07:36 PM

I dont get it, should be opened with the code you have provided.

Is that the if else code, or just an example?
Was This Post Helpful? 0
  • +
  • -

#13 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

Re: File I/O problem

Posted 20 September 2009 - 07:38 PM

That's the first bit of code in the main function.
Was This Post Helpful? 0
  • +
  • -

#14 poncho4all  Icon User is offline

  • D.I.C Head!
  • member icon

Reputation: 123
  • View blog
  • Posts: 1,405
  • Joined: 15-July 09

Re: File I/O problem

Posted 20 September 2009 - 07:56 PM

Well before i give up i want to know one last thing, is your file name tst or tsp.txt by this i mean when you read. Not because you know the file is .txt. I mean leteraly.

I tried this
#include <iostream>
#include <fstream>
#include <conio.h>
using namespace std;

int main(){
	ifstream data;
	data.open("test.txt",ios::in);
	if(data.is_open())
	{
		cout << "data.eof()"<<"\n";
	}else
		if(data.fail()){
		cout << "data.fail()" << "\n";

	}

	getch();
	return 0;
}



And it worked with a file named test and not with a file named test.txt

Hope that helps :)
Was This Post Helpful? 1
  • +
  • -

#15 Techno Mage  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 86
  • Joined: 10-June 07

Re: File I/O problem

Posted 20 September 2009 - 08:01 PM

Whoa! That did work! I know that Mac OSX does an extension hiding thing, but I thought that it was purely GUI aesthetics. Apparently its command line too.

EDIT:
Actually, the extension does show up. It's listed as tsp.txt. I don't know! Maybe it got bored of giving me a problem.
Thanks again!

This post has been edited by Techno Mage: 20 September 2009 - 08:03 PM

Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2