0

Hello,

I'm developing an application that reads a text file, and then replaces what's in the text file with an array (alphabet) It all works ok until I try and change the position of the inputted character and it returns:

Segmentation fault: 11 It's really confusing!

Here is my code:

#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

bool file_exists(string theFile)
{
	ifstream file(theFile.c_str());
	if(file) 
	{
		
		return true;
	}else{
	  return false;
	}
	
}

int main()
{
	string alphabet[26] = {"a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z" };
	if(file_exists("helloworld.txt"))
	{
		string str;
		string words[1000];
		int* pointer;
		ifstream file("helloworld.txt");
		
		for(int i=0; !file.eof(); i++)
		{
		   words[i] = file.get();
		   for(int o=0; (o < 26); o++)
		   { 
			  if(words[i] == alphabet[o])
			  {
			  	 words[i] = alphabet[o + 9];
			  	 cout << words[i];	
			  	
			}else{
			
			}
		
		  }
	
		}
	}else{
	  cout << "The file does not exist";
	}

	return EXIT_SUCCESS;
}

Any ideas? Thanks =)

3
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by sundip
0

Hello,

I'm developing an application that reads a text file, and then replaces what's in the text file with an array (alphabet) It all works ok until I try and change the position of the inputted character and it returns:

Segmentation fault: 11 It's really confusing!

So is your description!
You want to
1) read a file
2) replace the contents with the alphabet
3) change the position of inputted characters?
Can you try to explain better what you need? What's the assignment? What are you trying to accomplish?

0

I am not very sure about your requirement but I think you are getting segmentation fault at line :36

words[i] = alphabet[o + 9];

As value of 'o' vary from 0 to 25 and your array alphabet[] has only 26 objects.
So at the time of operation alphabet[o + 9], if o > 16 it will try to access a location that is not exist in array alphabet[] e.g 21+9 = alphabet[30] that why you are getting segmentation fault.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.