Display txt file using vector

Trying to display information from a text file using vectors

Page 1 of 1

3 Replies - 2281 Views - Last Post: 07 April 2009 - 05:13 AM Rate Topic: -----

#1 Jud202  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 02-March 09

Display txt file using vector

Post icon  Posted 05 April 2009 - 06:04 PM

I am trying to display data from a .txt file using vectors, but for some reason I get a display involving 10168 "0"
Its really annoying, but I can't figure out why it would do that.

The only code I actually made is

void display_vector(const vector<int>& v)
{
  cout << "size is " << v.size();
	if (v.size() == 0) 
		cout << "; no elements." << endl;
	else 
	{
		cout << "; elements are { ";
		for (int i = 0; i < v.size(); i++)
		{
			if (i > 0)
			{
				cout << " | ";
			}
			cout << v.at(i); 
		}
		cout << " }";
	}
}


and the data the .txt file contains is:

1
1989

It should display:

size is 2; elements are { 1 | 1989 }

but I'm getting:

size is 10168; elements are { 0 | 0 | 0 |...... continued 10168 times!!

when I type in:

./myprog < Untitled.txt in my terminal window.

Any help would be greatly appreciated. Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Display txt file using vector

#2 bsaunders  Icon User is offline

  • D.I.C Addict

Reputation: 44
  • View blog
  • Posts: 571
  • Joined: 18-January 09

Re: Display txt file using vector

Posted 05 April 2009 - 06:12 PM

Can you post the code that reads the file contents into the vector?
Was This Post Helpful? 0
  • +
  • -

#3 Jud202  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 53
  • Joined: 02-March 09

Re: Display txt file using vector

Posted 06 April 2009 - 11:04 AM

void read_vector(vector<int>& v)
{
  int size_input;

  cout << "Reading data into a vector<int> " << endl;
  cin >> size_input;
 
  if (size_input == 0) 
	{
	  cout << "Making vector empty." << endl;
	  v.resize(0);
	}

  else 
	{
	  v.resize(size_input);
 
	  for (int i = 0; i < v.size(); i++)
	cin >> v.at(i);
	}
}

Was This Post Helpful? 0
  • +
  • -

#4 Plus  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 41
  • View blog
  • Posts: 414
  • Joined: 24-November 08

Re: Display txt file using vector

Posted 07 April 2009 - 05:13 AM

your code only reads from user not file ...

if you want reading from a file, there are many ways ...

by using File Streams,
   #include<iostream>
   #include<vector>
   #include<fstream>
   using namespace std;

int main()
{
	fstream File("C:\File.txt",ios::in | ios::out); // "the File's place"
	...



reading a manual fed file
int a=1,b=2,c=3;

File << a << b << c; //write manually
File >> a >> b >> c; // read


or reading dynamically
vector<int> v(3);
v.push(a);
v.push(b);
v.push(c);

File.write(interpret_cast<*char>(&v),sizeof(v));
File.read(interpret_cast<*char>(&v),sizeof(v));



:: show your thanks as my hand of help
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1