Hey there pretty new to C++ and i'm trying to create a file that will create a file based on user input
here is a little snipplet of my code:
getline(cin,/*string*/ filename,'\n');
filename= " \" " + filename +" \" ";
outfile.open(filename);
any help appriciated thx
6 2344
vninja-
What's your question? Is there an error message you could post as well? Have you tried using cin, or is your problem not with getline?
Also, if you run that code, you are just putting a pair of quotes around the filename making, for file foo.txt, the variable hold
"foo.txt"
Why do you try to do this before you open it?
my problem lies within the outfile.open(filename)
because of the syntax of the open() function quotes are required around the filename therefore i need to add them, otherwise if i use open("filename") it takes filename literally and not the var filename.
[C++ Error] File1.cpp(12): E2034 Cannot convert 'string' to 'const char *'
[C++ Error] File1.cpp(12): E2342 Type mismatch in parameter '__s' (wanted 'const char *', got 'string')
and i apologize its ifstream and not ofstream.(ifstream outfile;).
above is my challege. thx for help
You're right, it's ofstream. I'm not sure exactly what is going on there, but I got it to work (somewhat) like this: -
#include <iostream>
-
#include <fstream>
-
#include <string>
-
using namespace std;
-
-
int main() {
-
ofstream outFile;
-
char filename[255];
-
cin >> filename;
-
outFile.open(filename);
-
outFile << "Hello, world\n";
-
outFile.close();
-
return 0;
-
}
-
-
It's not working exactly right on my *nix box, I can't use more to display it, but I can use cat to show the 'Hello, world', but that will probably get you started, and you can play with it from there.
Whoops, I always get confused just looking at one or the other ifstream is input file stream, ofstream is output file stream. You want ifstream. Sorry.
Try this: -
string filename;
-
getline(std::cin,filename);
-
ifstream input(filename.c_str());
-
-
Thx didn't know you cin would work with arrays. i'll try it out. thx again
yea i do the same thing with the if an of streams
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Ralph Freshour |
last post by:
I have a PHP web app using MySQL - when I save a .jpg file named
test's.jpg I see that the filename on the unix server is: test\'s.jpg
- the filename I end up saving in my SQL table is named...
|
by: hokiegal99 |
last post by:
This script works as I expect, except for the last section. I want the
last section to actually remove all spaces from the front and/or end of
filenames. For example, a file that was named " test ...
|
by: rbutch |
last post by:
guys, i need a little help with this.
this is working (well sort of)
i get the info, but it's not moving to a new line as it iterates thru the array
and all of the fields are like ONE HUGE LONG...
|
by: n33470 |
last post by:
We have a web site that is being converted from the 1.1 format into
2.0. I've noticed that after the web project has been converted, the
first time that the solution is opened in VS, all of the...
|
by: rtilley |
last post by:
# Spaces are present before and after the XXX
filename = ' XXX '
new_filename = filename.strip()
if new_filename != filename:
print filename
Macs allow these spaces in file and folder...
|
by: shahparags |
last post by:
Hello,
I am developing a script where one of my Bacth command uses a XML file as an argument.
This XML file contains the Path of the Data to be Converted from one format to another.
Now My...
|
by: Taras_96 |
last post by:
Hi everyone,
Firstly, I would like to know if you can open chinese filenames under
win2000 using PHP 5.0? I have a file named ä¸å›½.php, and try to open it
using
fopen(‘ä¸å›½.php','r');....
|
by: Neil Webster |
last post by:
Hi,
I was wondering whether anybody could help me out.
I have a program, for part of it I am trying to pass a variable to a
glob function, this returns an empty list. The strange thing is...
|
by: chongming |
last post by:
Hi, i want to display all the filenames on browser. However i found that if there are many filenames in that folder, result will be it will display a long list of filenames on that browser.
My...
|
by: Torsten Bronger |
last post by:
Hallöchen!
I'd like to map general unicode strings to safe filename. I tried
punycode but it is case-sensitive, which Windows is not. Thus,
"Hallo" and "hallo" are mapped to "Hallo-" and...
|
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,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
| |