Make cpp program a function

I have a small cpp program (1) that I want to call from another cpp program (2). I tried making (1) into a header file placing it into (2) so that I can call it. However (2) didn't like it and said I was overloading main. Here is ths code I tried making a header file out of. I just want to call ita s a function ideally from (2). Any help is appreciated. Thank you.

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
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <windows.h>
 
using namespace std; 
typedef vector<WIN32_FIND_DATA> tFoundFilesVector; 
std::wstring LastWriteTime;   
//int getFileList(wstring filespec, tFoundFilesVector &foundFiles) //uni
int getFileList(const char * filespec, tFoundFilesVector &foundFiles) //ansi
{ 
    WIN32_FIND_DATA findData; 
    HANDLE h; 
    int validResult=true; 
 
    int numFoundFiles = 0; 
    //h = FindFirstFile(filespec.c_str(), &findData); //uni 
	h = FindFirstFile((LPCSTR)filespec, &findData); //ansi 
    if (h == INVALID_HANDLE_VALUE) 
        return 0; 
 
    while (validResult) 
    { 
        numFoundFiles++; 
        foundFiles.push_back(findData); 
        validResult = FindNextFile(h, &findData); 
    } 
    return numFoundFiles; 
} 
 
void showFileAge(tFoundFilesVector &fileList) 
{ 
    unsigned _int64 fileTime, curTime, age; 
    tFoundFilesVector::iterator iter; 
    FILETIME ftNow; 
    CoFileTimeNow(&ftNow); 
          curTime = ((_int64) ftNow.dwHighDateTime << 32) + ftNow.dwLowDateTime; 
 
          for (iter=fileList.begin(); iter<fileList.end(); iter++) 
    { 
        fileTime = ((_int64)iter->ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime; 
 
        age = curTime - fileTime;
		if (age <= (_int64)200000000UL)
		{
			wcout << " Delete: '" <<endl;
			wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl; 
			remove(string("c:\\mapper\\").append(string(iter->cFileName)).c_str()); 			
		}
		else
		{
			//wcout << " Quit: '" <<endl;
			//wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl; 
			//return;
		}
    } 
} 
 
int main() 
{ 
    string fileSpec = "*.*"; 
    tFoundFilesVector foundFiles; 
    tFoundFilesVector::iterator iter; 
 
    int foundCount = 0; 
 
    getFileList("c:\\Mapper\\*.txt", foundFiles); 
    getFileList("c:\\Mapper\\*.jpg", foundFiles);
	     foundCount = foundFiles.size(); 
    if (foundCount) 
    { 
        wcout << "Found "<<foundCount<<" matching files.\n"; 
        showFileAge(foundFiles); 
    } 
    system("pause"); 
    return 0; 
}
Last edited on
You should know that this will not check subfolders for their contents. The "Main()" function in this is just an entry point, if you took the two functions and copy+pasted them into your other program, or better yet a header of their own, then you would be all set.
Thank you! I know, its only the one folder. Thats all I need a this point. I tried copying all of the code, dropping it into a header file. did an #include <header> at the top of (2). wacked everything up. Big one is that I was overloading main(). Is there a way to simply call this applet (1) when App (2) needs it?I'm still pretty new to this. I tried pasting this right into where I needed it and that brought up a slew of errors. Any help appreciated. Thank you.
Just paste Lines 1 - 59 into their own header, include that header with double quotes (because I assume you're pulling it from the same directory as your second applications .cpp file) and call the functions when you need them.
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
#include "stdafx.h"
#include <iostream>
#include <stdio.h>
#include <string>
#include <vector>
#include <windows.h>
 
using namespace std; 
typedef vector<WIN32_FIND_DATA> tFoundFilesVector; 
std::wstring LastWriteTime;   
int getFileList(const char * filespec, tFoundFilesVector &foundFiles)
{ 
    WIN32_FIND_DATA findData; 
    HANDLE h; 
    int validResult=true; 
 
    int numFoundFiles = 0; 
    h = FindFirstFile((LPCSTR)filespec, &findData);

But getting errors on

Getting a whole lot of macro redefination errors in winsock2.h and 
 
    if (h == INVALID_HANDLE_VALUE) 
        return 0; 
 
    while (validResult) 
    { 
        numFoundFiles++; 
        foundFiles.push_back(findData); 
        validResult = FindNextFile(h, &findData); 
    } 
    return numFoundFiles; 
} 
 
void showFileAge(tFoundFilesVector &fileList) 
{ 
    unsigned _int64 fileTime, curTime, age; 
    tFoundFilesVector::iterator iter; 
    FILETIME ftNow; 
    CoFileTimeNow(&ftNow); 
          curTime = ((_int64) ftNow.dwHighDateTime << 32) + ftNow.dwLowDateTime; 
 
          for (iter=fileList.begin(); iter<fileList.end(); iter++) 
    { 
        fileTime = ((_int64)iter->ftLastWriteTime.dwHighDateTime << 32) + iter->ftLastWriteTime.dwLowDateTime; 
 
        age = curTime - fileTime;
		if (age <= (_int64)200000000UL)
		{
			wcout << " Delete: '" <<endl;
			wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl; 
			remove(string("c:\\mapper\\").append(string(iter->cFileName)).c_str()); 			
		}
		else
		{
			wcout << " Quit: '" <<endl;
			wcout << "FILE: '" << iter->cFileName << "', AGE: " << (_int64)age/10000000UL << "  seconds" << endl; 
			return;
		}
    } 
} 
 
int colts() 
{ 
    string fileSpec = "*.*"; 
    tFoundFilesVector foundFiles; 
    tFoundFilesVector::iterator iter; 
 
    int foundCount = 0; 
 
    getFileList("c:\\Mapper\\*.txt", foundFiles); 
    getFileList("c:\\Mapper\\*.jpg", foundFiles);
	     foundCount = foundFiles.size(); 
    if (foundCount) 
    { 
        wcout << "Found "<<foundCount<<" matching files.\n"; 
        showFileAge(foundFiles); 
    } 
    return 0; 
}


getting a whole lot of macro redefination on winsock2.h, and ws2pdef.h Thank you.
Last edited on
Got it. Did what you said it works!
Including "functions" and initializing variables etc... in a header file will probably cause problems down the road, unless its not a big project, but still what I do is, I make a header and a cpp file to go hand in hand for example:

"Func1.h" then I make a cpp file: "Func1.cpp"

In the header file you would have your functions and variables and what not as extern like this:

1
2
3
extern int i;
extern void Func1();
// ETC... 


then on your cpp file you would actually initialize them like this;
1
2
3
4
5
6
7
8
9
int i = 1;

void Func1()
{
if (condition)
{
//do stuff
}
}


then you just include "Func1.h" in how ever many cpp files you need to use the functions in, this is the method I prefer, but what ever float your boat.
Topic archived. No new replies allowed.