0

I'm using Visual Studio 2019. The file path must be taken as an User input. I'm new to C++ and will appreciate any help i can get.

iWiiCK
  • 361
  • 5
  • 13
  • you can ask the user via `cout` to enter the full path, then store it in a string via `cin` and use `substr`, `find_last_of` to get the filename.extension. – asmmo Jul 29 '20 at 04:38

1 Answers1

0
#include <iostream>
#include <string>

int main()
{
    std::string filename;
    std::cout << "Enter file path: "; 
    std::cin >> filename; // "Example: C:/test/file1.txt"

    // Remove directory if present.
    // Do this before extension removal incase directory has a period character.
    const size_t last_slash_idx = filename.find_last_of("\\/");
    if (std::string::npos != last_slash_idx)
    {
        filename.erase(0, last_slash_idx + 1);
    }

    std::cout << filename;

    return 0;
}
sonulohani
  • 1,225
  • 9
  • 8
  • Thank you. This was very straight forward :) Ps :- I cant seem to tag you @sonulohani – iWiiCK Jul 30 '20 at 04:44
  • I'm working on a very basic Antivirus solution implementation. Your solution was the first step to my code. I'm currently working on the next part of it following [https://stackoverflow.com/a/1220177/13370488] this method. But where to specify the file path to get the hash isn't very clear for me. I cant Comment on that exact answer because i'm new here and doesn't have that much reputation. Would you be able to help me ? I'm an undergraduate student in software engineering :) @sonulohani – iWiiCK Jul 30 '20 at 06:55
  • 1
    Appreciate it a lot mate @sonulohani but after messing with the code for sometime i figured it out. Still. thank you loads.:) – iWiiCK Jul 31 '20 at 05:15