LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-23-2008, 04:27 PM   #1
tnjones
LQ Newbie
 
Registered: Aug 2008
Posts: 27

Rep: Reputation: 15
How to read filenames from a vector and open them in C++


Hello,
I am trying to write a program in C++ that stores all of the names of textfiles in a current directory in a vector,once having the filenames I need to open each and disply all vaild e-mail addresses. At this time I have it getting all filenames and stroing them in a vector. I even have a test program that is reading files and displaying all valid addresses. However, when trying to put everything together,I am having problem with opening the files(names stored in array). I have a loop to go through the vector and read and store the filename to a variable and then use the traditonal ifstream to open the file but it isn't working.Below is just a sample program where I have strored filenames in the vector and trying to open the files.

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <fstream>

using namespace std;
int main()
{
vector<string> vec;
string filename;
string line;
ifstream myfile;
vec.push_back("example.txt"); vec.push_back("atest.txt"); vec.push_back("backup.txt");
sort(vec.begin(), vec.end());
for (int i = 0; i < vec.size(); i++)
{
// cout << vec[i] << endl;
filename = vec[i];
myfile.open("filename");

if (myfile.is_open())
{
while (! myfile.eof() )
{
getline (myfile,line);
cout << line << endl;
}

myfile.close();
}
else cout << "Unable to open file";

}

return 0;
}

With this in mind, how do you open filenames that are stored in a vector?Any help will be greatly appreciated!!
 
Old 08-24-2008, 03:53 AM   #2
Aidin_36
LQ Newbie
 
Registered: Jun 2005
Location: Iran
Distribution: Debian
Posts: 24

Rep: Reputation: 15
Seems the problem is this line:

myfile.open("filename");

`filename' is the name of a variable, and you should write:

myfile.open(filename);

That should solve your problem!

NOTE: It's a good idea to use `at' function instead of those []. so change `filename = vec[i];' to `filename = vec.at(i);' But it's not neccessary.
 
Old 08-24-2008, 08:48 AM   #3
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
You should also use iterators rather than an index, and declare variables as late as possible:
Code:
for (std::vector<std::string>::const_iterator end = vec.end(), iter = vec.begin();iter != end;++iter)
{
   std::ifstream file(*iter);
   for(!file.eof())
   {
      std::string line;
      std::getline(file, line);
      std::cout << line << endl;
   }
}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: Inkscape - Open Source Vector Graphics Editor LXer Syndicated Linux News 0 07-02-2006 09:03 PM
LXer: Open source vector graphics app turns 0.5 LXer Syndicated Linux News 0 05-28-2006 08:33 PM
Can't read greek filenames! Braveheart1980 Linux - Software 18 11-10-2004 06:37 PM
C++ read csv file row into vector taban1 Programming 3 11-08-2004 02:01 PM
Windows unable to read full filenames from CD burnt on linux wearetheborg Linux - Software 6 09-14-2004 06:40 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:38 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration