Hi,

I have a problem with reading the ْXML file,Can you help me .
I need to extract the coordinates of points from the file and later use these points(U,V)to make a polygon . the U and V values are between two tags <contour> and <contour/>

Thank you in advance

You can read it using standard fstream I think, since its plain text file. You dont need "visual"C++.

To read files in you can use an ifstream object and to write files out you can use an ofstream object. You can also use fstream to do both operations to the same object. You use it like this:

#include <ifstream>
#include <iostream>

using namespace std;

int main(){
   ifstream inFile;
   inFile.open("in.txt", ios::in);
   if(inFile.is_open() == false){
      cerr << "Help! Couldn't open file!!" << endl;
      return 1;
   }
   
   cout << "File opened!" << endl;

   inFile.close();

   return 0;
}

You can find more details on cplusplus.com: here

Edited 5 Years Ago by ravenous: n/a

Thank you that is very helpful,but how I can extract the desired information from the text, which is between two tags like <contour> .............</contour>.

Ah, that bit is up to you. You can use something like:

std::string lineFromFile;
while(inFile.good()){
   getline(inFile,lineFromFile);
   
   /* Do things with the line from the file here */
}

to get at the file a line at a time. Since you're reading the lines from the file into a std::string then you can use std::string::find to check if the line has the tag that you're looking for:

std::string lineFromFile;
while(inFile.good()){
   getline(inFile,lineFromFile);
   
   if(lineFromFile.find("<contour>") != std::string::npos || lineFromFile.find("<CONTOUR>") != std::string::npos){
      /* Now do things with the line that has the tag on it */
   }
}

If you're going to do a lot with this kind of XML file parsing, then there are libraries that will parse the file into some kind of C++ structure for you. However, I've found that they can be quite complex to learn how to use (mainly because they have to deal with any XML file the find). If you only have a few different tags and a simple file structure, then you're probably best to write it yourself.

This article has been dead for over six months. Start a new discussion instead.