944,167 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 6984
  • C++ RSS
Mar 1st, 2005
0

Errors writing a vector to a file

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnson9000 is offline Offline
11 posts
since Mar 2005
Mar 1st, 2005
0

Re: Errors writing a vector to a file

>// 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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 1st, 2005
0

Re: Errors writing a vector to a file

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnson9000 is offline Offline
11 posts
since Mar 2005
Mar 1st, 2005
0

Re: Errors writing a vector to a file

Quote 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:
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 1st, 2005
0

Re: Errors writing a vector to a file

I have no idea why my code works the way it does.

shouldn't this code read and write vectors?

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnson9000 is offline Offline
11 posts
since Mar 2005
Mar 1st, 2005
0

Re: Errors writing a vector to a file

>shouldn't this code read and write vectors?
No, and this is why:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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. }
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Mar 1st, 2005
0

Re: Errors writing a vector to a file

I see my mistake. Thanks for your help.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
johnson9000 is offline Offline
11 posts
since Mar 2005
Jan 21st, 2010
-2
Re: Errors writing a vector to a file
I',m having some trouble using your programs all i have done so far is to write aa main to test them.
C++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. void write_to_file(const char * filename, vector<short> v);
  7.  
  8. vector<short> read_from_file(const char * filename);
  9.  
  10. int main()
  11. {
  12. vector<short>v(3);
  13. cout<<v[0]<<endl;
  14. cout<<v[1]<<endl;
  15. cout<<v[2]<<endl;
  16.  
  17. const char*f="file1.dat";
  18. write_to_file(f,v);*/
  19.  
  20. }
  21. void write_to_file(const char * filename, vector<short> v)
  22. {
  23. ofstream outf(filename);
  24. outf << v.size() <<' '; // Notice the space, it's important
  25. for(long i=0;i<v.size();i++)
  26. {
  27. outf << v[i];
  28. outf << " ";
  29. }
  30. outf.close();
  31. }
  32.  
  33. vector<short> read_from_file(const char * filename)
  34. {
  35. short size;
  36. ifstream inf(filename);
  37. inf >> size;
  38. vector<short> v(size);
  39. for(long i=0;i<size;i++)
  40. {
  41. inf >> v[i];
  42. }
  43. inf.close();
  44. return(v);
  45. }

but i get two errors in your functions


|error: variable `std::ofstream outf' has initializer but incomplete type|
|error: variable `std::ifstream inf' has initializer but incomplete type|
Reputation Points: 10
Solved Threads: 0
Newbie Poster
lnname is offline Offline
1 posts
since Jan 2010
Jan 21st, 2010
1
Re: Errors writing a vector to a file
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
And resist the urge to reply to years-old threads.
Last edited by Dave Sinkula; Jan 21st, 2010 at 11:54 am.
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Check for map in map
Next Thread in C++ Forum Timeline: how to display words backwards





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC