473,387 Members | 1,745 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,387 software developers and data experts.

Variable Filenames

vninja
40
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
Nov 6 '06 #1
6 2344
sicarie
4,677 Expert Mod 4TB
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?
Nov 6 '06 #2
vninja
40
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
Nov 6 '06 #3
sicarie
4,677 Expert Mod 4TB
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:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main() {
  7.     ofstream outFile;
  8.     char filename[255];
  9.     cin >> filename;
  10.     outFile.open(filename);
  11.     outFile << "Hello, world\n";
  12.     outFile.close();
  13.     return 0;
  14. }
  15.  
  16.  
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.
Nov 6 '06 #4
sicarie
4,677 Expert Mod 4TB
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:

Expand|Select|Wrap|Line Numbers
  1. string filename;
  2. getline(std::cin,filename);
  3. ifstream input(filename.c_str());
  4.  
  5.  
Nov 6 '06 #5
vninja
40
Thx didn't know you cin would work with arrays. i'll try it out. thx again
Nov 7 '06 #6
vninja
40
yea i do the same thing with the if an of streams
Nov 7 '06 #7

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

Similar topics

3
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...
9
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 ...
2
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...
0
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...
22
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...
1
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...
2
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');....
8
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...
0
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...
3
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...
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: 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$) { } ...
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: 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...
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
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.