Copying or Moving Files

Hello. So to start off, I've made a simple program in C# to move a given file into a specified directory, deleting the file if it already exists and moving the given file into its place. I'm completely new at C++ and am trying to create the exact same console program with C++. I am using Dev-Cpp as recommended by a friend and I am now aware I should have kept visual studio's C++ instead. However I'm still up to using bloodshed's cpp compiler.

Right now I'm stuck on moving/copying my file. I've researched quite a bit and tried to gather different code to perform the task. Below is where my compiler says there are too many arguments in the function but I retrieved this code from a website that I can't remember right now and I am still confused.

static void copyFile(const std::string& fileNameFrom, const std::string& fileNameTo)

And below is the rest of my code. I am still new so skip unnecessary / rude criticism.

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
#include <stdio.h>
#include <windows.h>
#include <fstream>
#include <iostream>
#include <string>
#include <cassert>
#include <vector>
using namespace std;
     
bool fileExists(const std::string& fileName)
{
     std::fstream file;
     file.open(fileName.c_str(), std::ios::in);
     if (file.is_open() == true)
     {
         file.close();
         return true;
     }
     file.close();
     return false;
}

static void copyFile(const std::string& fileNameFrom, const std::string& fileNameTo)
{
     assert(fileExists(fileNameFrom));
     std::ifstream in (fileNameFrom.c_str());
     std::ofstream out (fileNameTo.c_str());
     out << in.rdbuf();
     out.close();
     in.close();
}

bool checkFirst()
{
    char* cSplash = "Splash.jpg";
    //char* tSplash = "C:\ijji\ENGLISH\Gunz\Splash.jpg";
    
    DWORD attr = GetFileAttributes(cSplash);
    if (attr = INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
       return 1;
    else
       return 0;
}

bool checkSecond()
{
     char* tSplash = "C:\\ijji\\ENGLISH\\Gunz\\Splash.jpg";
     DWORD attr = GetFileAttributes(tSplash);
     if (attr = INVALID_FILE_ATTRIBUTES || (attr & FILE_ATTRIBUTE_DIRECTORY))
          return 1;
     else 
          return 0;
}


int main(int argc, char** argv)
{
    SetConsoleTitle("Beta Installer 1.0 by Reece Dizon");
    
    string cSplash = "Splash.jpg";
    string tSplash = "C:\\ijji\\ENGLISH\\Gunz\\Splash.jpg";
    
    string path = argv[0];
    
    menu:
    HANDLE hConsole;
    hConsole = GetStdHandle (STD_OUTPUT_HANDLE);
    int wWhite = 15;
    SetConsoleTextAttribute(hConsole, wWhite);
    cout << "                 --------------------------------------------" << endl;
    cout << "                               Installer 1.0" << endl;
    cout << "                 --------------------------------------------\n" << endl;
    cout << "0    -    Exit" << endl;
    cout << "1    -    Install\n" << endl;
    
    string mInput;
    cout << "Choose a numer and hit enter to proceed." << endl;
    cin >> mInput;
    if (mInput == "1")
    {
        if (checkFirst() == 1) //checkFirst() == 1
        {
            if (checkSecond() == 1 )
            {
                system("cls");
                if (remove(tSplash.c_str()) != 0)
                {
	             //
                }
                else
                {
                    bool flag = false;
                    fstream fin;
                    fin.open(tSplash.c_str()); //tSplash,ios::n
                    if (fin.is_open())
                    {
                        cout << "File was unable to be removed" << endl;
                        flag = true;
                        fin.close();
                        system("pause>nul");
                        goto Kill;
                    }
                    else
                    {
                    cout << "File removed.\n" << endl;
                    cout << "Continue with installation...." << endl;
                    fin.close();
                    system("pause>nul");
                    goto nMove; 
                    }
                }
            }
        }
        else
        {
            cout << "File not found...\n";
            system("pause>nul");
            goto Kill;
        }
    }
    else if (mInput == "0")
    {
         goto Kill;
    }
    else
    {
        cout << "That was not an option, please try again!" << endl;
        system("pause>nul");
    }
    system("cls");
    goto menu;
    
    nMove:
          system("cls");
          int result;
          copyFile(cSplash, tSplash, TRUE);
          cout << "Installation successful!" << endl;
          system("pause>nul");
          goto Kill;
    
    Kill:
         system("cls");
         return 0;

}


All suggestions much appreciated.
Last edited on
I am not sure, but the function copyFile has 2 arguments, however, you gave it 3. try copyFile(cSplash, tSplash) at line 136.
omg dude, it freaking works hahah. I knew I made stupid mistake. Thanks for that mate, probably would have never figured it out on my own. On the error board my compiler keeps stopping at my line 23, so I kept thinking something was wrong with that line of code. Bah I feel dumb, thanks again mate, greatly appreciate it!
closed account (zb0S216C)
2 things:

1) Don't use Dev-C++
2) Don't use label: ...goto

Wazzak
I haven't used my visual studios cpp in like 2-3 years and I wwanted to start again a week ago or so. My friend told me to use Dev-C++ because it is more user-friendly and will be better for a complete beginner such as myself. I already uninstalled msvs and installed dev-cpp so I'm too lazy to uninstall and reinstall all over again =[

and what's wrong with the goto labels
Last edited on
Dev-C++ hasn't been updated in years, it has lots of unresolved bugs and I see tons of beginners pop in the forum with issues that is attributed to their compilers. Do the switch, it'll make things much better.

goto is a command that is left over from before the days of structured programming. When you start to write code of more than 200 lines, the goto will get VERY complicated, debugging will be more difficult since you don't know how you got to that point in the code and no one will help you because no one wants to read a spaghetti mess riddled with gotos.
I see. Well so do you recommend I switch over to visual studios C++ 2010? I've had 2008 on my computer ever since and for some diddly reason it wouldn't let me create a new project. God knows why but I guess 2010 will be best? I know some other cpp compilers but I've always used msvs.

Edit: Well I switched over to visual studios C++ 2010, so different to 2008 and the layout design looks exactly like C# 2010 hahah. But yeah, I copy and pasted my code and i got some errors to start out with, fixed and moved some things around and works like a charm, thx for info and help and suggestion everyone.
Last edited on
Topic archived. No new replies allowed.