473,480 Members | 2,170 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

help with ofstream

7 New Member
My assingment is to write a command line program in Unix. This program takes 3 arguments. The third argument is a generation prefix. I am suppose to print out to a text file in the format of <gen_prefix> + < generation number > + ".txt"
If the gen_prefix is "gen", then the base Generation file will be: "gen0.txt"
Generation 1 will be "gen1.txt"
Im not sure how to create or declare such a file.
Please help
May 4 '07 #1
7 2834
Ganon11
3,652 Recognized Expert Specialist
Which part are you having trouble with? Getting the arguments passed to your program? Finding out the textfile name? Creating the ofstream object? Writing the data?
May 4 '07 #2
McCarthy
7 New Member
Which part are you having trouble with? Getting the arguments passed to your program? Finding out the textfile name? Creating the ofstream object? Writing the data?
Creating the ofstream object. I know that i have to declare the outfile by ofstream outFile, but the problem is when I want to open the outile using outFile.open(). Im not sure what to put in the parenthesis.
May 4 '07 #3
ilikepython
844 Recognized Expert Contributor
Creating the ofstream object. I know that i have to declare the outfile by ofstream outFile, but the problem is when I want to open the outile using outFile.open(). Im not sure what to put in the parenthesis.
You would put the name of the file you would want to open. There is also a second arguement that determines what mode you open the file in but it is only optional.
May 4 '07 #4
McCarthy
7 New Member
You would put the name of the file you would want to open. There is also a second arguement that determines what mode you open the file in but it is only optional.
Yea I know that i could do outFile.open("gen.txt") but since the file name depends on the third argument my program recieves plus an int that increments in the prog. as I posted above, and I know outFile.open(gen_prefix+id."txt") wont work
Im not sure how to do that.
May 4 '07 #5
ilikepython
844 Recognized Expert Contributor
Yea I know that i could do outFile.open("gen.txt") but since the file name depends on the third argument my program recieves plus an int that increments in the prog. as I posted above, and I know outFile.open(gen_prefix+id."txt") wont work
Im not sure how to do that.
Oh ok, I'm sorry if I misunderstood you.
You can try using strcat() and strcpy(). Strcat adds two strings together and strcpy copies a string into another:
Expand|Select|Wrap|Line Numbers
  1. char filename[20];
  2. strcpy(filename, gen_prefix);
  3. strcat(filename, gen_number);
  4. strcat(filename, ".txt");
  5. outFile.open(filename);
  6.  
Does that work for you?
May 4 '07 #6
McCarthy
7 New Member
Oh ok, I'm sorry if I misunderstood you.
You can try using strcat() and strcpy(). Strcat adds two strings together and strcpy copies a string into another:
Expand|Select|Wrap|Line Numbers
  1. char filename[20];
  2. strcpy(filename, gen_prefix);
  3. strcat(filename, gen_number);
  4. strcat(filename, ".txt");
  5. outFile.open(filename);
  6.  
Does that work for you?
For some reason the compiler is giving me an error regarding
outFile.open(filename) saying there is no matching function for call to std.basic ofstream
May 4 '07 #7
Ganon11
3,652 Recognized Expert Specialist
Is filename a string (std::string) or an array of characters? The .open() function requires an array of characters to work. If you are using a string, it can still work, but you must use filename.c_str() to make it work.
May 4 '07 #8

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

Similar topics

0
2493
by: james545 | last post by:
I am having a problem with ofstream creating files. Sometimes when I run the program, ofstream creates a new file for writing and writes to it. No problem. But sometimes it fails to create the...
4
2894
by: Tom Johnson | last post by:
Hi all, I cant believe that Im stuck on such simple code, but I am so... Im trying to open a text file for writing but can never get the file to initially open. Heres what Im having trouble with....
5
2815
by: titan0111 | last post by:
#include<iostream> #include<iomanip> #include<cstring> #include<fstream> using namespace std; class snowfall { private: int ft;
0
1272
by: fasteddie203 | last post by:
Hi, I hope this is appropriate for standard C++ group, I apologize if it is not. I'm trying to print to LPT1 using ofstream which works fine as long as the printer is connected - it doesn't need...
1
1598
by: tvn007 | last post by:
could someone please help me to point out what wrong with the code below ? #define OUPUT_DIR "/home/user/OUT/test.out" if (appenndmode=="YES"){ ofstream out(OUTPUT_DIR,ios::app); //error:...
5
1739
by: Jintty | last post by:
Hi, I'm trying to write a program that will read a txt file, copy it into another text file and display the number of words, lines and paragraphs. I was able to get the copying portion done, but...
3
1629
by: ern | last post by:
Is there a good example of simple usage of ofstream ? Also, I need to create a FOLDER before I create a file to write to. Is this possible ? If so, how ?
4
1817
by: lsu420luv | last post by:
'i have been working on this assignment for like 45 hours. the file reads from Piano.data and outputs into report.out. I have everything working, except for the fact that it will not repeat until...
11
1616
by: Kush | last post by:
Hello All, I am having problem writing some data to file and I do not understand my mistake / misconception. I am trying to write some simple text to file but the text is rather output to...
6
2842
by: priyajohal | last post by:
#include<fstream.h> #include<process.h> #include<stdlib.h> #include<conio.h> #include<string.h> #include<dos.h> #include<ctype.h> #include<stdio.h> void setup() void help();
0
7055
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
6920
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
7060
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6760
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7022
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4799
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4501
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
206
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.