type

in python, if let' say I want to obtain the type of a variable,
I simply write as an example:

a=10
print(type(a))

output= int

Is there a way to do this for C/C++ ?

And let's say that I find out that the type is a file, how would I convert it to 'char' ?
Last edited on
The answer is 'yes' technically, C++ has run-time type information (RTTI) available.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Example program
#include <iostream>
#include <string>
#include <typeinfo>

int main()
{
    int a = 42;
    
    if (typeid(a) == typeid(int))
    {
        std::cout << "Yes, it's an int. But you already knew that.\n";   
    }
}


But better answer is 'no', or 'please don't': C++ is a statically typed language, and the type a will always be known at compile-time, so thinking in Python terms will probably just produce inefficient or confusing C++ code.

And let's say that I find out that the type is a file, how would I convert it to 'char' ?
This question really doesn't make sense. What does it mean for a file to be a char? A char is a single character. Are you saying you want the first character from a file? Please clarify.

Really -- I hope you reply with more specifics of what problem you're running into, because I guarantee there's a better way to solve it than with typeid.

Edit: Based on your question from the other thread, it sounds like you want to convert the contents of a file to char array? That's very different than converting it into a 'char'. :)

To convert a file stream into a string, you can do:
1
2
3
4
5
6
7
8
9
10
#include <fstream>
#include <string>
#include <sstream>

// https://stackoverflow.com/questions/116038/how-do-i-read-an-entire-file-into-a-stdstring-in-c
std::string slurp(std::ifstream& in) {
    std::stringstream sstr;
    sstr << in.rdbuf();
    return sstr.str();
}
Last edited on
Ganado wrote:
you want to convert the contents of a file to char array?

Trying to follow the OP’s questions, we’ve start adopting his terminology :)

In the other thread the OP proposes a code where the opened file is read into a std::vector<char> by means of std::istreambuf_iterator<char>s.
I guess he could do the same with a std::string… To what avail, it’s a mystery.
To what avail, it’s a mystery.
Possibly not a mystery - just a waste of time and effort.

The way I 'understand' it from other posts from @OP it goes like this:
1. OP wants to send files (images in fact) using sockets blah blah
2. OP has numerous problems with directories and dirent.h structure blah blah
3. OP has been 'able' to send a file, maybe, blah blah
4. OP gets file size somewhere in this process as zero size blah blah
5. OP knows file is actually bigger than that, megabytes in fact blah blah
6. OP was advised that maybe just sending a bit of text might be better blah blah
7. OP now wants to circumvent that with sending a char array or string blah blah
8. OP has created an ever-deepening hole of misery blah blah
9. OP blah, blah, blah
Edit: Based on your question from the other thread, it sounds like you want to convert the contents of a file to char array?

You are totally right, that's what I am trying to do.

I have a question:
In your code block, which would be the name of the file?
You see, when it comes to open files, I am used to
the following

FILE *f=open("pic.jpg", "r")

then i know that f is the file
Well, you can use 'auto', which tends to do what python would do without any information.

1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>
#include <typeinfo>

int main()
{
   auto a = 42;
   std::cout << typeid( a ).name();
}

You will be told that a is an int ("i"), because that is what the language standard prescribes in this situation.

But what if you want a to be an unsigned short, or an unsigned long long ... or even a double that just happened to have a whole-number value?

Python and C++ work in very different ways; they work best if you code to the idioms of that particular language and don't presume that lines of code will necessarily work in the same way when taken from one language to the other.
In your code block, which would be the name of the file?
If you are using C++ (as opposed to just C), then the usual suggestion would be to use C++ file streams, <fstream>.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

// https://stackoverflow.com/questions/116038/how-do-i-read-an-entire-file-into-a-stdstring-in-c
std::string slurp(std::ifstream& in) {
    std::stringstream sstr;
    sstr << in.rdbuf();
    return sstr.str();
}

int main()
{
	std::ifstream fin("main.cpp"); // (this file)
	std::string contents = slurp(fin);
	std::cout << contents << '\n';
}


______________________

You will be told that a is an int ("i"), because that is what the language standard prescribes in this situation.
Just for completeness (I'm not trying to be a smartass, I swear), the language actually doesn't specify what string the name() function will become. It seems to be more helpful for purposes of debugging than actual logic, if useful at all.
cppreference wrote:
No guarantees are given; in particular, the returned string can be identical for several types and change between invocations of the same program.
I'd assume most compilers are a bit stricter than that, though.
Last edited on
Topic archived. No new replies allowed.