A
a
(I've reached that familiar place where I've got a nagging little
problem in a program I'm writing but I've been staring at code for too
long and I probably wouldn't be able to recognize the answer even if I
was staring right at it.)
I'm trying to design a function that reads input from my data file into
(a pair of) arrays. Simple enough? However, each line of the file is
either going to be 4 integers or a character (then a carriage return)
and I'm not sure which (i.e. there could be 4 integers followed by
another line of 4 integers followed by a letter or it could just be 1
letter/1 letter/1 letter). If the input is a character, it goes into
charArray, but if the input are those 4 integers, then they take up the
next 4 spots in intArray.
So, basically, I need to know how to read in something from a file when
I don't know its type. Also, I'm a bit rusty on the C++ and I've never
done file I/O so I'm trying to do this in the simplest way possible but
most of the file input examples I've seen online contain many function
calls that I don't understand. So when explaining, please do the typing
equivalent of speaking loudly and slowly =)
//The initializations are simple enough:
int in1, in2, in3, in4;
char ch1;
ifstream file_in;
file_in.open("test.txt");
if (!file_in)
{
cout << "Cannot open the file test.txt" << endl;
return (-1);
}
//And I think I have the while loop:
while( (file_in >> in1 >> in2 >> in3 >> in4 ) || (file_in >> ch1) )
//But then, I'm not sure how to check for type with my input
thanks in advance,
-frank (email: (e-mail address removed))
problem in a program I'm writing but I've been staring at code for too
long and I probably wouldn't be able to recognize the answer even if I
was staring right at it.)
I'm trying to design a function that reads input from my data file into
(a pair of) arrays. Simple enough? However, each line of the file is
either going to be 4 integers or a character (then a carriage return)
and I'm not sure which (i.e. there could be 4 integers followed by
another line of 4 integers followed by a letter or it could just be 1
letter/1 letter/1 letter). If the input is a character, it goes into
charArray, but if the input are those 4 integers, then they take up the
next 4 spots in intArray.
So, basically, I need to know how to read in something from a file when
I don't know its type. Also, I'm a bit rusty on the C++ and I've never
done file I/O so I'm trying to do this in the simplest way possible but
most of the file input examples I've seen online contain many function
calls that I don't understand. So when explaining, please do the typing
equivalent of speaking loudly and slowly =)
//The initializations are simple enough:
int in1, in2, in3, in4;
char ch1;
ifstream file_in;
file_in.open("test.txt");
if (!file_in)
{
cout << "Cannot open the file test.txt" << endl;
return (-1);
}
//And I think I have the while loop:
while( (file_in >> in1 >> in2 >> in3 >> in4 ) || (file_in >> ch1) )
//But then, I'm not sure how to check for type with my input
thanks in advance,
-frank (email: (e-mail address removed))