Page 1 of 1

Advice searching for index in a char array causing seg fault

PostPosted: Mon May 07, 2012 12:56 am
by LoGiCaL__
What up hts. Was hoping someone would be able to help me along. So far I have narrowed it down to the function it is happening in by outputing "TEST\n". However I am still having this issue.

Here is the previously mentioned function.


Code: Select all
void initialization::populateTempCharArr(std::string tempStr1, char tempCharArr[])
{
    int iterator;


    for(iterator = 0; iterator < returnHighestCount(); ++iterator)
    {
        if(tempStr1[iterator] == '\0')
            break;
        else
        {
                //This is where the seg fault occurs
            tempCharArr[iterator] = tempStr1[iterator];         

        }
    }
}


-- Mon May 07, 2012 12:58 am --

Ill come back and check on the forums. However, ill be on teamspeak for a while if you wanna bs.


Thanks in advance for any input.

-- Mon May 07, 2012 4:14 am --

Nvm I got rid of the fault. Thnx to atropine.

Re: Advice searching for index in a char array causing seg fault

PostPosted: Mon May 07, 2012 8:37 am
by ghost107
The problem when using a array as a parameter, the array will be a pointer(not a array):
Looking at the code and it crashes there, looks more you are sending a constant string or a unallocated pointer, as parameter.

Re: Advice searching for index in a char array causing seg fault

PostPosted: Mon May 07, 2012 9:24 am
by LoGiCaL__
Yeah i got rid of the char array, here is updated version of whole source. Main reason for program is to translate braille to readable english. Mind you it's not fully done yet. But I got past the seg fault and a littler further.

Here is updated source:

Code: Select all
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

class initialization
{
    private:
        int amount, highestCount;
        std::string brailleLine, str[3]; 

    public:

        void readLinesFromText(char *file[]);
        void setAmountOfLines(char *file[]);
        int returnAmount() { return amount; }
        void populateTempCharArr(std::string tempStr1);
        int returnHighestCount(){ return highestCount; }
        std::string returnStr(int index){ return str[index]; }

};

class brailleReading : public initialization
{
    private:

    public:
        void populateTempBrailleLetter(std::string row);   //<----------LEFT OFF HERE

};

void initialization::setAmountOfLines(char *file[])
{
    std::string line;
    std::ifstream myFile;                                            //change file name when done
    int count = 0, iterator = 0;


    myFile.open(file[1]);


    if(myFile.is_open())
    {
        while(myFile.good())
        {
            getline(myFile, line);

            for( ; iterator < 3; iterator++)
                str[iterator] = line;

            ++count;
        }

        myFile.close();

    }
    else
        std::cout << "Unable to open file\n";


    amount = count - 1;

}

void initialization::readLinesFromText(char *file[])
{
    std::ifstream myFile;                                  //change file name when done
    int count, iterator = 0;
    std::string tempStr;   
    std::ostringstream stringStream;

    myFile.open(file[1]);


    if(myFile.is_open())
    {
        while(myFile.good())
        {
            getline(myFile, brailleLine, '\0');
            stringStream << brailleLine;
            tempStr += stringStream.str();        //<<<<<<<<THIS IS WHERE PROBLEM IS AT

        }


        myFile.close();

    }
    else
        std::cout << "Unable to open file\n";

    while(iterator < tempStr.length())
       {
        if(tempStr[iterator] == '.')
            std::cout << tempStr[iterator];

        ++iterator;

    }
}

int main(int argc, char *argv[])
{

    brailleReading rayCharles;

    rayCharles.setAmountOfLines(argv);
    rayCharles.readLinesFromText(argv);


    return 0;

}

Re: Advice searching for index in a char array causing seg fault

PostPosted: Mon May 07, 2012 1:33 pm
by centip3de
LoGiCaL__ wrote:Yeah i got rid of the char array, here is updated version of whole source. Main reason for program is to translate braille to readable english. Mind you it's not fully done yet. But I got past the seg fault and a littler further.

Here is updated source:


I'm curious to the purpose of this program... As far as I'm concerned, braille is read by the fingers of blind people. Actually FEELING what they are reading. Which would make it really difficult to implement such a system, dynamically, and on the computer. Seeing as the only tactile response a computer will ever give out is the signal from your keyboard or mouse to their respective drivers...

On-topic: I would comment on your code, but after coding in C for ~2 now, I've all but forgotten anything related to OOP. Lo siento amigo.

Re: Advice searching for index in a char array causing seg fault

PostPosted: Mon May 07, 2012 2:31 pm
by ghost107
Code: Select all
while(myFile.good())
{
     getline(myFile, brailleLine, '\0');
     stringStream << brailleLine;
     tempStr += stringStream.str();        //<<<<<<<<THIS IS WHERE PROBLEM IS AT

}

I think now the problem is that stringStream appends the data from brailleLine, and appending stringStream to tempStr would increase the size of the tempstr (example: 1121231234...)

