Variable Filenames

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • vninja
    New Member
    • Oct 2006
    • 40

    Variable Filenames

    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(fi lename);


    any help appriciated thx
  • sicarie
    Recognized Expert Specialist
    • Nov 2006
    • 4677

    #2
    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?

    Comment

    • vninja
      New Member
      • Oct 2006
      • 40

      #3
      my problem lies within the outfile.open(fi lename)

      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.(ifstr eam outfile;).

      above is my challege. thx for help

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        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:

        Code:
        #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.

        Comment

        • sicarie
          Recognized Expert Specialist
          • Nov 2006
          • 4677

          #5
          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:

          Code:
          string filename;
          getline(std::cin,filename);
          ifstream input(filename.c_str());

          Comment

          • vninja
            New Member
            • Oct 2006
            • 40

            #6
            Thx didn't know you cin would work with arrays. i'll try it out. thx again

            Comment

            • vninja
              New Member
              • Oct 2006
              • 40

              #7
              yea i do the same thing with the if an of streams

              Comment

              Working...