Sign in to follow this  
kev670

file streams help

Recommended Posts

hi. this is a simple program to convert a flat excel file into xml. My problem is my program crashes if you try to enter a file that does not exist. can someone give me a bit of code that i can implement that tells the user this file does not exist. thanks for any help
#include <iostream>                      //cin, >>, cout, <<
#include <fstream>                       //for file I/O
#include <string>                        //strings
using namespace std;




void main(void)
{
 	string file1,file2, quit;
 	string empFileName;
  	int position, position2;				// defining
   	string head1,head2,head3,head4;
	string data1,data2,data3,data4,y;	
   //----------------------------------------------------------------------------
   
	for(;;)									// forever loop
	{   	
   	
   	
   	ifstream fil;
	cout << "\nPlease enter the file name or enter q to quit:  ";
	getline(cin,empFileName);				// takes the file name
	
	
	if (empFileName == "q" || empFileName == "Q")
	{
		cout << "\nGoodbye: "; break;			// if you want to quit
		
	}
	
	
	fil.open(empFileName.data());				//open a file
	
	//////////////////////////////////////////////////////////////
	position = empFileName.find(".",0);
	y = empFileName.substr (position+1,3);		// finds out if file is txt or csv
	

	//----------------------------------------------------------------
	if  (y == "txt")
	
	{
		position2 = empFileName.find(".",0);		// find . in file name
		
		file1 = empFileName.erase (position,4);		// erases file designation
		 string outputFileName = file1 + ".xml";	// adds .xml to file name	
		
		 ofstream out(outputFileName.data());		// creates .xml file
		
		file2 = empFileName.erase (position-1,5);
		////////////////////////////////////////////////////////////////
		
		
		fil >> head1 >> head2 >> head3 >> head4;	// seperates words
		out << "<" << file1 << ">" << endl;

	
		
		for(;;)					// loop to get all information from file
		{
			fil >> data1 >> data2>> data3;
			getline(fil,data4);
			
			if (fil.eof() ) break;	// breaks when the end of file is reached
			out << "	" "<" << file2 << ">" << endl 		// formatting file for xml
			     << "		" << "<" << head1 << ">" << data1 << "</" << head1 << ">" << endl
				 << "		" << "<" << head2 << ">" << data2 << "</" << head2 << ">" << endl
				 << "		" << "<" << head3 << ">" << data3 << "</" << head3 << ">" << endl
				 << "		" << "<" << head4 << ">" << data4 << "</" << head4 << ">" << endl
		  		 << "	" "</" << file2 << ">\n" << endl; 

		}
		out << "</" << file1 << ">" << endl;
		fil.close();
		
	}
	
	//-------------------------------------------------------------
	else if (y == "csv")			// LOOK AT PREVIOUS COMMENTS
	{
		position2 = empFileName.find(".",0);
		
		file1 = empFileName.erase (position,4);
		
		 string outputFileName = file1 + ".xml";

		 ofstream out(outputFileName.data());
		
		file2 = empFileName.erase (position-1,5);
		////////////////////////////////////////////////////////////////
		
		
		
		out << "<" << file1 << ">" << endl;

		getline(fil,head1,',');
		getline(fil,head2,',');			// finds seperating commas and take word before them
		getline(fil,head3,',');
		getline(fil,head4);
	
		for(;;)
		{
			getline(fil,data1,',');
			getline(fil,data2,',');
			getline(fil,data3,',');
			getline(fil,data4);
			
			if (fil.eof() ) break;
			out << "	" "<" << file2 << ">" << endl 
			     << "		" << "<" << head1 << ">" << data1 << "</" << head1 << ">" << endl
				 << "		" << "<" << head2 << ">" << data2 << "</" << head2 << ">" << endl
				 << "		" << "<" << head3 << ">" << data3 << "</" << head3 << ">" << endl
				 << "		" << "<" << head4 << ">" << data4 << "</" << head4 << ">" << endl
		  		 << "	" "</" << file2 << ">\n" << endl; 
	
		}
		out << "</" << file1 << ">" << endl;
		fil.close();
	}
	else 
	{				// if file doesn't have .txt or .csv this is displayed
		cout << "\nI'm sorry but that File does not exist or the address you entered is incorrect \nplease try again\n\n";
	}
	}
}
//-------------------------------------------------------------------


Share this post


Link to post
Share on other sites

fil.open(empFileName.data());

if(!fil.is_open())
{
std::cout << "file failed\n";
return false; // or whatever
}


After you open the file, check is_open() to see if it succeeded or not.

Oh, and we normally use empFileName.c_str(), not .data() to get a c-style string for passing to interfaces like fstream's. According to this, the array returned by data() is not necessarily null-terminated, so you should not really be using it to get a c-style representation of the std::string.

HTH

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this