how compare a string and a file name\folder?

Dec 3, 2018 at 6:27pm
the file folder name it's a string, so how can i compare a string with a file name? or C++ have another type?
i want create a function(with same name but use differents arguments) that accepts a file name instead a normal string. what you can advice me?
Dec 3, 2018 at 6:44pm
This object you have that represents the name of a file. What type is it? What class? Is it a string?

string filename;

or something else?
Dec 3, 2018 at 6:50pm
more than that..
we can have on same string:
"hello world"
or
"C:\\faldername\\filename.txt"
seen these what is the difference between the 1st string and the second?
i can't compare "C:\\", because we can use another drive. maybe compare the 3 last chars, right? because i only need 1 or 2 extensions
Dec 3, 2018 at 6:54pm
i'm sorry, but using fstream type on function arguments, i can use the file and folder names?
Dec 3, 2018 at 6:55pm
Are you interested in comparing the actual files? If so, sounds like you need https://en.cppreference.com/w/cpp/filesystem/path

You can construct them with a string type, and then compare them with the https://en.cppreference.com/w/cpp/filesystem/equivalent function (and various other functions). You can get just the filename with https://en.cppreference.com/w/cpp/filesystem/path/filename

Have a read.


If you're actually just interested in comparing strings, then they're just strings. Just compare them, or compare substrings of them, like any other string.
Dec 3, 2018 at 6:58pm
i'm sorry, but using fstream type on function arguments, i can use the file and folder names?


fstream doesn't know the name of the file.

https://stackoverflow.com/questions/10773391/getting-filename-or-path-from-fstream
Dec 3, 2018 at 7:00pm
header function:
void GetFileInfo(fstream File)

used:
GetFileInfo("C:\\foldername\\filename.txt")
not tested, but can be used on these way?
Dec 3, 2018 at 8:00pm
No, you can't implicitly convert a string literal or std::string to an std::fstream.

You have two options available:
1. Pass in a reference to an existing std::fstream
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example program
#include <iostream>
#include <string>
#include <fstream>

void GetFileInfo(std::fstream& File)
{
    // do stuff with File
}

int main()
{
    std::fstream f("C:\\foldername\\filename.txt");
    GetFileInfo(f);
}


2. Create the std::fstream within the function.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Example program
#include <iostream>
#include <string>
#include <fstream>

void GetFileInfo(const std::string& Filename)
{
    std::fstream File(Filename);
    // do stuff with File
}

int main()
{
    GetFileInfo("C:\\foldername\\filename.txt");
}


Note that, in general, it is discouraged to use absolute paths because it makes programs less portable.

PS: Even on Windows, you can still use '/' instead of '\\' when opening files with fstream.
GetFileInfo("C:/foldername/filename.txt"); works fine!
Last edited on Dec 3, 2018 at 8:03pm
Dec 3, 2018 at 8:04pm
and using:
if (!File) std::cout << "Failed to open file" << std::endl;
i can test if the file exists... thank you so much for all
Topic archived. No new replies allowed.