Code: Select all
while(myFile.good())
{
     getline(myFile, brailleLine, '\0');
     stringStream << brailleLine;
     tempStr += brailleLine;        //<<<<<<<<THIS WOULD BE BETTER I THINK

}

Re: Advice searching for index in a char array causing seg fault

PostPosted: Mon May 07, 2012 2:43 pm
by LoGiCaL__
centip3de wrote:Which would make it really difficult to implement such a system, dynamically, and on the computer..


That's what inspired me to write it. The actual site is helenkellersim.com . Check the source code when you get to the page.

-- Mon May 07, 2012 2:50 pm --

Updated code thus far:

Code: Select all

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

class initialization
{
    private:
        int amount, highestCount;
        std::string brailleLine, str[3];           

    public:

        void readLinesFromText(char *file[]);
        void setAmountOfLines(char *file[]);
        int returnAmount() { return amount; }
        void populateTempCharArr(std::string tempStr1);
        int returnHighestCount(){ return highestCount; }
        std::string returnStr(int index){ return str[index]; }

};

class brailleReading : public initialization
{
    private:
        char tempBrailleLetter[2][3];       
        std::string brailleLines2;

    public:
        void populateTempBrailleLetter(std::string row);

};


void initialization::setAmountOfLines(char *file[])
{
    std::string line;
    std::ifstream myFile;             
    int count = 0, iterator = 0;


    myFile.open(file[1]);


    if(myFile.is_open())
    {
        while(myFile.good())
        {
            getline(myFile, line);

            for( ; iterator < 3; iterator++)
                str[iterator] = line;

            ++count;
        }

        myFile.close();

    }
    else
        std::cout << "Unable to open file\n";


    amount = count - 1;

}


void initialization::readLinesFromText(char *file[])
{
    std::ifstream myFile;                     
    int count, iterator = 0;
    std::string tempStr, tempStr2;                           
    std::ostringstream stringStream;

    myFile.open(file[1]);


    if(myFile.is_open())
    {
        while(myFile.good())
        {
            getline(myFile, brailleLine, '\0');

            stringStream << brailleLine;

            tempStr += stringStream.str();       


        }
       
        while(iterator < tempStr.length())
        {
            if((tempStr[iterator] == ' ') || (tempStr[iterator] == '.'))
                tempStr2 += tempStr[iterator];

            ++iterator;

        }


        myFile.close();

    }
    else
        std::cout << "Unable to open file\n";

    std::cout << tempStr2;

}

int main(int argc, char *argv[])
{

    brailleReading rayCharles;

    rayCharles.setAmountOfLines(argv);
    rayCharles.readLinesFromText(argv);



    return 0;




-- Mon May 07, 2012 2:54 pm --

Thanks again for your input. I do appreciate it. :D

Re: Advice searching for index in a char array causing seg fault

PostPosted: Mon May 07, 2012 7:19 pm
by centip3de
LoGiCaL__ wrote:
centip3de wrote:Which would make it really difficult to implement such a system, dynamically, and on the computer..


That's what inspired me to write it. The actual site is helenkellersim.com . Check the source code when you get to the page.



I was trying to say if you sat a real blind person down at a computer and asked them to tell you what was on the screen, no matter how advanced the program was, they couldn't do it. The only way the could do that (with braille) is for there to be dynamically changing hardware, which is insanely hard. That was what I was trying to say.

Re: Advice searching for index in a char array causing seg fault

PostPosted: Tue May 08, 2012 12:36 pm
by LoGiCaL__
Yeah, I realize there is basically no use unless people who can see want to learn braille. However, it does seem like a good way to pass messages digitally. What I mean is that it could just be another layer in some already encrypted message.

Re: Advice searching for index in a char array causing seg fault

PostPosted: Tue May 08, 2012 1:45 pm
by centip3de
LoGiCaL__ wrote:Yeah, I realize there is basically no use unless people who can see want to learn braille. However, it does seem like a good way to pass messages digitally. What I mean is that it could just be another layer in some already encrypted message.


Possibly... But from what I understand, braille uses multiple lines to symbolize different things. For instance a square of bumps could mean one thing, while a triangle another, which would seem to make translations between the two difficult and inefficient. I honestly don't know much about braille, or encryption in all honesty, so take what I say with an extremely large grain of salt.

Re: Advice searching for index in a char array causing seg fault

PostPosted: Tue May 08, 2012 2:44 pm
by LoGiCaL__
Basically a letter number or symbol in English would be represented by a 3x2 square rectangle each square either has a '.' or a ' '. This rectangle can make either a letter or contraction of letters.

http://en.wikipedia.org/wiki/Braille

-- Fri May 25, 2012 3:50 pm --

Well, I finally got this program in working order. There are still some things I would like to work on to make it work correctly. For now though it's good. Some things I would like to work on are reducing line count some. As of right now in the .txt file it takes the braille from, at the end of each line you have to manually make sure it has one space after last char and then goes to the next line for all the lines. I want to build this into the program just in case of different copy and pastes.

Used g++. http://pastebin.com/NNMhNkUu