CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Mar 2011
    Posts
    52

    [RESOLVED] problem using vector for file directory

    so i posted some code a few days ago and agree it was hard to read.In response to that i was using vector stacks to group a lot of elements together one being directories to text files.
    I am trying to learn data base like SQL but at the time needed to make this program.
    note:I also would like to know how what you use to save data in a program and it stays when the program exits.

    anyway the problem is I tried to save my directories in a vector like this:
    Code:
    vector<string> files;
           files.push_back("C:/bills/default.txt");
            files.push_back("C:/bills/jan.txt");
             files.push_back("C:/bills/feb.txt");
              files.push_back("C:/bills/mar.txt");
               files.push_back("C:/bills/april.txt");
                files.push_back("C:/bills/may.txt");
                 files.push_back("C:/bills/june.txt");
                  files.push_back("C:/bills/july.txt");
                   files.push_back("C:/bills/aug.txt");
                    files.push_back("C:/bills/sept.txt");
                     files.push_back("C:/bills/oct.txt");
                      files.push_back("C:/bills/nov.txt");
                       files.push_back("C:/bills/dec.txt");
    the reason was so I could make a loop to add and delete elements to all files in one shot
    the loop just view the default file looks like this:
    Code:
    void list(vector<string>files)
    {
         system("CLS");
          string bills;
           string file;
            int counter=0;
             ifstream list;
          
         list.open(files[0]);
           if(list.good())
            {while(!list.eof())
              {getline(list,bills);
                cout<<counter<<"."<<bills<<endl;
                 counter++;
              }
            } 
          system("PAUSE");
          main();  
    }
    I tried to assign the value to a string file:
    Code:
    file=files[0];
    still no dice
    any idea what i am doing wrong?thanks

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: problem using vector for file directory

    What's a string file? What's with that goofy indentation style?

  3. #3
    Join Date
    Apr 2008
    Posts
    118

    Re: problem using vector for file directory

    As an aside to your primary question, that call to main() at the end of your function void list(vector<string>files) - what is that for? Why are you calling main from within a function?

    I think we might need to see your main() function to work out the problem. The code below works as expected.

    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    
    
    int main()
    {
      
      using std::vector;
      using std::string;
      using std::cout;
      
    
    
      vector<string> files;
      files.push_back("C:/bills/default.txt");
      files.push_back("C:/bills/jan.txt");
      files.push_back("C:/bills/feb.txt");
      files.push_back("C:/bills/mar.txt");
      files.push_back("C:/bills/april.txt");
      files.push_back("C:/bills/may.txt");
      files.push_back("C:/bills/june.txt");
      files.push_back("C:/bills/july.txt");
      files.push_back("C:/bills/aug.txt");
      files.push_back("C:/bills/sept.txt");
      files.push_back("C:/bills/oct.txt");
      files.push_back("C:/bills/nov.txt");
      files.push_back("C:/bills/dec.txt");
      string file;
                       
      file=files[0];
      
      cout << file;
      return 0;
      
    }
    Last edited by Moschops; March 4th, 2011 at 11:24 AM.

  4. #4
    Join Date
    Mar 2011
    Posts
    52

    Re: problem using vector for file directory

    reasoning?
    I am very new (about 6 months)give me a reason not to.I am here looking for advice.I dont really know why or why not to do something still figuring it out but here is what i did.the call to main() was just to get back to the beginning of the program.I am rebuilding the original so its just pieces right now.I am really trying to do it right just not sure how.thats why i am here.
    Code:
    int main()
    {
        system("CLS");
         vector<string>files;
          get_files(files);
           menu();
            selection(files);
        
        
        
        return EXIT_SUCCESS;
    }
    whats wrong with the indent?
    oh.I see very sloppy compared to above.I will pay more attention to that.
    Last edited by josh26757; March 4th, 2011 at 11:36 AM.

  5. #5
    Join Date
    Mar 2011
    Posts
    52

    Re: problem using vector for file directory

    i can get the vector to read just cant get the fstream to read it
    Code:
    fstream LogFile;
    LogFile.open(files[0])
    the problem is opening a file with it even if:
    Code:
    file=files[0];
    LogFile.open(file)
    wont work?
    Last edited by josh26757; March 4th, 2011 at 11:39 AM.

  6. #6
    Join Date
    Apr 2008
    Posts
    725

    Re: problem using vector for file directory

    Code:
    fstream logFile;
    logFile.open(files[0].c_str());
    Code:
    fstream::open(...)
    takes a const char *, not a std::string. Handily std::string has a member c_str() that gives a const char *.

    The compiler should have given you some error saying it cannot convert std::string<...> to const char *. Upon receiving that, one might visit http://www.cplusplus.com/reference/string/string/ to see if string can be 'converted' to a (const) char *. I have found http://www.cplusplus.com a useful reference.
    Last edited by Amleto; March 4th, 2011 at 12:11 PM.

  7. #7
    Join Date
    Apr 2008
    Posts
    118

    Re: problem using vector for file directory

    Are you getting any compiler errors? The open function of an ifstream object takes a char* and you are feeding it a std::string. If I try that, the compiler objects.

    Also, double check whether or not your strings describing the location of your files require a double slash (i.e. // instead of /).

    This code works fine and on my system outputs the contents of the file 16.cpp

    Code:
    #include <vector>
    #include <string>
    #include <iostream>
    #include <fstream>
    
    
    int main()
    {
      
      using std::vector;
      using std::string;
      using std::cout;
      using std::ifstream;
      
      
    
    
      vector<string> files;
      files.push_back("16.cpp");
    
      string file;
                       
      file=files[0];
    
      ifstream Logfile;
      Logfile.open(file.c_str(), ifstream::in);
      while (Logfile.good())
        cout << (char) Logfile.get();
    
    
     
      return 0;
      
    }

  8. #8
    Join Date
    Mar 2011
    Posts
    52

    Re: problem using vector for file directory

    thanks everyone I have some more research to do.that should be enough for me to figure it out.
    you guys are the best thanks a ton.

  9. #9
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: problem using vector for file directory

    Quote Originally Posted by josh26757 View Post
    i did.the call to main() was just to get back to the beginning of the program.
    Don't ever call main() yourself, and in general, realize that function calls are not simply "jumps". When the function returns execution will go back to the place it was called from.

    If you wish to get back to the beginning of main(), then return from all functions called from main(), and put a loop in main().

  10. #10
    Join Date
    Mar 2011
    Posts
    52

    Re: problem using vector for file directory

    ah..i see i do remember doing that some months ago on a calculator program.I will refrain from calling main from now on and just use a loop.It is actually easier.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured