Selecting Types for infile Variables

  • Thread starter kronecker
  • Start date
  • Tags
    Variables
In summary: It seems like your problem is not well defined. Can you give a specific example of what you are trying to accomplish? It would be helpful to see some code or a more detailed explanation of your problem.
  • #1
kronecker
19
0
How can i tell compuler select the correct type to whether to be able to 'infile' into int or float vars declared as private member in a class. sorry for my bad english.
Code:
template<bool TypeFlag, typename iType, typename fType>
		struct Selector{
			typedef iType Result;
		};
		
		template<typename iType, typename fType>
		struct Selector<false, iType, fType>{
			typedef fType Result;
		}; 
		void ReadFile(const char* filename){
			try{
				std::ifstream infile(filename);

				//////////////What can I do here///////////

			 catch(...){
				std::cerr<<"Readfile error\n";
				exit(EXIT_FAILURE);
			}
		}

Thanks in advance
Regards,
 
Computer science news on Phys.org
  • #2
I'm not sure I understand you. Do you want to have the computer read something from an infile and then decide whether it is an int or a float? In that case, it's impossible, unless you impose additional requirements (eg. int variables should be between 0 and 1000). Suppose you write something to an output file using something like:

int data_1 = 10;
float data_2 = 20;
outfile << data_1 << data_2;


The problem is that no type information is saved - you have to keep track of what went in through some other method.
 
  • #3
Here's an idea: When you create the file print a character before the number to signify which type it is. For example

i123 d12.42

i = int
d = double

When you read back the file you'll know what type to store the variable in.

You can do the same thing with classes, for example:

classname:eek:bject_name {variable1 , classname:eek:bject_name {variable 1, variable 2} , variable 3}

When you convert variables or objects using this technique it is called serializing data. When you read back the data it is called unserializing.
 
  • #4
thank you, zefram_c and dduardo

i have a class that i would use templates, the types will be, say, typename T and typename U, and in this class, i have some other functions among which there will be some that are 100% the same as each other in implementation but differ in types, say X and Y, that will be passed later from code in main function. i am not checking types of variables, just looking for a way to 'switch' from type to type and ways to use that 'switcher' in actuality. i posted code above only as an example. so, do you know how to deal with this problem ? i think it is trivial to you. but i am quite a dumb guy and really busy these day, i can't think much, i need help.
thanks if you or someone could be a little bit more definitive about the problem i am having.

Regards,
---really sorry for my bad english and misunderstandings if any.
 
  • #5
kronecker, i don't understand what you want to do. What exactly is it that you want to "switch"? With your example above you could do something like:

Code:
// modify this at compile time, e.g. thru preprocessing based on platform
const bool a = true; 

template <typename T>
void f() {cout << typeid(T).name() << endl; }

int main()
{
   typedef Selector<a, int, float>::Result T;
   f<T>();
}

But I'm not sure where you could use something like this. Or even if I'm close to what you want.. BTW, the most important thing to notice in the code above is that "a" MUST be computable at compile time.
 
  • #6
i write simple classes for numerical computation. i was looking for ways to use that selector more effectively or maybe some other ways to handling type problems, my previous posts should already meantion, thank you very much for your help anyway.
 
  • #7
What type problems?
 

1. What is the purpose of selecting types for infile variables?

The purpose of selecting types for infile variables is to specify the type of data that will be read from a file into your program. This ensures that the data is interpreted correctly and can be used in the intended manner.

2. How do I select the appropriate type for infile variables?

The appropriate type for infile variables depends on the type of data that is being read from the file. For example, if the data is numeric, you would use a numeric type such as integer or float. If the data is textual, you would use a string type. It is important to choose the correct type to avoid errors and ensure the data is properly handled.

3. Can I change the type of an infile variable after it has been selected?

Yes, you can change the type of an infile variable after it has been selected. However, this may result in data loss or incorrect data interpretation. It is recommended to select the correct type from the beginning to avoid any issues.

4. Are there any limitations to selecting types for infile variables?

There are some limitations to selecting types for infile variables. For example, if the data in the file is not in the expected format, it may cause errors or incorrect data interpretation. Additionally, some programming languages have specific limitations on the types that can be used for infile variables.

5. What should I do if I am unsure of the appropriate type for an infile variable?

If you are unsure of the appropriate type for an infile variable, you can consult the documentation for your programming language or seek assistance from a more experienced programmer. You can also test different types to see which one properly interprets the data from the file.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
1
Views
895
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Programming and Computer Science
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Programming and Computer Science
Replies
6
Views
920
  • Programming and Computer Science
3
Replies
75
Views
4K
Replies
63
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
3K
Back
Top