Listing Files

Hi all,

I am doing a program to list all the files in a folder. I wrote all the code, but when I tried to compile, I got several errors.

With a quick search on the internet, I changed the parameter “Common Language Runtime Support” to /clr. It solved all the erros.

Now it´s time to run the generated .exe file in a friend´s computer. But I got this error: “… MSVCR100D.dll is missing…”.

Aparently this problem can be solved by changing the setting Runtime Libary to “Multi-threaded (/MT)”. Ok, but now I got the error: “Command line error D8016: '/clr' and '/MT' command-line options are incompatible”.

Is this a way to run my problem on any computer?

Thanks in Advance,

The Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
// Nomes_Duplicados.cpp : main project file.

#include "stdafx.h"
#include "math.h"
#include "stdio.h"
#include "windows.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
char**lista;
char**lista2;
int lista_limite;
int lista_cont;

void incluir_nome(char* in)
{
	int novo_limite,i;

	lista[lista_cont] = new char[strlen(in)+1];
	strcpy(lista[lista_cont],in);
	if(lista_cont == lista_limite)
	{
		novo_limite = lista_limite+100;
		char** lista_aux = new char*[lista_limite+1];
		for (i=0;i<=lista_limite;i++) lista_aux[i] = lista[i];
		delete lista;
		lista = new char*[novo_limite+1];
		for (i=0;i<=lista_limite;i++) lista[i] = lista_aux[i];
		delete lista_aux;
		lista_limite = novo_limite;
	}
	lista_cont++;
	return;
}
void erro(int i)
{
	cout << "\n\n ERRO! \n";
	if(i==1) cout << "Nome de um arquivo muito grande!\n";
	if(i==2) cout << "Nenhuma pasta foi ifnromada ao programa\n";
	while(true);
	return;
}
int tamanho(WCHAR in[])
{
	int i=0;
	while(true)
	{
		if(in[i]==0) break;
		i++;
		if(i==2000) erro(1);
	}
	return (i+1);
}
void copiar(char* aqui, WCHAR esse[])
{
	int i=0;
	while(true)
	{
		if(esse[i]==0) break;
		aqui[i]=esse[i];
		i++;
	}
	aqui[i] = '\0';
	return;
}
void incluir(char caminho[])
{
	int i,mem_tam,tam_caminho;
	char asterisco[] = "\\*";
	tam_caminho = strlen(caminho);
	WIN32_FIND_DATA fd;
	HANDLE hFind;
	char* nome;
	char* nome_completo;
	wchar_t* wtext = new wchar_t[tam_caminho];
	mbstowcs(wtext, caminho, tam_caminho+1);//Plus null
	LPWSTR a = wtext;
	hFind = FindFirstFile(a, &fd);
    FindNextFile(hFind,&fd);
	while(FindNextFile(hFind,&fd))
	{
		
		mem_tam = tamanho(fd.cFileName);
		nome = new char[mem_tam+3];
		nome_completo = new char[mem_tam+tam_caminho+3];
		copiar(nome,fd.cFileName);
		strncpy(nome_completo,caminho,tam_caminho-1);
		nome_completo[tam_caminho-1] = '\0';
		strcat(nome_completo,nome);
		//Verifica se é uma pasta
		if(fd.dwFileAttributes == 16) 
		{
			strcat(nome_completo,&asterisco[0]);
			incluir(nome_completo);
		}
		else           		          incluir_nome(nome_completo);
		delete nome;
		delete nome_completo;
	}
	//delete wtext;
	return;
}
int main(int argc,char *argv[])
{
    //Ler o caminho de entrada (FAZER !!!)
	int i,j,k,l;
	char* caminho;
	char asterisco[] = "*";
	if(argc==-2) erro(2);
	//cout << argv[0] <<endl;
	
	k=0;
	while(argv[0][k]!='\0') k++;
	j=k;
	while(argv[0][j]!='\\') j--;
	caminho = new char[j+2];
	for(l=0;l<=j;l++) caminho[l] = argv[0][l];
	caminho[j+1]='\0';
	strcat(caminho,&asterisco[0]);
	//cout << caminho <<endl;

	
	//Inicializar a Lista
	lista_limite = 2;
	lista_cont = 0;
	lista = new char*[lista_limite+1];

	incluir(caminho);
	lista2 = new char*[lista_cont];
	for(i=0;i<=lista_cont-1;i++)
	{
		k=0;
		while(lista[i][k]!='\0') k++;
		j=k;
		while(lista[i][j]!='\\') j--;
		k--;
		j++;
		lista2[i] = new char[k-j+2];
		for(l=j;l<=k;l++) lista2[i][l-j] = lista[i][l];
		lista2[i][k-j+1]='\0';
	}
	//heapsort(lista2,lista,lista_cont);

	for(i=0;i<=lista_cont-1;i++) cout << lista[i] << endl;
	for(i=0;i<=lista_cont-1;i++) cout << lista2[i] << endl;
	delete lista;
	delete lista2;
	while(getchar()=='\0');	
}
Last edited on
Please use the code format tags to format code.

