help with ofstream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • McCarthy
    New Member
    • May 2007
    • 7

    help with ofstream

    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
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

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

    Comment

    • McCarthy
      New Member
      • May 2007
      • 7

      #3
      Originally posted by Ganon11
      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.

      Comment

      • ilikepython
        Recognized Expert Contributor
        • Feb 2007
        • 844

        #4
        Originally posted by McCarthy
        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.

        Comment

        • McCarthy
          New Member
          • May 2007
          • 7

          #5
          Originally posted by ilikepython
          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("g en.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(ge n_prefix+id."tx t") wont work
          Im not sure how to do that.

          Comment

          • ilikepython
            Recognized Expert Contributor
            • Feb 2007
            • 844

            #6
            Originally posted by McCarthy
            Yea I know that i could do outFile.open("g en.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(ge n_prefix+id."tx t") 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:
            Code:
            char filename[20];
            strcpy(filename, gen_prefix);
            strcat(filename, gen_number);
            strcat(filename, ".txt");
            outFile.open(filename);
            Does that work for you?

            Comment

            • McCarthy
              New Member
              • May 2007
              • 7

              #7
              Originally posted by ilikepython
              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:
              Code:
              char filename[20];
              strcpy(filename, gen_prefix);
              strcat(filename, gen_number);
              strcat(filename, ".txt");
              outFile.open(filename);
              Does that work for you?
              For some reason the compiler is giving me an error regarding
              outFile.open(fi lename) saying there is no matching function for call to std.basic ofstream

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                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.

                Comment

                Working...