Error with File Loading

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Error with File Loading

Post by lotios611 »

Well, I was just screwing around with file loading, and this weird bug happened. I've fixed the bug, but I just wanted to post what happened.

Here's main.cpp:

Code: Select all

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

int main()
{
	std::string temp[5];
	int i = 0;
	std::fstream file;
	file.open("Dictionary.txt");
	if (file.is_open())
	{
		while (!file.eof())
		{
			std::getline(file, temp[i++]);
			std::cout << temp[i++] << std::endl;
		}
		file.close();
	}
	else
	{
		std::cout << "Unable to load file";
	}
	return 0;
}
And here's Dictionary.txt:

Code: Select all

Hello - A word
Testing - Testing
APPLES - ANOTHER WORD
banana - a fruit 
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
dani93
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 38
Joined: Mon Apr 13, 2009 9:38 am
Location: Austria

Re: Error with File Loading

Post by dani93 »

Your loop is wrong. It should be:

Code: Select all

for (int i = 0; !file.eof(); i++)
{
  std::getline(file, temp[i]);
  std::cout << temp[i] << std::endl;
}
You got a too high index cause you incremented "i" wrong -> segmentation fault
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Error with File Loading

Post by lotios611 »

Like I said, I already fixed it. So that's what a segmentation fault is...
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
User avatar
short
ES Beta Backer
ES Beta Backer
Posts: 548
Joined: Thu Apr 30, 2009 2:22 am
Current Project: c++, c
Favorite Gaming Platforms: SNES, PS2, SNES, SNES, PC NES
Programming Language of Choice: c, c++
Location: Oregon, US

Re: Error with File Loading

Post by short »

lotios611 wrote:Like I said, I already fixed it. So that's what a segmentation fault is...
Segment faults (on the low level) happen when you try and de-reference an invalid pointer. Since arrays are after all pointers, when you try to index that is larger then you declared your array size to be, you are indeed de-referencing an invalid pointer. :)
My github repository contains the project I am currently working on,
link: https://github.com/bjadamson
User avatar
lotios611
Chaos Rift Regular
Chaos Rift Regular
Posts: 160
Joined: Sun Jun 14, 2009 12:05 pm
Current Project: Game engine for the PC, PSP, and maybe more.
Favorite Gaming Platforms: Gameboy Micro
Programming Language of Choice: C++

Re: Error with File Loading

Post by lotios611 »

I actually never bothered to look "Segmentation fault" up. You learn something new every day.
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
Post Reply