4 Replies - 873 Views - Last Post: 06 March 2012 - 05:33 AM Rate Topic: -----

#1 Kiakime   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 27-February 12

Why's this crashing the program? [getline with a vector]

Posted 06 March 2012 - 03:56 AM

I'm just making a simple example to try to figure out getline. The file does exist and reads in fine, but I'm getting abort errors when I try to run this. The error message just says that "abort" has been called. The file is just "test test1 test2 test3 test4".

#include <iostream>
#include <string>
#include <fstream>
#include <vector>

using namespace std;

int main () {

	vector<string> vecTest;

	string test[4];
	fstream testing("testing.txt");

	for (int i = 0; i < 2; i++){
	getline(testing, vecTest.at(i), ' ');
	cout << vecTest.at(i);
	}

	return 0;
}


My goal is to be able to take a file, read it in, cycle through the words and check if they match anything. I want to use a vector instead of an array so the number of elements can be flexible.

Is This A Good Question/Topic? 0
  • +

Replies To: Why's this crashing the program? [getline with a vector]

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: Why's this crashing the program? [getline with a vector]

Posted 06 March 2012 - 04:40 AM

When you create a new vector using the default constructor, it will have size 0 and any attempts to call at on it will crash (and any attempts to use [] on it will lead to undefined behavior).

You can either give vecTest the proper size (i.e. vector<string> vecTest(2);) or you can use getline with a temporary string and then use push_back to add that temporary string to the end of the vector. Of course in cases where you know the size of the vector in advance, creating the vector with the correct size is faster than using push_back.
Was This Post Helpful? 1
  • +
  • -

#3 Kiakime   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 27-February 12

Re: Why's this crashing the program? [getline with a vector]

Posted 06 March 2012 - 04:44 AM

I think this attempt is better:

fstream members("\\member\\members.txt");

			int size = 0;
			string line;

			while (getline(members, line))
				{
				size++;
				 }

			cout << size;


But it never increments. Why? I'm trying to figure out if I need to specify '\n' somewhere.

Oops, sorry, you posted while I was posting. Thanks for the help, I'll definitely keep that in mind.
Was This Post Helpful? 0
  • +
  • -

#4 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2770
  • View blog
  • Posts: 4,429
  • Joined: 21-June 11

Re: Why's this crashing the program? [getline with a vector]

Posted 06 March 2012 - 04:56 AM

View PostKiakime, on 06 March 2012 - 12:44 PM, said:

fstream members("\\member\\members.txt");

			int size = 0;
			string line;

			while (getline(members, line))
				{
				size++;
				 }

			cout << size;


But it never increments. Why?


Maybe the file is empty or doesn't exist at that location?

Just to be sure: Are you aware that a path that starts with a backslash is an absolute path (well relative to the current drive, but not the current directory) in Windows?

This post has been edited by sepp2k: 06 March 2012 - 04:58 AM

Was This Post Helpful? 1
  • +
  • -

#5 Kiakime   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 24
  • Joined: 27-February 12

Re: Why's this crashing the program? [getline with a vector]

Posted 06 March 2012 - 05:33 AM

Yep, I'm aware. Also got it working, the file was fine but I'm not sure what I even thought my logic for that while statement was.

Thanks for the help. This worked fine --

fstream members("member\\members.txt");
	string mems;
	int stuff = 0;

	while (!members.eof()){
		getline(members,mems);
		stuff++;
	}
	
	cout << stuff;


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1