| | |
Errors writing a vector to a file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Mar 2005
Posts: 11
Reputation:
Solved Threads: 0
Why does my file operation code write random data over the first two numbers I store in the file.
The code compiles and runs without error messages, but
I try to store the vector [0 0 0 0 0] and instead I get [20520 2414 0 0 0].
I am using linux if that makes a difference.
The code compiles and runs without error messages, but
I try to store the vector [0 0 0 0 0] and instead I get [20520 2414 0 0 0].
I am using linux if that makes a difference.
C++ Syntax (Toggle Plain Text)
#include <vector.h> #include <fstream.h> void write_to_file(const char * filename, vector<short> v) { //const char * filename = "/home/johnson/data/data.txt"; ofstream outf(filename); for(long i=0;i<v.size();i++) { outf << v[i]; outf << " "; } outf.close(); } vector<short> read_from_file(const char * filename) // there is probably a better way to write this function { vector<short> v; //const char * filename = "/home/johnson/data/data.txt"; ifstream inf(filename); for(long i=0;i<v.size();i++) { inf >> v[i]; } inf.close(); // I think there should be a return but my compiler doesn't give me an error } void testVector(vector<short> a) { cout << "begin testVector | "; long max = a.size(); for(short i=0;i<max;i++) { cout << a[i] << " "; } cout << endl; } int main() { vector<short> newVector; for(short i=0;i<5;i++) newVector.push_back(0); testVector(newVector); write_to_file("/home/johnson/data/data1.txt", newVector); testVector( read_from_file("/home/johnson/data/data1.txt") ); return(0); }
•
•
•
•
Originally Posted by johnson9000
i agree, however this code does work if i just add two extra numbers a the begining of my vector and then ignore the bad data that is written over them. But when I add a return at the end of my read_from_file function I get a compiler error.
I'm here to prove you wrong.
•
•
Join Date: Mar 2005
Posts: 11
Reputation:
Solved Threads: 0
I have no idea why my code works the way it does.
shouldn't this code read and write vectors?
I've been trying for hours to understand why it crashes. Any help would be greatly appreciated.
shouldn't this code read and write vectors?
C++ Syntax (Toggle Plain Text)
void write_to_file(const char * filename, vector<short> v) { ofstream outf(filename); outf << v.size(); for(long i=0;i<v.size();i++) { outf << v[i]; outf << " "; } outf.close(); } vector<short> read_from_file(const char * filename) { short size; vector<short> v; ifstream inf(filename); inf >> size; for(long i=0;i<size;i++) { inf >> v[i]; } inf.close(); return(v); }
I've been trying for hours to understand why it crashes. Any help would be greatly appreciated.
>shouldn't this code read and write vectors?
No, and this is why:
Tell me, what is the size of v? Where do you specify enough information for the vector to know that v[i] should be valid? You don't. What you have is an empty vector, and the subscript operator is an unchecked operation, so it tries to access the ith element of an empty vector, which will always fail no matter what value i has. Compare:
No, and this is why:
C++ Syntax (Toggle Plain Text)
vector<short> v; ... inf >> v[i];
C++ Syntax (Toggle Plain Text)
void write_to_file(const char * filename, vector<short> v) { ofstream outf(filename); outf << v.size() <<' '; // Notice the space, it's important for(long i=0;i<v.size();i++) { outf << v[i]; outf << " "; } outf.close(); } vector<short> read_from_file(const char * filename) { short size; ifstream inf(filename); inf >> size; vector<short> v(size); for(long i=0;i<size;i++) { inf >> v[i]; } inf.close(); return(v); }
I'm here to prove you wrong.
![]() |
Similar Threads
- Reading and writing to XML file with ActionScript (Graphics and Multimedia)
- Writing to INI file issue (C#)
- Writing data into a file (C)
Other Threads in the C++ Forum
- Previous Thread: Class that represents sets of integers
- Next Thread: saving progress
| Thread Tools | Search this Thread |
api application array arrays beginner binary bitmap c++ c/c++ calculator char char* class classes coding compile compiler console conversion convert count data database delete desktop developer directshow dll dynamic email encryption error file forms fstream function functions game generator getline google graph homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux loop looping loops map math matrix memory multiple newbie news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive return sorting string strings struct template templates test text tree unix url vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