I am doing a program to list all the files in a folder. I wrote all the code, but when I tried to compile, I got several errors.
You should post the error text that the compiler generates. Believe it or not, the compiler is telling you what's wrong.
Thanks for the reply ! And sorry about the code format!

The error message is quite simple, line 13 of the question:
“Command line error D8016: '/clr' and '/MT' command-line options are incompatible”.

Solving the error looks pretty simple: Changing the option '/MT' to another option.

But with this settings the program would not work on any other computer (I am also not sure about that, I tried only once). And the question remains:

Is this a way to run my problem on any computer?

Last edited on
'/clr' means you're trying to build your code as C++/CLI -- is that what you want?

Your code looks like native C++ to me so I think you should get rid of the /clr and keep the /MT

In case you don't already know, /MT means link to the static version of the CRT (C Run-Time), /MD to the DLL version. Both are multi-threaded. (In the "olden days" there was also /ML, which meant the single-threaded version of the static library, but that option has been dropped.)

Also, if you're working with the WinAPI you should be up to working with the standard containers, so I'd expect to see std::string and std::vector<> being used rather than all the dangerous pointer work.

Andy

PS Nice colors -- but your code could still do with being formatted (esp the indenting.)

(Note you can go back an edit prior posts, which would have saved screen real estate for this thread!)
Last edited on
PS Are you also getting confused about WCHAR (i.e. wchar_t) and char?

void copiar(char* aqui, WCHAR esse[])

worries me.

