HI, I've been looking for a long time now, for some simple ways to get the full path of a file in the same directory of my console and the console itself. The main purpose of my small project is to just locate a file and move it into another directory. I've got the jist of how to do it I just can't find the proper code.

The part where I'm getting most stuck at, is for a check system to see if the files exist before and after they've been moved or not. This is what I got so far..

#include <windows.h>
#include <fstream>
#include <iostream>
#include <string>
using namespace std;


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 FileExists(const char* "Splash.jpg")
{
     FILE* fp = NULL;
     fp = fopen("Splash.jpg", "rb");
     if (fp != NULL)
     {
            fclose(fp);
            return true;
     }
     return false;
}
*/

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];
    string fullPath;
    fullPath = Path::GetFullPath(cSplash);
    
    
    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;
    
    //ifstream cSplash;
    //ifstream tSplash;
    if (mInput == "1")
    {
        if (checkFirst() == 1)
        {
            cout << "works.." << endl << endl; 
            cout << "GetFullPath('{0}') returns '{1}'", cSplash, fullPath << endl;
            system("pause>nul");
            goto Kill;
            /*if (tSplash)
            {
                cout << "works so far";
                system("pause>nul");         
            }*/
        }
        else
        {
            cout << "\nError...";
            system("pause>nul");
            goto Kill;
        }
    }
    else
    {
        cout << "That was not an option, please try again!" << endl;
        system("pause>nul");
    }
    system("pause>nul");
    system("cls");
    goto menu;
    
    Kill:
         system("cls");
         return 0;

}

I've already written up what I wanted in C#. I'm pretty much trying to translate it into C++, which I'm still new at.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Reflection;

namespace Installer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Title = "Beta Installer 1.0 by Reece Dizon";
            string a = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
            string ijji = @"C:\ijji\ENGLISH\Gunz";
            string cSplash = a + @"\Splash.jpg";
            string tSplash = ijji + @"\Splash.jpg";

            menu:
            Console.Clear();
            Console.WriteLine("                  --------------------------------------------");
            Console.WriteLine("                                Installer 1.0");
            Console.WriteLine("                  --------------------------------------------\n\n");

            Console.WriteLine("Choose a number and hit enter to proceed... \n\n");
            Console.WriteLine("0    -    Exit");
            Console.WriteLine("1    -    Install \n\n");
            string input = Console.ReadLine();
            if (input == "1")
            {
                Console.Clear();
                if (File.Exists(cSplash))
                {
                    if (File.Exists(tSplash))
                    {
                        File.Delete(tSplash);
                        File.Move(cSplash, tSplash);
                        Console.ForegroundColor = ConsoleColor.Green;
                        Console.WriteLine("Installation Complete! Proceed to exit isntaller...");
                        Console.ReadLine();
                        goto Kill;
                    }
                    else
                    {
                        Console.WriteLine("An error has occurred. Terminating installer...");
                        Console.ReadLine();
                        goto Kill;
                    }
                }
                else
                {
                    Console.WriteLine("You Splash.jpg does not exist, please try agian.");
                    goto menu;
                }
            }
            else if (input == "0")
            {
                goto Kill;
            }
            else
            {
                Console.Clear();
                Console.WriteLine("That was not an option, please try again!");
                Console.ReadLine();
                goto menu;
            }

            Kill:
            Console.Clear();
            Environment.Exit(0);


        }
    }
}

First, I must ask:
1) Are you really interested in keeping the goto statements instead of just calling a function or letting the program terminate naturally?
2) Do you know you can just make your C# a class library and call it from C++?
3) ...or do you intend on converting this from managed code to native code?
4) Have you looked into actually creating an installer (or is this something special)?

Some other issues:
Do you really want to "try again" if the source file does not exist?
It seems like the program should exit at that point.

Also, if it exists, do you really want to "move" it to its new location or just copy it?

To test to see if the file is in the current directory, you can do something like this (in managed C++):

//using namespace System::IO;
      String^ strCurrDir = Directory::GetCurrentDirectory();
      String^ strSplashName = "Splash.jpg";
      String^ strSrceFile = Path::Combine(strCurrDir, strSplashName);
      // now the FULL path name is in strSrceFile.
      if(!File::Exists(strSrceFile))
      {
         //strError = (strSrceFile + " does not exist.");
         //return false;
      }

you should check out the boost::filesystem library, it has lots of handy file-related functions.

Hi, and thanks for replying.

@thines, I don't care too much for the goto statements, but I thought I'd give it a try for this time ;). And I actually did not know I can turn it into a .dll and use in C++. I've made some actual installers in C# windows forms applications, I'm actually not too fond of consoles. I'm in the progress of trying to learn C++ as I said, I'm pretty much new at it. I've been using C# for quite a while and C++ is a little more complicated than C# than I thought, but I'll get used to it in the long run.

As for the try again portion, I didn't get to work out all the check and error parts of it, because I cannot get if (file.exist(file)) { if file.exist(file2)) {} } in C++. And I'd much rather move the file than copying. I'll check out the code you pm'd me and the one above, thanks for all the feedback. Appreciate it. Also, I just need to get these few kinks resolved. I hope once I can understand this a little more I can work with the rest of my project, which includes this of course :D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.