Greetings.

I'm currently doing a project on Traveling Salesman Problem. But I'm not asking for answers for the whole project. I just started my programming lessons so I hope can get some help from all of you.

About this project: Initially you have to read 2 input files, Input1.txt and Input2.txt. An example of the contents for these 2 input files are as follows:

Input1.txt:
0 1
0 7
1 0
1 2 and so on..

Input2.txt:(Showing location name, first 3 numbers are for longitude and the next 3 numbers for latitude. Units for longitude are deg, min, sec)
Africa 111 85 23 2 14 59.72
America 112 27 55 2 29 35.29
China 101 21 22.31 2 20 21.29 and so on.....

My problem right now is after I wrote the codes to read the first file as shown below, it always display "Can't Open Input File!" as its cout result. Can I know where is it gone wrong? Another question is since I'm using array to store all the data, is using pointer will be easier than using array? Thank you for your help

-----------------------------------------------------------------------------------------------

#include <cstdlib> //to use exit()
#include <iostream>
#include <fstream>
using namespace std;

const int ARRAY_SIZE = 100;

void getConnectionValue(int connection[ARRAY_SIZE], int connection1[ARRAY_SIZE]);

int main()
{
	//Declaring array
	int connection [ARRAY_SIZE];
	int connection1 [ARRAY_SIZE];

	getConnectionValue(connection, connection1);
	return 0;
}

void getConnectionValue(int connection[ARRAY_SIZE], int connection1[ARRAY_SIZE])
{
	//Declare stream and open the first .txt file
	ifstream inStream;
	inStream.open("Input1.txt"); 
	
	if (inStream.fail()) 
	{
		cerr << "Can't open input file!\n" << endl;
		exit(1);		
	}
	
	int i=0;
	while (!inStream.eof())
	{	
		inStream >> connection[i] >> connection1[i];
			
		cout << i+1 << "numbers " << connection[i] << "and "<< connection1[i] << "\n" << endl;
		i++;
	}
	inStream.close();
		
}

Dani AI

Generated

As discovered, the usual cause of "can't open" is a path/working-directory mismatch (the process looks where it runs, not where the source file lives). A reliable fix is to either give ifstream an absolute path, put the text file next to the running executable, or configure the IDE to copy the file to the output directory. Also verify exact filename, case (on some systems), and file permissions.

For reading the pairs, avoid using eof() to control the loop. A safer pattern is to attempt extraction and stop when it fails; this cleanly handles EOF and malformed lines. Prefer a container that grows automatically instead of fixed C arrays or raw pointers; std::vector offers automatic lifetime and bounds safety and is easier for later processing. Check the stream state after parsing to detect partial reads.

Parsing the location file (degrees/minutes/seconds) is best done by reading the name (use getline if names can contain spaces) and then extracting the numeric fields. Convert DMS to decimal degrees with the formula:

double decimal = deg + min/60.0 + sec/3600.0;

Small validations (enough fields, numeric ranges) help avoid subtle bugs. Tying back to : token-reading loops are fine, but for structured lines parse into appropriate types and containers, check return values from extractions, and prefer std::vector over manual pointer/array management.

Recommended Answers

All 9 Replies

Member Avatar for Member #46692

Is the text file in the same directory as your program?

Yes it is.. I put it into the Debug folder. The file name is exactly the same

Member Avatar for Member #46692

Can you try dumping your text file in the c: drive then change your program so this line:inStream.open("Sarawak_Cnx.txt");

becomes: inStream.open("C:\\Sarawak_Cnx.txt");

What happens now?

commented: <.< +3

Argh... I got it! I put into the wrong directory. Thanks for the advice.

One question. Is the code above able to change to using pointer instead of arrays?

Member Avatar for Member #46692

Um no using an array and indexing should be easier to understand than pointers and is perfectly ok. I'd stick with that.

Just to let you know the way you are reading in data from a file is wrong and horribly flawed. Shall I show you the correct way.

Yes please. I'm just a new student taking C++ lesson this semester. Any advice would be much appreciated

Member Avatar for Member #46692

Oops sorrie I gotta go. :(

Okay.. nvm.. Thx for the help. :) I'll proceed with the code...

Member Avatar for Member #46692

Yeah basically you wanna use something like

#include <iostream> //basic i/o
#include <fstream> //file input out
#include <string> //string handling

using namespace std;

int main()
{
  //open a file for reading
  ifstream read ("foo.txt");
  string chunk; //this will be the string which holds each word or number

  while ( read >> chunk )
  {
   cout << chunk << endl;
  }
  
  read.close(); //close file
  return 0;
}

to read files, instead of eof as that has problems. As a newbie I don't think it is important to bore you with the details as to why eof is bad.

Obviously after that you need to convert the string to actual numbers. But I'm not sure if you are allowed to use strings. Let me know.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.