| | |
TSP read input file problem
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Sep 2007
Posts: 5
Reputation:
Solved Threads: 0
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
-----------------------------------------------------------------------------------------------
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
-----------------------------------------------------------------------------------------------
C++ Syntax (Toggle Plain Text)
#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(); }
Last edited by sycc2172; Sep 16th, 2007 at 12:50 pm.
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.
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.
Last edited by iamthwee; Sep 16th, 2007 at 12:59 pm.
Yeah basically you wanna use something like
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.
C++ Syntax (Toggle Plain Text)
#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; }
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.
Last edited by iamthwee; Sep 16th, 2007 at 4:39 pm.
![]() |
Similar Threads
- how to read a text file backwards? (C)
- Help in Input from a file (C++)
- Reading an input file as a class memeber function (C++)
- I need help on STRUCTURES and on input file (C)
- How to Read an input file (C++)
- Lines are being skipped in the input file. (Visual Basic 4 / 5 / 6)
- how to place a specific info from input file (C)
Other Threads in the C++ Forum
- Previous Thread: help with windows api in c++
- Next Thread: C++ Help Immediately!
| Thread Tools | Search this Thread |
api array based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







I'll proceed with the code...