Question on ofstream

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • yeshello54
    New Member
    • Mar 2009
    • 54

    Question on ofstream

    Quick question on c++ ofstream. in my main i have it declared as int main(int arg, char*argv[]). so that when i run the following command ./program.cpp test.txt it will run my program and pass the test.txt file into it. My question is how do i save my output file as the file name i passed in. i know to use ofstream but im not sure how to pass in the file i passed into my program to test. I have the following code.any help would be great. thanks

    ofstream myfile(" ", ios::out);
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    The filename will be saved in the String argv[1], so just use that as the filename you give to your ofstream.

    Greetings,
    Nepomuk

    Comment

    • yeshello54
      New Member
      • Mar 2009
      • 54

      #3
      ok i think i understand that concept. Im just not sure if im coding it right.. here is how i have tried to implement it..but it doesn not work..


      int main(int arg, char *argv[]){
      string fname= argv[1];
      ofstream myfile( fname, ios::out);

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        You cannot use a string object as the argument.

        Either do this:
        Code:
        int main(int arg, char *argv[]){
        ofstream myfile( argv[1], ios::out);
        or do this:

        Code:
        int main(int arg, char *argv[]){
        string fname= argv[1];
        ofstream myfile( fname.c_str(), ios::out);

        Comment

        • Banfa
          Recognized Expert Expert
          • Feb 2006
          • 9067

          #5
          Also before you do anything with argv[1] you should check that argc > 1 otherwise you will be using a NULL pointer.

          Comment

          • yeshello54
            New Member
            • Mar 2009
            • 54

            #6
            so i get the following error ...
            "terminate called after throwing an instance of 'std::bad_alloc '
            what(): St9bad_alloc
            Abort trap

            not sure why but it might have to do with me passing the argc into my parser function.??



            this is the code i have.

            int main(int argc, char *argv[]){

            string fname= argv[1];
            ofstream myfile(fname.c_ str(), ios::out);

            string c = argv[1];
            file_parser parser(c);

            Comment

            • weaknessforcats
              Recognized Expert Expert
              • Mar 2007
              • 9214

              #7
              Odd you would get a run-time error on code that won't compile:

              Code:
              string c = argv[1];
              file_parser parser(c);
              That call to parser looks like it returns a file_parser variable. If so the call should be:

              Code:
              string c = argv[1];
              file_parser x = parser(c);

              Comment

              • yeshello54
                New Member
                • Mar 2009
                • 54

                #8
                well before i started trying to pass the argument as the file name to be saved the following code compiled and ran just fine...

                int main(int argc, char *argv[]){

                string c = argv[1];
                file_parser parser(c);
                parser.read_fil e();


                but i am confused by your following code...

                string c=argv[1];
                file_parser x (c);

                Comment

                • Banfa
                  Recognized Expert Expert
                  • Feb 2006
                  • 9067

                  #9
                  This

                  file_parser parser(c);

                  and this

                  file_parser x (c);

                  are exquivilent. One creates avariable called parser the other creates a variable called x.

                  Comment

                  • weaknessforcats
                    Recognized Expert Expert
                    • Mar 2007
                    • 9214

                    #10
                    Originally posted by Banfa
                    file_parser parser(c);

                    and this

                    file_parser x (c);

                    are exquivilent. One creates avariable called parser the other creates a variable called x.
                    The OP says that parser is a function and not a variable. See Post #6.

                    Comment

                    • Banfa
                      Recognized Expert Expert
                      • Feb 2006
                      • 9067

                      #11
                      Originally posted by weaknessforcats
                      The OP says that parser is a function and not a variable. See Post #6.
                      No he says he has a parser function, not a function called parser. Nit picky I know but that then makes sense of the code :D

                      Comment

                      • yeshello54
                        New Member
                        • Mar 2009
                        • 54

                        #12
                        ya guys im not sure why i keep getting this problem..but let me give you a more detailed run down..

                        my project works fine when i give ofstream a string to name the file..
                        so the code looks like this.. ofstream myfile("file.li s", ios::out);

                        then down the road a bit i do

                        string c = argv[1];
                        where argv[1] is the file i am calling when i type the following command..
                        "./sicxe_asm test.asm"

                        then i have

                        file_parser parser(c);
                        this is my constructor that takes a file name as a string.

                        then i call my actual parser function

                        parser.read_fil e();
                        and everything wroks fine.


                        but if i try to pass argv[1] in the ofstream i get the crazy mem allocation error
                        so this code does not work for me..
                        string fname=argv[1];
                        ofstream myfile(fname.c_ str(), ios::out)

                        so hopefully that helps explain my problem. i dont know whats wrong. but thanks for all the help so far.

                        Comment

                        • yeshello54
                          New Member
                          • Mar 2009
                          • 54

                          #13
                          just wanted to post my solution to my problem. after a bit of reading and refreshing on pointers, I was able to solve my problem with the following code..

                          filename= *argv[1];
                          for(int i=0;i<filename. length();i++)
                          {
                          filename=argv[1]
                          )

                          ofstream myfile(filename .c_str(), ios::out);


                          and this worked for me. I used the for loop to iterate from the starting positon of *argv[1] and increment to get my whole word i was looking for.

                          thanks for all of your help

                          --Anthony

                          Comment

                          Working...