Errors writing a vector to a file

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2005
Posts: 11
Reputation: johnson9000 is an unknown quantity at this point 
Solved Threads: 0
johnson9000 johnson9000 is offline Offline
Newbie Poster

Errors writing a vector to a file

 
0
  #1
Mar 1st, 2005
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.

  1. #include <vector.h>
  2. #include <fstream.h>
  3.  
  4. void write_to_file(const char * filename, vector<short> v)
  5. {
  6. //const char * filename = "/home/johnson/data/data.txt";
  7. ofstream outf(filename);
  8. for(long i=0;i<v.size();i++)
  9. {
  10. outf << v[i];
  11. outf << " ";
  12. }
  13. outf.close();
  14. }
  15.  
  16. vector<short> read_from_file(const char * filename)
  17. // there is probably a better way to write this function
  18. {
  19. vector<short> v;
  20. //const char * filename = "/home/johnson/data/data.txt";
  21. ifstream inf(filename);
  22. for(long i=0;i<v.size();i++)
  23. {
  24. inf >> v[i];
  25. }
  26. inf.close();
  27. // I think there should be a return but my compiler doesn't give me an error
  28.  
  29. }
  30.  
  31.  
  32. void testVector(vector<short> a)
  33. {
  34. cout << "begin testVector | ";
  35. long max = a.size();
  36. for(short i=0;i<max;i++)
  37. {
  38. cout << a[i] << " ";
  39. }
  40. cout << endl;
  41. }
  42.  
  43. int main()
  44. {
  45. vector<short> newVector;
  46. for(short i=0;i<5;i++) newVector.push_back(0);
  47. testVector(newVector);
  48. write_to_file("/home/johnson/data/data1.txt", newVector);
  49. testVector( read_from_file("/home/johnson/data/data1.txt") );
  50. return(0);
  51. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Errors writing a vector to a file

 
0
  #2
Mar 1st, 2005
>// I think there should be a return but my compiler doesn't give me an error
Just because your compiler doesn't complain doesn't mean that there's no error.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 11
Reputation: johnson9000 is an unknown quantity at this point 
Solved Threads: 0
johnson9000 johnson9000 is offline Offline
Newbie Poster

Re: Errors writing a vector to a file

 
0
  #3
Mar 1st, 2005
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Errors writing a vector to a file

 
0
  #4
Mar 1st, 2005
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.
Oh, it's all okay then. :rolleyes:
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 11
Reputation: johnson9000 is an unknown quantity at this point 
Solved Threads: 0
johnson9000 johnson9000 is offline Offline
Newbie Poster

Re: Errors writing a vector to a file

 
0
  #5
Mar 1st, 2005
I have no idea why my code works the way it does.

shouldn't this code read and write vectors?

  1. void write_to_file(const char * filename, vector<short> v)
  2. {
  3. ofstream outf(filename);
  4. outf << v.size();
  5. for(long i=0;i<v.size();i++)
  6. {
  7. outf << v[i];
  8. outf << " ";
  9. }
  10. outf.close();
  11. }
  12.  
  13. vector<short> read_from_file(const char * filename)
  14. {
  15. short size;
  16. vector<short> v;
  17. ifstream inf(filename);
  18. inf >> size;
  19. for(long i=0;i<size;i++)
  20. {
  21. inf >> v[i];
  22. }
  23. inf.close();
  24. return(v);
  25. }

I've been trying for hours to understand why it crashes. Any help would be greatly appreciated.
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,752
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 740
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Errors writing a vector to a file

 
0
  #6
Mar 1st, 2005
>shouldn't this code read and write vectors?
No, and this is why:
  1. vector<short> v;
  2. ...
  3. inf >> v[i];
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:
  1. void write_to_file(const char * filename, vector<short> v)
  2. {
  3. ofstream outf(filename);
  4. outf << v.size() <<' '; // Notice the space, it's important
  5. for(long i=0;i<v.size();i++)
  6. {
  7. outf << v[i];
  8. outf << " ";
  9. }
  10. outf.close();
  11. }
  12.  
  13. vector<short> read_from_file(const char * filename)
  14. {
  15. short size;
  16. ifstream inf(filename);
  17. inf >> size;
  18. vector<short> v(size);
  19. for(long i=0;i<size;i++)
  20. {
  21. inf >> v[i];
  22. }
  23. inf.close();
  24. return(v);
  25. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 11
Reputation: johnson9000 is an unknown quantity at this point 
Solved Threads: 0
johnson9000 johnson9000 is offline Offline
Newbie Poster

Re: Errors writing a vector to a file

 
0
  #7
Mar 1st, 2005
I see my mistake. Thanks for your help.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC