473,511 Members | 15,624 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

very basic c++ question hopefully

Okay, so I am doing a project for my beginning C++ course, and I'm not
too familiar with file I/O. Here is my code so far.

--------beginning-------
char filename[20];
//user output/input
cout << "please enter the name of a file for output\n";
cout << "(20 character max):";
cin >> filename;
ofstream outStream;
outStream.open(filename);
------end------

how do i write this file as a .txt without causing hell?? is there a
different way I can rewrite this, because when i write this, it just
creates the file as whatever the user inputs, and if I put "" around
the filename, it nullifies the users input....

please help!

Dec 1 '05 #1
3 1073
kdubs wrote:
Okay, so I am doing a project for my beginning C++ course, and I'm not
too familiar with file I/O. Here is my code so far.

--------beginning-------
char filename[20];
//user output/input
cout << "please enter the name of a file for output\n";
cout << "(20 character max):";
cin >> filename;
ofstream outStream;
outStream.open(filename);
Place this line at the top of your file
#include <string>

Replace this:
char filename[20];

with:
std::string filename;

AND NEVER EVER read from a file in an unconditional way into a fixed
length array like that ever again. This construct allows for "buffer
overflow" and is the main cause for your Internet Exploder security
breaches. It may be a bit early to introduce you to that concept but
security IS A BIG issue.

Then you can add your favorite file suffix.
outStream.open(filename+".txt");


------end------

how do i write this file as a .txt without causing hell?? is there a
different way I can rewrite this, because when i write this, it just
creates the file as whatever the user inputs, and if I put "" around
the filename, it nullifies the users input....

please help!

Dec 1 '05 #2

Gianni Mariani wrote:
kdubs wrote:
Okay, so I am doing a project for my beginning C++ course, and I'm not
too familiar with file I/O. Here is my code so far.

--------beginning-------
char filename[20];
//user output/input
cout << "please enter the name of a file for output\n";
cout << "(20 character max):";
cin >> filename;
ofstream outStream;
outStream.open(filename);


Place this line at the top of your file
#include <string>

Replace this:
char filename[20];

with:
std::string filename;

AND NEVER EVER read from a file in an unconditional way into a fixed
length array like that ever again. This construct allows for "buffer
overflow" and is the main cause for your Internet Exploder security
breaches. It may be a bit early to introduce you to that concept but
security IS A BIG issue.

Then you can add your favorite file suffix.
outStream.open(filename+".txt");


You'll need to convert that to a C-style string:

filename += ".txt";
ofstream outStream( filename.c_str() );

Cheers! --M

Dec 1 '05 #3
thank you all...

Dec 1 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

42
9640
by: Steven O. | last post by:
I am seeking some kind of tool that I can use for GUI prototyping. I know how to use Visual Basic, but since a lot of software is being coded in Java or C++, I'd like to learn a Java or C++ -based...
5
7207
by: K. Shier | last post by:
when attempting to edit code in a class file, i see the bug "Visual Basic ..NET compiler is unable to recover from the following error: System Error &Hc0000005&(Visual Basic internal compiler...
1
11854
by: ANDRES BECERRA | last post by:
Herfried K. Wagner was kind enough to point me to the PropertyGrid control http://msdn.microsoft.com/library/en-us/cpref/html/frlrfsystemwindowsformspropertygridclasstopic.asp I have found a few...
9
7679
by: slimicus | last post by:
The IDE crashes about every 10-20 minutes for me.. its been doing this ever since I started this project (The project was a 2003 project but I recreated it in 2005) When it crashes, its...
13
1693
by: Jack B | last post by:
I'm using Access 2002 to create a database for a small opera company that my wife is involved in, and I'm more than a bit rusty because I haven't created a new Access database since about 1999. ...
0
7252
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
7432
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...
1
7093
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
7517
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
5077
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
4743
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...
0
3230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3218
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
452
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.