constructing filename string at runtime

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • drsmooth
    New Member
    • Oct 2007
    • 112

    constructing filename string at runtime

    i need to open a file whose name i only know part of until runtime.

    it is based on a char value.
    the char represents gender and is either 'm' or ''f'

    i need to load from a text file called names_m.txt or names_f.txt but i don't know until runtime.

    i tried this but it didnt quite work...

    Code:
     char fileName[18] = {"/data/names_"};
      fileName[13] = gender;
      fileName[14] = '.';
      fileName[15] = 't';
      fileName[16] = 'x';
      fileName[17] = 't';
      fileName[18] = '\0';
    i want to use an fstream to open the file in the folder 'data' at the working directory
  • Firecore
    New Member
    • Jul 2007
    • 114

    #2
    You couls use something like the sprintf() function.
    I think that formats strings like printf except instead of printing them, it saves the string in a variable.

    Look it up in MSDN for more info.

    I think it mite help u.

    Comment

    • drsmooth
      New Member
      • Oct 2007
      • 112

      #3
      actually i ran through it and the problem seems to be in the "/data/" part...is there something wrong with the way i access the file...

      like i need to access the folder 'data' in the working directory

      Comment

      • Studlyami
        Recognized Expert Contributor
        • Sep 2007
        • 464

        #4
        I would use a string as a variable (are you unable to do this for some reason).

        Code:
        string Test = "data//names_";
        Test += gender;
        Test +=".txt";
        
        FILE * f_ptr;
        
        f_ptr = fopen(Test.c_str(), <flags>);
        Another problem is the /data/filename

        you need "data//names_" a single line is used for special operations like new line \n.

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Studlyami, why use a FILE* in C++? If strings are available, then so are ifstreams and ofstreams.

          Then again, if it's not C++ but C, then strings are not available, and you must use a char array.

          Something else I see wrong is the fact that you declare filename to be 18 chars long (with indeces 0-17), but then try to use fileName[18] (which is out of bounds).

          Comment

          • Studlyami
            Recognized Expert Contributor
            • Sep 2007
            • 464

            #6
            I guess because its what I'm comfortable with. If you can't use strings add a second / to you array and change the size appropriately and it should work.

            Comment

            • oler1s
              Recognized Expert Contributor
              • Aug 2007
              • 671

              #7
              I guess because its what I'm comfortable with.
              That's a poor excuse. You'll turn everything into a nail because you can only use a hammer?

              Comment

              • Studlyami
                Recognized Expert Contributor
                • Sep 2007
                • 464

                #8
                I know it is. It was more of a problem of ignorance than laziness. Thats one of the reasons why i stay around this site. Theres always room for improvement.

                Comment

                • drsmooth
                  New Member
                  • Oct 2007
                  • 112

                  #9
                  could someone maybe show me an example of this with the fstream or ifstream?

                  i tried this:

                  Code:
                  void Civilian::assignName()
                  {
                   string test = "data//names_";
                   test += gender;
                   test +=".txt";
                   fstream file(test, ios::in);
                   string g;
                   file>>g;
                   cout<<g;
                  }
                  but i must have set up the fstream wrong i got:

                  [C++ Error] Civilian.cpp(36 ): E2285 Could not find a match for 'fstream::basic _fstream(string ,enum)'

                  Comment

                  • Studlyami
                    Recognized Expert Contributor
                    • Sep 2007
                    • 464

                    #10
                    As you read above i havn't used ifstream or ofstream, but here is a reference on how to use these two streams .

                    Comment

                    • weaknessforcats
                      Recognized Expert Expert
                      • Mar 2007
                      • 9214

                      #11
                      Originally posted by drsmooth
                      [C++ Error] Civilian.cpp(36 ): E2285 Could not find a match for 'fstream::basic _fstream(string ,enum)'
                      Did you include <fstream>??

                      Comment

                      Working...