There's gotta be a better way to do this

getting data from a text file and writing it to XML via DOM

Page 1 of 1

1 Replies - 467 Views - Last Post: 03 June 2009 - 02:17 PM Rate Topic: -----

#1 stephen.madden  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 64
  • Joined: 15-May 09

There's gotta be a better way to do this

Posted 03 June 2009 - 12:59 PM

First of all, I will post the code in question, which creates an xml element (node1), and then populates node1 with data read from a text file. I created an array for the loop so that it grabs the first character, puts it in the array, and then repeats until eof. Question, is there an easier and cleaner way of doing this?:
	pe = pXMLDom->createElement("node1");
	if (pe != NULL)
	{
		//add newline + tab for indentation
		pXMLDom->documentElement->appendChild(pXMLDom->createTextNode("\n\t"));

		fin.open("C:\\Documents and Settings\\stephen\\My Documents\\test.txt", ios::in);
		//check if file opened. fin.fail() returns 1 if there is a fail in the  //stream
		if(fin.fail())
		{
			cout << "Error: Unable to open file.\n";
			exit(1);
		}
		char charStr[10];
		fin.get( c );	//get firt char
		int i= 0;
		charStr[i] = c;

		//while the stream hasn't failed or reached the eof, read
		while(!fin.fail() && !fin.eof())
		{
			i++;
			fin.get( c );	//get the next char from stream
			charStr[i] = c;

		}
		fin.close();	//Close stream once done
		charStr[i] = '\0';



		pe->text = (_bstr_t)charStr;
		pXMLDom->documentElement->appendChild(pe);
		pe.Release();


	}


I've attached the full code so you can see everything. You may have questions that arise like "why in the hell did he typecast charStr?" There may be some stuff commented out in the actual file that I didn't delete. I combined two files...

Attached File(s)


This post has been edited by stephen.madden: 03 June 2009 - 01:14 PM


Is This A Good Question/Topic? 0
  • +

Replies To: There's gotta be a better way to do this

#2 ajwsurfer  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 373
  • Joined: 24-October 06

Re: There's gotta be a better way to do this

Posted 03 June 2009 - 02:17 PM

A quick Google of "c++ file input output" returned this page:
http://www.cplusplus...tutorial/files/
as the top result :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1