473,386 Members | 1,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Question on ofstream

Quick question on c++ ofstream. in my main i have it declared as int main(int arg, char*argv[]). so that when i run the following command ./program.cpp test.txt it will run my program and pass the test.txt file into it. My question is how do i save my output file as the file name i passed in. i know to use ofstream but im not sure how to pass in the file i passed into my program to test. I have the following code.any help would be great. thanks

ofstream myfile(" ", ios::out);
Apr 22 '09 #1
12 3313
Nepomuk
3,112 Expert 2GB
The filename will be saved in the String argv[1], so just use that as the filename you give to your ofstream.

Greetings,
Nepomuk
Apr 22 '09 #2
ok i think i understand that concept. Im just not sure if im coding it right.. here is how i have tried to implement it..but it doesn not work..


int main(int arg, char *argv[]){
string fname= argv[1];
ofstream myfile( fname, ios::out);
Apr 23 '09 #3
weaknessforcats
9,208 Expert Mod 8TB
You cannot use a string object as the argument.

Either do this:
Expand|Select|Wrap|Line Numbers
  1. int main(int arg, char *argv[]){
  2. ofstream myfile( argv[1], ios::out); 
or do this:

Expand|Select|Wrap|Line Numbers
  1. int main(int arg, char *argv[]){
  2. string fname= argv[1];
  3. ofstream myfile( fname.c_str(), ios::out); 
Apr 23 '09 #4
Banfa
9,065 Expert Mod 8TB
Also before you do anything with argv[1] you should check that argc > 1 otherwise you will be using a NULL pointer.
Apr 23 '09 #5
so i get the following error ...
"terminate called after throwing an instance of 'std::bad_alloc'
what(): St9bad_alloc
Abort trap

not sure why but it might have to do with me passing the argc into my parser function.??



this is the code i have.

int main(int argc, char *argv[]){

string fname= argv[1];
ofstream myfile(fname.c_str(), ios::out);

string c = argv[1];
file_parser parser(c);
Apr 24 '09 #6
weaknessforcats
9,208 Expert Mod 8TB
Odd you would get a run-time error on code that won't compile:

Expand|Select|Wrap|Line Numbers
  1. string c = argv[1];
  2. file_parser parser(c); 
That call to parser looks like it returns a file_parser variable. If so the call should be:

Expand|Select|Wrap|Line Numbers
  1. string c = argv[1];
  2. file_parser x = parser(c); 
Apr 24 '09 #7
well before i started trying to pass the argument as the file name to be saved the following code compiled and ran just fine...

int main(int argc, char *argv[]){

string c = argv[1];
file_parser parser(c);
parser.read_file();


but i am confused by your following code...

string c=argv[1];
file_parser x (c);
Apr 27 '09 #8
Banfa
9,065 Expert Mod 8TB
This

file_parser parser(c);

and this

file_parser x (c);

are exquivilent. One creates avariable called parser the other creates a variable called x.
Apr 27 '09 #9
weaknessforcats
9,208 Expert Mod 8TB
file_parser parser(c);

and this

file_parser x (c);

are exquivilent. One creates avariable called parser the other creates a variable called x.
The OP says that parser is a function and not a variable. See Post #6.
Apr 27 '09 #10
Banfa
9,065 Expert Mod 8TB
@weaknessforcats
No he says he has a parser function, not a function called parser. Nit picky I know but that then makes sense of the code :D
Apr 27 '09 #11
ya guys im not sure why i keep getting this problem..but let me give you a more detailed run down..

my project works fine when i give ofstream a string to name the file..
so the code looks like this.. ofstream myfile("file.lis", ios::out);

then down the road a bit i do

string c = argv[1];
where argv[1] is the file i am calling when i type the following command..
"./sicxe_asm test.asm"

then i have

file_parser parser(c);
this is my constructor that takes a file name as a string.

then i call my actual parser function

parser.read_file();
and everything wroks fine.


but if i try to pass argv[1] in the ofstream i get the crazy mem allocation error
so this code does not work for me..
string fname=argv[1];
ofstream myfile(fname.c_str(), ios::out)

so hopefully that helps explain my problem. i dont know whats wrong. but thanks for all the help so far.
Apr 27 '09 #12
just wanted to post my solution to my problem. after a bit of reading and refreshing on pointers, I was able to solve my problem with the following code..

filename= *argv[1];
for(int i=0;i<filename.length();i++)
{
filename=argv[1]
)

ofstream myfile(filename.c_str(), ios::out);


and this worked for me. I used the for loop to iterate from the starting positon of *argv[1] and increment to get my whole word i was looking for.

thanks for all of your help

--Anthony
Apr 28 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: red floyd | last post by:
Is there any way to retrieve the filename given to a std::ofstream (passed in constructor or in ofstream::open())? Or, should I derive from ofstream (should probably be a template to handle...
2
by: slyphiad | last post by:
i'm kinda new at c++ so be patient ^_^ i was just wondering if u guys could help me to solve this problem that i had. i'm trying to create 5 sequential files using ofstream. this is what i...
13
by: Kitty | last post by:
Hi, I have a question about vector. Suppose that I have "vector<Object>" container, where Object is some class. I would like to output this vector<Object> into a file to save. Then, in the next...
5
by: Squid Seven | last post by:
I'm trying to use a pointer to an ofstream object and having problems: ofstream *sessionFile = NULL; if( directory == "" ) sessionFile = new ofstream( fileName.c_str(), ios::out ); else {
3
by: Peng Yu | last post by:
I'm going to do something like this #define INSPECT( FILENAME,) \ ofstream FILENAME("FILENAME.out");\ For example, I want "INSPECT(abc)" open a file with name "abc.out", the file object's...
2
by: Harry | last post by:
Hi all, I am writing a logger program which can take any datatype. namespace recordLog { enum Debug_Level {low, midium, high}; class L { std::ofstream os; Debug_Level cdl; const...
1
by: gdarian216 | last post by:
okay I had a code that asked user to input a file name and instead i changed it to take the input from the command line. I now want to output results of some functions to an output file. I have...
13
by: John Simeon | last post by:
I understand that to creat a file output stream for example, I have to include <fstreamand then: ofstream fout; which will create an object of type ofstream that I can use to interact with a...
8
by: kevin | last post by:
Hello! So I was reading O'Reilly's C++ in a Nutshell when I came accross something interesting: The class definition for basic_ostream contains numerous overloaded operator<< functions: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.