I'm still trying to figure out C++ file I/O. Is it possible to dereference a pointer to an fstream and pass the results to a function? I'm trying this:
template <class T> int getVector(fstream& s,
int32_t nbrCells,
typename vector<T>::iterator iter)
{
int rc = 0;
typename vector<T>::iterator save = iter;
if (s.flags() & ios::hex)
{
uint32_t utemp;
while ((distance(save, iter) != nbrCells) && (s >> utemp))
*iter++ = utemp;
}
else
{
int32_t temp;
while ((distance(save, iter) != nbrCells) && (s >> temp))
*iter++ = temp;
}
It reads one value, then returns (with a fail). Any ideas?
EDIT:
I see in your example that you're working with an actual stream, not a pointer to a stream. This may be the difference. If I can't use pointers, I'll have to do something else with my routine that opens the files for me.
Here's part of my openfile routine:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
fstream* openFile(string& name,
constint ioMode) // 0 = read; 1 = write
{
fstream* fp = new fstream;
if (ioMode == FILE_READ)
{
// make sure the input file exists.
fp->setf(ios_base::hex, ios::basefield);
fp->open(name.c_str(), ifstream::in);
if (!fp->good())
{
cout << "Demod: error opening " << name << "." << endl;
}
}
...
return fp;
}
I'm more than willing to modify this if I can figure out how to return an fstream.
@mzimmers, your code works in the following testcase: http://ideone.com/bc1l7 (I only changed fstream to istream, to have a self-contained testcase). What is your input?
At the moment, it's just a text file that contains 4 very large numbers (I'm only bothering to read two) in decimal format. Each number is on its own line. And again, the first read works (I can see it in the debugger), but I think that s fails the test in the while loop after that.
This routine worked fine until I modified it; it used to just accept an ifstream by value. I changed the ifstream to fstream (because I'm also trying to use a different utility function to open the files for me), and am now trying to pass the fstreams by reference rather than by value.
I would be happy to revert to the original getVector() if I could figure out how to get my openFile routine to return an fstream and not a pointer to an fstream.
Hi, ne555. Thanks for the reminder on that. With regard to the ios fields, I think I have those set correctly, since the routine runs the correct code.
So: my application needs to open and read several files. I'm trying to do both the opens and the reads with utility functions. My open only works with pointers to fstreams, and my read won't accept the pointer. What's my best plan B here?
EDIT:
Here's an abstract of what I'm trying to do:
1 2 3 4 5 6 7 8
fstream* s;
fileName = "coeffs/nyquist.txt";
s = openFile(fileName, FILE_READ);
s->flags(ios::dec);
iter = hostValues.begin() + COEFF_DEMOD_NYQ_00_03;
rc = getVector<FlipFlopReg32>(*s, 2, iter);
Can you see a way of making this happen? Again, I easily can change "s" to just an fstream instead of a pointer.
OK...I now have my calling routines passing the fstream to fileopen() by reference, and the program compiles, and runs to the point that the opens seem to succeed.
Now, though, I'm back to the problem mentioned above, namely that getVector() doesn't work with the stream passed by reference. It reads one value from the file, then returns an error.
I need to fix my getVector() program...can someone see a solution?
EDIT:
Could the problem be the fstream instead of the ifstream? Currently I use fstreams because I need to use openFile for both input and output files. Before, getVector() was using ifstreams. If this is the problem, I'm not sure how I'll go about fixing it.
The getVector() routine is fine. The problem was, in all the confusion, I wasn't setting the ios flags properly for the file. Now that I've done that, all seems well. (I love screwing myself into the ground for hours over dumb mistakes.)
I love screwing myself into the ground for hours over dumb mistakes.
You never walk alone. Your mistake is something that had happened to seasoned programmers too! In fact, before we become seasoned programmers we are also beginners like all of you. It is just that we persevere on for many years. Among the we, some have left this line and went into other industries.
Programming is not something that is for everyone. For those that do, the passion to program is strong and continue to do even if we retire? :P