Help passing command line arguments to the “_chdir” function (VS2013 C++)

Hello. It's safe to say I suck at programming and it's really hard for me to wrap my head around some of the concepts, but I really enjoy it. Right now I am trying to right a program to fix damaged image files. The files (.bmp, .gif, .png, and .jpg) have had their extensions and dimensions (except .jpg) removed. I need to process directories and find all the files and fix/rename them with their correct extension and dimensions. Also, I am using VS2013 to write this program.

Right now I am trying to write a function to pass the directory received from the command line arguments (argv[2]) to the _chdir function so I can validate that it is a directory. The problem with this is that _chdir function only takes a pointer to a 1D array as an argument. So, how can I pass the directory command line argument (pointer to a 2D array) to the _chdir function?

Right now I just have the specific directory as the argument for the _chdir function to confirm that it wokrs and print out all of the files in it, but I can' figure out how to get it to work if someone else was to run the program and they want to just run the program without having to edit the code and give the _chdir function the full path before every time they run it.

Here is what my code looks like so far...

Functions.cpp

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
#include "Functions.h"
#include <direct.h>
#include <io.h>
#include <iostream>
#include <fstream>

using namespace std;

void checkArgs ( int argTest )
{
    if (argTest != 2)
    {
        cout << "Usage: " << __argv[0] <<  " dir"
            << endl;
        cout << "   --dir: The directory where the files are"
            " to be processed from" << endl << endl;

        cout << "Exiting now..." << endl << endl;

        exit(1);
    }
}

void validDir(  )
{
    if (_chdir( "C:\\Users\\7031326\\data") == 0)
    {
        cout << "Program changed directories successfully" << endl;
    }
    else
    {
        cout << "Unable to change to the directory" << endl;
    }
}

void listFiles(  )
{
    _finddata_t a_file;
    intptr_t dir_handle; 
    
    dir_handle = _findfirst("*.*", &a_file);

    if (dir_handle == -1)
    {
        return;
    }

    do
    {
        cout << a_file.name << endl;
    } 
    while (_findnext(dir_handle, &a_file) == 0);

    _findclose(dir_handle);
}


Functions.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef __FUNCTIONS__H_
#define __FUNCTIONS__H_

#include <direct.h>
#include <io.h>
#include <iostream>
#include <fstream>

using namespace std;

void checkArgs( int argTest );
void validDir(  );
void listFiles(  );

#endif 


Program.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "Functions.h"
#include <direct.h>
#include <io.h>
#include <iostream>
#include <fstream>
#include <cstring> 


int main( int argc, char **argv )
{
    int argTest;
    argTest = argc;
    checkArgs ( argTest );

    validDir(  );

    listFiles(  );

    return 0;
}
try the simple example below

for example the name of your program is converter on your command prompt type .

converter.exe "C:\\Users\\7031326\\data"

argv[1] will be your directory path.

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
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <iostream>
#include <algorithm>
#include <vector>
#include <iomanip>

using namespace std;

int main(int argc, char **argv)
{
	
	std::cout << argv[1] << std::endl;
	/*int argTest;
	argTest = argc;
	checkArgs(argTest);

	validDir();

	listFiles();*/
	int x;
	cin >> x;

	return 0;
}


http://stackoverflow.com/questions/3024197/what-does-int-argc-char-argv-mean
Last edited on
Topic archived. No new replies allowed.