Answer to Question #21654 in C++ for mahmoud tarek

Question #21654
i want to read vertices and edges from text file
it is text file and contain this
8
16
4 5 0.35
4 7 0.37
5 7 0.28
0 7 0.16
1 5 0.32
0 4 0.38
2 3 0.17
1 7 0.19
0 2 0.26
1 2 0.36
1 3 0.29
2 7 0.34
6 2 0.40
3 6 0.52
6 0 0.58
6 4 0.93
where 8 is number of vertices and 16 is number of edges and for example "4 5 0.35" from vertices number 4 to vertices number 5 the edge equal 0.35 how can i read it and store it in array ?
please help me to contact me my yahoo mail is anapssee_mtm@yahoo.com
1
Expert's answer
2013-01-09T08:56:36-0500

#include <fstream>
#include <iostream>
#include <string>
#include <conio.h>

void read_data();
double sum=0;
using namespace std;

double vertices[1000];

int main()
{
read_data();

getch();
return 0;
}

void read_data(){
string line;
ifstream myfile ("edges.txt");
int countofline=0;
if (myfile.is_open())
{
while ( myfile.good() )
{
getline (myfile,line);//read each lines
char* cstr = new char [line.size()+1];
strcpy (cstr, line.c_str());
char* pch= strtok(cstr," ");
while (pch != NULL)
{
cout<<pch<<"
";
vertices[countofline]=atof(pch);//add to array from file
pch = strtok (NULL, " ");

}

}
myfile.close();//cloe file
}
else cout << "Unable to open file

";

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS