finding specific files in a file in c++ - c++

I have a file named mama. This file contains 3 files named child1 child2 and child3. child1 contains 5 txts,child2 contains 7 txts and child3 contains 4 txts. The path of mama is C:\Users\John\Desktop\mama .Sorry for my way of writing but i am trying to explain exactly my case. My goal is to find the paths of all these txts(16 in number) so i can do things with them. So i think a function that finds this paths and put them in a linked list
struct paths
{
string pathName;
paths *next;
};
would be exactly what i need to use them one by one. I found some examples about FindFirstFile() and FindNextFile(), i also tried to run some code so i could understand with some testing how it works but erros keep apeared forbiding the oportunity for testing and understanding. By the way i use visual Studio 2008(it's the one they said we should use so i guess i can't change it). If someone can help me understand or got any link that would have some good and somehow easy to understand examples i would be really thankfull.
Edit:
For example with this code
#include <windows.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
WIN32_FIND_DATA fData;
void * handle = FindFirstFile( "C:/Users/John/Desktop/*", &fData );//<~~~error
cout << fData.cFileName;
system("pause");
}
There is 1 error: Error 1 error C2664: 'FindFirstFileW' : cannot convert parameter 1 from 'const char [24]' to 'LPCWSTR'

first, you should declare a WIN32_FIND_DATA structure
then find the first file in a directory ( ie C:/Users/John/Desktop/* )
paths * head;
WIN32_FIND_DATA fData;
void * handle = FindFirstFile( "C:/Users/John/Desktop/*", &fData );
check to see if it's the file you want using:
fData.cFileName // the file name is stored here ( ie C:/Users/John/Desktop/child2.txt )
you can do this in a loop:
while( !CheckFileNameHere( fData.cFileName, head->pathname ) ) FindNextFile( handle, &fData );
increment the list:
head = head->next;
or finish:
CloseHandle( handle );
How you obtain the paths is up to you. If you have stored them in a file, use and of the
functions from stdio.h, iostream, or windows.h. If you write this as a function, you can reuse it for any file name you have.
If you are getting runtime errors using these methods, you should post the exact errors so that we can figure out why they aren't working. Same goes for compilation errors.

Related

The expression must have a class type

I am currently programming a patching application for my game. As i am used to program with Java its hard for me to get along with C++, the patcher has to be wridden in C++ unfortunately, in Java i could do this in 5 minutes but a new language. . . not so much.
This is my current Code to create the folders i need:
#include <windows.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
//Set the Strings the Patcher needs.
string DNGDirectory = "C:\\dnGames";
const char* DDDirectory = "C:\\dnGames\\DuelistsDance";
//Create directories if they don't exist yet.
if (CreateDirectory(DNGDirectory.c_str(), NULL) || ERROR_ALREADY_EXISTS == GetLastError())
{
if (CreateDirectory(DDDirectory.c_str(), NULL) || ERROR_ALREADY_EXISTS == GetLastError())
{
cout << "Directories successfully created." << std::endl;
}
}
return 0;
}
One time i use string for the variable, because this was in the example code i picked out from Google (Create a directory if it doesn't exist), but i get the error "Das Argument vom Typ ""const char "" ist mit dem Parameter vom Typ ""LPCWSTR"" inkompatibel." (Should be the argument of type ""const char" is incompatible with the parameter of type ""LPCWSTR"" in english) I tried to fix it by using "const char*" as type, but this gets me the error "Der Ausdruck muss einen Klassentyp aufweisen." (They expression must have a class type). Does anyone know how to fix this? I am using Visual Studio 2019 for this.
Since C++17 (and to a lesser extent 14) we can use std::filesystem (std::experimental::filesystem in C++14) to manipulate files and create directories.
For example in your case:
...
std::filesystem::path DDDirectory("C:\\dnGames\\DuelistsDance");
try {
std::filesystem::create_directories(DDDirectory); // Creates all the directories needed, like mkdir -p on linux
// Success here
} catch(std::filesystem::filesystem_error& e) {
// Handle errors here
}
This will make handling of your errors cleaner and your code cross-platform (although you will have to change the path, but std::filesystem::path turns / into \\ on windows anyway). It also makes your code easier to read and as you can see, much much shorter.
In case someone reads this and has the same problem, this is the final fix:
change project settings to not use unicode anymore
change variables to const char* DNGDirectory = "C:\\dnGames";
use CreateDirectory() and remove .c_str() after the variable name
Thanks for the comments and the alternate solution with std::filesystem!

Get list of all types/classes from Mono Assembly.dll in C++

I am trying to get a list of all the types and/or classes of a Mono Assembly file, but I could not find out how to do that. My goal is to load an assembly.dll in C++ and read its types and classes so I can use mono_class_get_fields etc.
This is what I tried with a known class name in the .dll, but class1 was NULL. What is going wrong here?
ExampleClass is exending System.Object, which is in mscorlib.dll. Is there some kind of linking I need to do to get a proper MonoClass * inside my C++ code?
This is what I tried, but mono_class_from_name returns NULL:
#include <windows.h>
#include <mono/metadata/assembly.h>
#include <mono/metadata/mono-config.h>
#include <mono/metadata/appdomain.h>
#include <mono/jit/jit.h>
int main()
{
mono_set_dirs("C:\\Program Files\\Mono\\lib", "C:\\Program Files\\Mono\\etc");
MonoDomain *domain;
domain = mono_jit_init("test");
MonoAssembly *assembly = mono_domain_assembly_open(domain, "C:\\Users\\Ik\\Documents\\Visual Studio 2015\\Projects\\MonoExtractor\\x64\\Debug\\Assembly-CSharp.dll");
MonoImage *image = mono_assembly_get_image(assembly);
// testing to see if I can get a known class
MonoClass *class1 = mono_class_from_name(image, "", "ExampleClass");
if (!class1) {
printf("Could not find class!\n");
}
system("pause");
return 0;
}
I had to put the other related dlls in the same directory too in order for it too work.

Calling zlib (C code) from a C++ file

I have a .cpp file that contains the following function to decompress a file via zlib:
#include <zlib.h>
#include <fstream>
bool gzip_uncompress(std::string &compressed_file_path,std::string &uncompressed_file_path)
{
char outbuffer[1024*16];
gzFile infile = (gzFile)gzopen(compressed_file_path, "rb");
FILE *outfile = fopen(uncompressed_file_path, "wb");
gzrewind(infile);
while(!gzeof(infile))
{
int len = gzread(infile, outbuffer, sizeof(outbuffer));
fwrite(outbuffer, 1, len, outfile);
}
fclose(outfile);
gzclose(infile);
return true;
}
This looks like it should run to me, but I'm getting compile time errors stating:
No matching function call to 'gzopen'
and
No matching function call to 'fopen'
The only thing I can thing of is that I am calling these in a C++ file, and the zlib is a C library. I'm not sure about the fopen error though.
Does anyone see how I can call these functions and get around the compile error?
I hava also tried:
extern "C" {
#include <zlib.h>
}
but still no go. Am I barking up the wrong tree? Should I move this function into a C file? But then I'd presumable have the same issue.
Use .c_str() when passing a std::string to functions that expect a char *.
there are two things here...
Compile time inclusion of C header in C++ file
Linking C compiled lib (zlib) with C++ program
for the first issue, your compiler is not able to see declaration of gzopen, there might be many reasons for that..
please check the declaration of functions gzopen in zlib.h
Make sure you need to include path to zlib.h in the arguments to g++
ex: -I/usr/local/include/zlib.h
Please follow the link below for developer documentation
http://www.zlib.net/manual.html
Please follow the help
For 2. Linking your C++ program with zlib, please use
extern "C" {
#include <zlib.h>
}

Difference between two header orderings in C++ seems to generate error

I have a class called File that is defined (along with other classes) in the header "dmanager1.h". In the "dmanager1.cpp" file (implementation for the dmanager1.h file), when I list the headers in one order I get an error when trying to compile along with my main.cpp (main.cpp is empty except for the header call and an empty "int main()"...basically I'm just testing the class .h and .cpp files)... If I switch the headers around in the dmanager1.cpp file I get no errors. I don't understand what is happening. The error I'm getting is:
error: 'File' does not name a type
I get said error when I have my header's ordered in my "dmanager1.cpp" as follows:
#include "dmanager1.h"
#include <iostream>
#include <cstring>
If I switch the header's around to:
#include <iostream>
#include <cstring>
#include "dmanager1.h"
...I don't get the compilation error. Is the first order getting parsed funny? Any thoughts would be greatly appreciated.
EDIT: Added part of the header in question...
#ifndef _dmanager1_h
#define _dmanager1_h
//--------------------
// Forward References
//--------------------
// Node_L, Node_T, and Sector are defined in File: dmanager1a.h
class Node_L;
class Node_T;
class Sector;
class File
{
public:
// Default Constructor
//File();
// Constructor: Allowing "name", "size", and/or "permissions" to be set
// Permissions set to default of 0 == read and write
File(const char * & name, float size = 0, int permissions = 0) : timestamp(11223333) {};
// Default Destructor
~File();
//returns an int corresponding to the date modified (mmddyy)
int get_date_mod(void) const {return timestamp;}
// Return's current level of permission on the File: 0 = read/write, 1 = read only
int get_permission(void) const {return permission;}
// Set's Permission to "level": 0 = read/write, 1 = read only
int set_permission(int level);
private:
// Data members
char * name;
float size_OA;
//function used to update "date modified"
void update_timestamp(void);
// Current permission level of the file: 0 = read/write, 1 = read only
int permission;
//value modified by update_timestamp() and the value returned by get_date_mod(). Date file last edited.
int timestamp;
};
Most likely your dmanager1.h header needs something that iostream or cstring define.
As a result, it doesn't get parsed correctly, and the compiler doesn't understand the declaration of your File class.
If you post your dmanager1.h file, you'll be able to get a more detailed answer.
Make sure that each of your headers is completely self-sufficient. It needs to #include headers for everything that it uses and not assume that they will be included by something else. Every header should work even if it is the only header that a .c file includes.
I'm betting that your dmanager1.h header is using something from the standard library and you aren't including the header that it needs. Swapping the header appears to fix the problem, but it's only working by coincidence.
One diagnostic test you can do is to create a .c file that contains nothing but the line #include "dmanager1.h". Try to compile it. If the compiler throws an error, it should provide hints as to which additional headers need to be included.
Update: I can compile using the initial portion of the header that you posted using g++ -Wall and I get no errors or warnings at all. Please post a sample that reproduces the problem.

GCC compile error : declaration of ‘strlen’ must be available

My problem is that when I want to make a downloaded library I get some weird compile errors from GCC and the code that the compiler demands to correct seems just to be right.
The errors are all like this:
Catalogue.h:96: error: there are no
arguments to ‘strlen’ that depend on a
template parameter, so a declaration
of ‘strlen’ must be available
Here is the code around line 96:
GaCatalogueEntry(const char* name, T* data)
{
if( name )
{
_nameLength = (int)strlen( name ); // LINE 96
// copy name
_name = new char[ _nameLength + 1 ];
strcpy( _name, name ); // LINE 100: similar error
_data = data;
return;
}
_name = NULL;
_nameLength = 0;
_data = NULL;
}
What can I do to fix these compile errors?
You probably just need to include the header that contains the strcpy and strlen library functions.
#include <string.h>
or (preferably for C++)
#include <cstring>
In C++ the strlen() function is part of the string library, and it almost looks like the header file was not included.
Is it included anywhere?
include <string.h>
If not, try adding it and see if that fixes the problem.
The code is buggy. You are probably missing an #include <string.h>.
If you don't want to change the code, add -fpermissive to the compiler options. (See the GCC documentation.)
a declaration of ‘strlen’ must be available
Include string.h or <cstring> (C++) for the declaration of strlen().