954,492 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Errors writing a vector to a file

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.

#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);
}
johnson9000
Newbie Poster
11 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

>// 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.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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.

johnson9000
Newbie Poster
11 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 
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:

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

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

shouldn't this code read and write vectors?

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.

johnson9000
Newbie Poster
11 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

>shouldn't this code read and write vectors?
No, and this is why:

vector<short> v;
...
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:

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);
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I see my mistake. Thanks for your help.

johnson9000
Newbie Poster
11 posts since Mar 2005
Reputation Points: 10
Solved Threads: 0
 

I',m having some trouble using your programs all i have done so far is to write aa main to test them.

#include <iostream>
#include <vector>

using namespace std;

void write_to_file(const char * filename, vector<short> v);

vector<short> read_from_file(const char * filename);

int main()
{
   vector<short>v(3);
    cout<<v[0]<<endl;
    cout<<v[1]<<endl;
    cout<<v[2]<<endl;

    const char*f="file1.dat";
   write_to_file(f,v);*/

}
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);
}


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|

lnname
Newbie Poster
1 post since Jan 2010
Reputation Points: 10
Solved Threads: 0
 
#include <fstream>

And resist the urge to reply to years-old threads.

Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You