Dudearoo
Useing Code::Blocks

Heys you guys! ive got a question, i want to know if its possable to read a file name like for example dudearoo.dat and output the name
ive got an idea for it, just i dont know how to read file name not coded into the program.
if your wondering why ive got a .dat file, im creating a program for minecraft servers and i just need the players names, there not in a
.txt file so i have to look in the players file, i just need to know how to read file names. thanks you Guys! :)

I do know a good amount of File I/O too, if it'll help you guys :)

I don't know if you mean reading the file's name? OR whether you mean reading data from the file and outputting a list of names from the data.

//Read a file's contents and output it to a string. For your purpose, you can even use GetLine which I have not demonstrated.

    #include <iostream>
    #include <fstream>

    using namespace std;

    int main ()
    {
      int length;
      char * buffer;

      ifstream is;
      is.open ("dudaroo.dat", ios::binary );

      // get length of file:
      is.seekg (0, ios::end);
      length = is.tellg();
      is.seekg (0, ios::beg);

      // allocate memory:
      buffer = new char [length]; //can use std::vector<char> instead if you don't want to allocate and delete buffers.

      // read data as a block:
      is.read (buffer,length);
      is.close();

      std::string Data;
      Data.append(buffer, length);
      delete[] buffer;


      //Data now contains all the data from the file.

      //Use String.Find to find names within the file.
      //OR use Boost::Regex to find names with a specified pattern.

      return 0;
    }

As stated before, you can use getline with a while loop so that you can get each line in the file and search it for the playernames.

what i want is to read the files name and input it to a char(Char's i know)

the .dat is binary, so i need to know the files name to know the players name. help Please?

in other words the first one you asked me :)

WHO, WHAT?
triumphost could you please simplfy for me please.
ive almost got it

#include <iostream>
#include <windows.h>
#include <string>
#include <fstream>
using namespace std;

int main()
{
    char filesource[250];
    ifstream myfile (" " << filesource[250] << " ",ios::in|ios::out|ios::ate|ios::binary);
    cout << "Please type in the source file:" << endl;
    cin.getline(filesource, 250);
    cout << filesource[250] << endl;



    cout << "Hello world!" << endl;
    return 0;
}

but i get an ERROR:invalid operands of types 'const char[2]' and 'char' to binary 'operator<<'

Oh and yes i do know the file location

  1. You cannot do:cout<<CharIdentifier[250]... You have to do cout<<CharIdentifier.

  2. Not sure if you understood me :S but my laptop is about to die so I'll post what I wrote:

    #include <windows.h>
    #include <iostream>
    #include <vector>
    
    using namespace std;
    
    
    vector<string> IterateDirectory(const char* DirectoryLocation)
    {
        WIN32_FIND_DATA WFD;
        HANDLE hFind = INVALID_HANDLE_VALUE;
        vector<string> Result;
        char Directory[1024];
        Directory[0] = '\0';
    
        strcat(Directory, DirectoryLocation);  //Append our directory location to a blank char* buffer.
        strcat(Directory, "\\*"); //To find any file, we append \* to the end of the directory location.
    
        hFind = FindFirstFile(Directory, &WFD);  //Try to find the first file in our directory.
        if (hFind == INVALID_HANDLE_VALUE)
        {
            return Result;  //Return an empty vector since we failed to find any files.
        }
    
        do
        {
            Result.push_back(WFD.cFileName);  //Add each file found to the vector.
        }while(FindNextFile(hFind, &WFD) != 0);  //While there are more files, keep going.
    
        FindClose(hFind);  //Clean up our handles.
        return Result;     //Return a copy of our vector of files. You can probably change this to return a bool and pass-by-reference vector.
    }
    
    int main()
    {
        char* Location = "C:\\Users\\Brandon\\Desktop"; //Directory containing Files.
        vector<string> ListOfFiles = IterateDirectory(Location);  //Returned a copy of our internal vector which holds our list of files.
    
        for (vector<string>::iterator I = ListOfFiles.begin(); I != ListOfFiles.end(); I++) //Loop through our vector and print all file names.
            cout<<*I<<endl;  //Prints every file name.
        return 0;
    }
    

That's as far as I got. It should work. I used Iterators, you can use vector Indicies instead if you're unconfortable with iterators:

for (int I = 0; I < ListOfFiles.size(); I++)  //Same thing as our Iterator version above..
   cout<<ListOfFiles[I]<<endl;

Now every thing above should be much simpler than microsoft's example. I didn't put it into a char* but I put it into a vector of strings. One string = one file. Go through each string and that will be your FileNames. As you said before, each file name is a player name well then I think the above is everything you need.

See this structure for more information on what you can do in the "Do-While" loop. You can get all sorts of information: http://msdn.microsoft.com/en-us/library/windows/desktop/aa365740%28v=vs.85%29.aspx

Not just cFileName. There's also cAlternateFileName, etc.. You can also make it search by file extension by doing an if-else statement:

    do
    {
        if (/*Find Last Of (.dat)*/) 
           Result.push_back(WFD.cFileName);   //Basically only add files with .dat extension to our vector.
    }while(FindNextFile(hFind, &WFD) != 0);

I do not have much time left as my battery is about to die and I'm not at home so no charger with me but you can definitely implement the FindLastOf from std::string inorder to find the extension. Like I stated in my comments, it's always better to use a pass-by-reference vector rather than copy. The code above isn't optimized for the best possible performance but meh, it won't be a big deal since it's just a vector of strings. Unless of course your directory holds thousands of files then pass by reference will be a must.

Goodluck I'll check back when I get access to my comp. Also sorry for the excessive amount of comments in the code. I just wanted to explain it well so you understand. I usually do not comment in my code.

Have you checked this site? http://www.cplusplus.com/forum/unices/9187/
I'm not sure if this would work.

#include <fstream>
#include <iostream>
#include <iterator>
#include <string>
#include <vector>
using namespace std;
int main(){
    string filename;
    cout<<"File name: ";
    cin>>filename;
    ifstream input( filename.c_str(), ios::binary );
    vector<char> buffer((istreambuf_iterator<char>(input)),(istreambuf_iterator<char>()));
    for (int i=0;i<(int)buffer.size();i++){
        if (buffer[i]=='\n') continue;
        else cout<<buffer[i];
    }
    return (0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.