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

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

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

I see my mistake. Thanks for your help.

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|

#include <fstream>

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.