Why when reading from a file I get zeros after maybe reading half the data file? What could cause that? Do I need to increase the buffer size or something?
Printable View
Why when reading from a file I get zeros after maybe reading half the data file? What could cause that? Do I need to increase the buffer size or something?
Can you show us how you read the file?
Oh, I maybe on the wrong forum. But the code is listed here.Code:float input;
/*open data file*/
ifstream inFile("output");
//inFile.seekg(0,ios::cur);
inFile >> input;
while(!inFile.eof())
{
inFile >> input;
data.push_back(input); //vector
inFile >> input;
}
Try taking out the second inFile>>input statement.
Also, how is the data file laid out?Code:while(!inFile.eof())
{
inFile >> input;
data.push_back(input); //vector
//inFile >> input;
}
removing this line makes more sense:Code:float input;
/*open data file*/
ifstream inFile("output");
1) inFile >> input;
while(!inFile.eof())
{
2) inFile >> input;
data.push_back(input); //vector
3) inFile >> input
2) inFile >> input;
This technique allows you to check each individual input to see if it is EOF.
It's being laid out like
Code:
float p1;
fprintf(data,"%f\n",p1);
Thanks.
One of my friend told me that when I open the file to read from it, whatever is in the file previously (before I execute the other program) is all that will be displayed, not the new data that is being written?
Unfortunately your last post isn't very clear in what you intended to say. With C++ you can't add material into the middle of a preexisting file without rewriting the entire file, but you can add to the end of a file. If you open the file, read data into the program from the file until you find the end of the file, then append more material to the end of the file, then read the stuff appended to the end of the file, it might work, though I haven't tried it.
Code://assume filename.txt exists and has data in it already
ifstream fin("filename.txt");
fin >> data;
while(!fin.eof())
{
buffer.push_back(data);
fin >> data;
}
//file all read into buffer at this point
cout << buffer.size() << endl; //assume size is x.
//open stream to append to the end of the file.
ofstream fout("filename.txt", ios::app);
//append second copy of data to end of file
for(iterator = buffer.begin(); iterator != buffer.end(); ++iterator)
{
fout << *iterator;
}
fin.clear();//clear the EOF bit
//start reading where you left off
fin >> data;
while(!fin.eof())
{
buffer.push_back(data);
fin >> data;
}
cout << buffer.size() << endl; //size will probably be 2x now.
//etc.
> With C++ you can't add material into the middle of a
> preexisting file without rewriting the entire file,
Yes, you can. But you need a binary file (not text file) to do it, and it's a little more work.
> With C++ you can't add material into the middle of a
> preexisting file without rewriting the entire file,
You can if you use binary files and seekg( ). You'd have to rewrite all of the data from the insertion point on, but you don't have to re-write the entire file.