What you should prob. be doing it sticking to the ANSI entry points by using WIN32_FIND_DATAA, FindNextFileA, etc (i.e. add -A suffix to most of the calls; FindClose is an exception, as it doesn't take any C string params.) And then get rid of all WCHARs and the like.

And get rid of your copiar() function!!

Andy

PS Further reading:

WinAPI: Being Unicode Friendly
http://www.cplusplus.com/articles/2w6AC542/
Last edited on
PPS I am guessing that you're using Visual Studio as your IDE, given the WCHAR/char issues (your project is configured to build with "Use Unicode Character Set")

If so, you should crank up the compiler warning level to W4 and add _CRT_SECURE_NO_WARNINGS to the preprocessor definitions so you can mute all the C4996 warnings (benign if this is a tool for persona use.)

warning C4996: 'strcat': This function or variable may be unsafe. Consider using strcat_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

It reveals a what is hopefully a problem on line 41 of your code (I assume this isn't deliberate?)

36
37
38
39
40
41
42
43
void erro(int i)
{
    cout << "\n\n ERRO! \n";
    if(i==1) cout << "Nome de um arquivo muito grande!\n";
    if(i==2) cout << "Nenhuma pasta foi ifnromada ao programa\n";
    while(true);
    return;
}


Andy
Last edited on
Andy, many thanks for all your tips! As you can see this is a beginer´s code, very beginner to be more precise : ) ! I will implement all the corrections and improvements as soon as possible.

Yes, I am using Microsoft Visual Studio like you said.

I tried to get rid of the /clr. But when I change the setting to “No Common Language Runtime Support” I got lots of erros, for example:
“…error C2871: 'System' : a namespace with this name does not exist”.

To a layman like me, the funny thing is that when I press “F5” the program gracefully displays the list (works ok with no error). But when I copy the .exe file to another computer, I get this unpleasant message: “The program can´t start because MSVCR100D.dll is missing from your computer. Try reinstalling the program to fix this problem.”

Many thanks once again,
I don't see any reference to system in the code you posted. If you can't find any other configuration issues it might make sense to create a clean Win32 console app project and copy your code across to it.

MSVCR100D.dll is the debug (D = debug) version of the DLL. Building the release version should fix the problem...

But I did suggest using /MT which would eliminate the need for the MSVCRT DLL totally (it statically links to the CRT.)

Andy
Andy,

I created a new project as you said. It´s working now! First I created the project as a "CLR Console Application", I believe it was the problem since the very beginning.

I also implemented many of the changes you suggested.

My problem now is in these conversions (caminho is a std::string):

1
2
wstring stemp = wstring(caminho.begin(), caminho.end());
LPCWSTR a = stemp.c_str();


Apparently the variable "stemp" can´t handle some Portuguese characters as "çâáéí" for example. Can anyone help me with this?

Many thanks once again.

Here is the new code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// Nomes_Duplicados_V1.1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "math.h"
#include "stdio.h"
#include "windows.h"
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;
vector<string> lista;
vector<string> lista2;

vector<int> heapsort(vector<string>* in)
{
	bool order = 1; //TRUE when the 0 is the smallest / FALSE when 0 is the bigger
	vector<int> ret;
	vector<string> vetor = *in;
	string aux_string;
	unsigned int i,j,p,l,r,maior,aux_int,ultimo;
	bool cont;
	for(i=0;i<=vetor.size()-1;i++) ret.push_back(i);
	//Turn it in to a HEAP
	for(i=1;i<=vetor.size()-1;i++)
	{
		j=i;
		while(true)
		{
			if(j==0) break;
			cont = 0;
			p = ((j+1)/2)-1;
			if( ( (order)  &&  (vetor[j].compare(vetor[p]) > 0)   )||
				( (!order) &&  (vetor[j].compare(vetor[p]) < 0)   )
			  )
			{
				cont = 1;
				aux_string = vetor [j];
				vetor[j] = vetor[p];
				vetor[p] = aux_string;
				aux_int = ret [j];
				ret[j] = ret[p];
				ret[p] = aux_int;
				j = p;
			}
			if(!cont) break;
		}
	}
	//Organize the heap
	ultimo = vetor.size()-1;
	while(true)
	{
		if(ultimo == 0) break;
		aux_string = vetor[ultimo];
		vetor[ultimo] = vetor[0];
		vetor[0] = aux_string;
		aux_int = ret[ultimo];
		ret[ultimo] = ret[0];
		ret[0] = aux_int;
		ultimo--;
		//Top value goes down
		j = 0;
		while(true)
		{
			cont = 0;
			l = 2*(j+1)-1;
			r = l+1;
			if((l>ultimo)&&(r>ultimo)) break;
			maior = j;
			if((order) &&(l<=ultimo)&&(vetor[l].compare(vetor[maior]) > 0)) maior = l;
			if((order) &&(r<=ultimo)&&(vetor[r].compare(vetor[maior]) > 0)) maior = r;
			if((!order)&&(l<=ultimo)&&(vetor[l].compare(vetor[maior]) < 0)) maior = l;
			if((!order)&&(r<=ultimo)&&(vetor[r].compare(vetor[maior]) < 0)) maior = r;
			if(maior!=j)
			{
				cont=1;
				aux_string = vetor[maior];
				vetor[maior] = vetor[j];
				vetor[j] = aux_string;
				aux_int = ret[maior];
				ret[maior] = ret[j];
				ret[j] = aux_int;
				j = maior;
			}
			if(!cont) break;
		}
	}
	//Return
	*in = vetor;
	return ret;
}
void incluir(string caminho)
{
	string nome,aux;
	WIN32_FIND_DATA fd;
	HANDLE hFind;
	wstring stemp = wstring(caminho.begin(), caminho.end());
	LPCWSTR a = stemp.c_str();
	hFind = FindFirstFile(a, &fd);
    FindNextFile(hFind,&fd);
	while(FindNextFile(hFind,&fd))
	{
		wstring nome_w( fd.cFileName );
		string nome( nome_w.begin(), nome_w.end() );
		//Verifica se é uma pasta
		if(fd.dwFileAttributes == 16) 
		{
			aux = caminho;
			aux.insert(aux.size()-1,nome);
			aux.insert(aux.size()-1,"\\");
			incluir(aux);
		}
		else 
		{
			lista.push_back(nome);
			aux = caminho;
			aux = aux.substr(0,aux.size()-1);
			aux += nome;
			lista2.push_back(aux);
		}
	}
	return;
}
int main(int argc,char *argv[])
{
	unsigned int i;
	try
	{
		if(argc == -2) throw 1;
		string caminho = argv[0];
		size_t last = caminho.find_last_of('\\');	
		caminho = caminho.substr(0,last+1);
		caminho.append("*");
		cout<<"Lendo os Arquivos." << endl << endl;
		incluir(caminho);
		if(lista.empty()) throw 2; 
		cout<< lista.size() << " Arquivo(s) Encontrado(s)." << endl << endl;
		cout << "Organizando Arquivos" << endl << endl;
		vector<int> org = heapsort(&lista);
		vector<string> aux;
		for(i=0;i<=lista2.size()-1;i++) aux.push_back(lista2[org[i]]);
		lista2 = aux;
		cout <<"Listando Arquivos" << endl << endl;
		caminho = caminho.substr(0,caminho.size()-2);
		caminho += "\\Lista.csv";
		ofstream MyFile (caminho,ios_base::out);
		for(i=0;i<=lista.size()-1;i++)  MyFile << lista[i] << ";" << lista2[i] << endl; 
	}
	catch(int e)
	{
		if(e==1) cout << "Erro " << e << ":Nenhum caminho especificado." << endl << endl;
		if(e==2) cout << "Erro " << e << ":Nenhum arquivo encontrado." << endl << endl;
	}
	cout << "Pressione ENTER para concluir." << endl;
	while(getchar()=='\0');
	return 0;
}
Last edited on
Topic archived. No new replies allowed.