GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 22-May-2006, 11:32
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

File Output - Using Variable for FileName


Hi,

Can someone give me an idea how I would go about using a variable in my program to determine what a name of a file should be when output is written?

My code is below:

CPP / C++ / C Code:
	ofstream output;
	
	#define FILE_OUT "Env.txt"

	output.open(FILE_OUT, ios::out);

	output<<env.sender.FName<<" "<<env.sender.LName<<endl;
	output<<env.sender.Street<<endl;
	output<<env.sender.City<<", "<<env.sender.State<<" "<<env.sender.Zip<<endl<<endl<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.FName<<" "<<env.receiver.LName<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.Street<<endl;
	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.City<<", "<<env.receiver.State<<" "<<env.receiver.Zip<<endl;

	output.close();

I am unsure how to change the format in my #define statement to accept a variable name instead of hardcoding the name.

Any suggestions are appreciated. Thanks!
  #2  
Old 22-May-2006, 13:28
davis
 
Posts: n/a

Re: File Output - Using Variable for FileName


Quote:
Originally Posted by jdbrine
I am unsure how to change the format in my #define statement to accept a variable name instead of hardcoding the name.

There is no way to convert a macro argument into a character constant.

Just use a program variable.

CPP / C++ / C Code:
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main()
{
    cout << "Enter a filename: ";
    std::string strFilename;
    cin >> strFilename;
    fstream outfile( strFilename.c_str(), ios_base::out );
    if( outfile.is_open() )
    {
        cout << "Successfully created file: " << strFilename << endl;
        outfile.close();
    }
    else
    {
        cout << "Creating file named: " << strFilename << " failed!" << endl;
    }
    return 0;
}


Output:
Code:
Enter a filename: bogus Successfully created file: bogus

...and please consider using more whitespace in your code!


:davis:
  #3  
Old 22-May-2006, 14:17
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: File Output - Using Variable for FileName


So, if I wanted to make the filename be an element of my structure, I would do something like:

CPP / C++ / C Code:
void WriteEnvelope(Envelope env)
{

	ofstream output;
	
	//#define FILE_OUT "Env.txt"

	std::string strFilename;
	
	strFilename=env.receiver.LName;

	
	fstream outfile( strFilename.c_str(), ios_base::out );
	
	//output.open(FILE_OUT, ios::out);

	output<<env.sender.FName<<" "<<env.sender.LName<<endl;

	output<<env.sender.Street<<endl;

	output<<env.sender.City<<", "<<env.sender.State<<" "<<env.sender.Zip<<endl<<endl<<endl;

	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.FName<<" "<<env.receiver.LName<<endl;

	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.Street<<endl;

	output<<'\t'<<'\t'<<'\t'<<'\t'<<env.receiver.City<<", "<<env.receiver.State<<" "<<env.receiver.Zip<<endl;

	output.close();


}


It created the file, but didn't dump any of the data.
  #4  
Old 22-May-2006, 14:40
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,258
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: File Output - Using Variable for FileName


Wrong whitespace. He meant horizontal (between operators and such), not vertical:
CPP / C++ / C Code:
output<<env.sender.City<<", "<<env.sender.State<<" "<<env.sender.Zip<<endl<<endl<<endl;
to
CPP / C++ / C Code:
output << env.sender.City << ", " << env.sender.State << " " 
       << env.sender.Zip << endl << endl << endl;
shortening lines also so they aren't over 80-100 characters per line.

Whatyouhaveisakintothistext.Veryhardtoread.
__________________

Got a cough? Go home tonight and eat a whole box of Ex-Lax. Tomorrow, you'll be afraid to cough.
-- Pearl Williams
  #5  
Old 22-May-2006, 14:56
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: File Output - Using Variable for FileName


Okay thanks. So, if I wanted to make the filename be an element of my structure, I would do something like:


CPP / C++ / C Code:
void WriteEnvelope(Envelope env)
{

	ofstream output;
	
	//#define FILE_OUT "Env.txt"

	std::string strFilename;
	
	strFilename=env.receiver.LName;

	
	fstream outfile( strFilename.c_str(), ios_base::out );
	
	//output.open(FILE_OUT, ios::out);

	output<<  env.sender.FName  <<  " " <<  env.sender.LName <<  ndl;
	output <<  env.sender.Street  <<  endl;
	output <<  env.sender.City << ", " <<  env.sender.State  <<  " "
                       <<  env.sender.Zip  << endl  <<  endl  <<  endl;
	output <<  '\t' << '\t' << '\t' << '\t' <<  env.receiver.FName  << " "
                       <<  env.receiver.LName  <<  endl;
	output <<  '\t' << '\t' << '\t' << '\t' <<  env.receiver.Street << endl;
	output <<  '\t' << '\t' << '\t' << '\t' <<  env.receiver.City  << ", "
                      <<  env.receiver.State  << " "<<  env.receiver.Zip<<endl;

	output.close();


}

It created the file, but didn't dump any of the data.
  #6  
Old 22-May-2006, 15:05
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: File Output - Using Variable for FileName


Thanks, I figured out my problem. I appreciate your help!
  #7  
Old 22-May-2006, 15:19
jdbrine jdbrine is offline
Junior Member
 
Join Date: May 2006
Posts: 56
jdbrine is on a distinguished road

Re: File Output - Using Variable for FileName


If I wanted to append a filetype, .txt, would I be able to using this same example?

CPP / C++ / C Code:
void WriteEnvelope(Envelope env)
{

	ofstream output;
	
	//#define FILE_OUT "Env.txt"

	std::string strFilename;
	
	strFilename=env.receiver.LName;

	
	output.open( strFilename.c_str(), ios_base::out );
	
	//output.open(FILE_OUT, ios::out);

	output << env.sender.FName << " " << env.sender.LName << endl;
	output << env.sender.Street << endl;
	output << env.sender.City << ", " <<env.sender.State << " " 
		   <<env.sender.Zip << endl << endl << endl;
	output << '\t' << '\t' << '\t' << '\t' << env.receiver.FName << " "
		   <<env.receiver.LName << endl;
	output << '\t' << '\t' << '\t' << '\t' << env.receiver.Street << endl;
	output << '\t' << '\t' << '\t' << '\t' << env.receiver.City << ", "
		   << env.receiver.State << " " <<env.receiver.Zip << endl;

	output.close();


}

  #8  
Old 22-May-2006, 15:32
davis
 
Posts: n/a

Re: File Output - Using Variable for FileName


Quote:
Originally Posted by jdbrine
If I wanted to append a filetype, .txt, would I be able to using this same example?


The user can just enter filename.txt or you can simply append a desired file extension to the string.

CPP / C++ / C Code:
std::string strFilename;
strFilename += ".txt";
strFilename=env.receiver.LName;


:davis:
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Airport Log program using 3D linked List : problem reading from file batrsau C Programming Language 11 29-Feb-2008 08:44
empty output file Marjolein C++ Forum 8 17-Sep-2004 10:10
CD burner wont burn!! robertli55 Computer Hardware Forum 1 18-Jun-2004 11:53
Yet another CD burner problem: Lite-On LSC-24082K Erwin Computer Hardware Forum 1 22-May-2004 12:28
using vector or array oshiotse C++ Forum 4 16-Apr-2004 11:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:23.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.