Solved

Accessing a File

Posted on 2000-05-13
2
189 Views
Last Modified: 2010-04-02
The following code is part of what I have at the moment:
while (!Done)
{
   string FileName = FindDat.cFileName;
   FileList.push_back(FileName);
   Done = !FindNextFile(FndHnd, &FindDat);
}
if (FndHnd)
   FindClose(FndHnd);
const int ItmCnt = FileList.size();
if (ItmCnt<12)
{
      for (int i=0; i<11; ++i)
      {
            gotoxy(menux, ++menuy);
            if (i<ItmCnt)
                  cout << "* " << FileList[i];
            if (i>=ItmCnt)
                  cout << "* -EMPTY SLOT-" << endl;
      }
      x-=13;
      y+=2;
      char *barrier={"T08;B18"};
      movecursor(&x,&y,0,1,barrier,0,'\0','\0','\0', player);
      if (y-8<ItmCnt)
      {
            char ch;
            ifstream fin(FileList[y-8]);
            while (fin.get(ch))
            {
                  cout << char(ch);
            }
            fin.close();
      }

The part I'm having trouble is when I try to read the file.  This part: ifstream fin(FileList[y-8]); doesn't seem to work, since I'm sending in a vector instead of a char.  The strcpy doesn't seem to work either.  Is there any way of copying a vector <string> to a char?  Or, is there another workaround for this?
0
Comment
Question by:junkyboy
2 Comments
 
LVL 7

Accepted Solution

by:
KangaRoo earned 75 total points
ID: 2806599
Looks like you are using
vector<sting> FileList;
Then the problem is that FileList[y-8] returns a string while ifstream's constructor expects a char* (c-style) string. You can obtain the char* string from a string class object by it;s member function c_str(). So
ifstream fin(FileList[y-8].c_str());
might work here.
0
 

Author Comment

by:junkyboy
ID: 2806602
Thanks!  It worked perfectly!
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question