|
-
March 4th, 2011, 11:12 AM
#1
[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:
still no dice
any idea what i am doing wrong?thanks
-
March 4th, 2011, 11:14 AM
#2
Re: problem using vector for file directory
What's a string file? What's with that goofy indentation style?
-
March 4th, 2011, 11:15 AM
#3
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.
-
March 4th, 2011, 11:29 AM
#4
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.
-
March 4th, 2011, 11:35 AM
#5
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.
-
March 4th, 2011, 12:08 PM
#6
Re: problem using vector for file directory
Code:
fstream logFile;
logFile.open(files[0].c_str());
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.
-
March 4th, 2011, 12:10 PM
#7
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;
}
-
March 4th, 2011, 12:16 PM
#8
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.
-
March 4th, 2011, 12:42 PM
#9
Re: problem using vector for file directory
 Originally Posted by josh26757
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().
-
March 4th, 2011, 03:58 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|