Advice searching for index in a char array causing seg fault

Advice searching for index in a char array causing seg fault

Post by LoGiCaL__ on Mon May 07, 2012 12:56 am
([msg=66164]see Advice searching for index in a char array causing seg fault[/msg])

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.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


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

Post by ghost107 on Mon May 07, 2012 8:37 am
([msg=66169]see Re: Advice searching for index in a char array causing seg fault[/msg])

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.
ghost107
Poster
Poster
 
Posts: 132
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


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

Post by LoGiCaL__ on Mon May 07, 2012 9:24 am
([msg=66170]see Re: Advice searching for index in a char array causing seg fault[/msg])

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;

}
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


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

Post by centip3de on Mon May 07, 2012 1:33 pm
([msg=66179]see Re: Advice searching for index in a char array causing seg fault[/msg])

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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1430
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


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

Post by ghost107 on Mon May 07, 2012 2:31 pm
([msg=66182]see Re: Advice searching for index in a char array causing seg fault[/msg])

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

}
ghost107
Poster
Poster
 
Posts: 132
Joined: Wed Jul 02, 2008 7:57 am
Blog: View Blog (0)


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

Post by LoGiCaL__ on Mon May 07, 2012 2:43 pm
([msg=66183]see Re: Advice searching for index in a char array causing seg fault[/msg])

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
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


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

Post by centip3de on Mon May 07, 2012 7:19 pm
([msg=66188]see Re: Advice searching for index in a char array causing seg fault[/msg])

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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1430
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


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

Post by LoGiCaL__ on Tue May 08, 2012 12:36 pm
([msg=66205]see Re: Advice searching for index in a char array causing seg fault[/msg])

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.
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)


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

Post by centip3de on Tue May 08, 2012 1:45 pm
([msg=66209]see Re: Advice searching for index in a char array causing seg fault[/msg])

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.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. -Rick Cook
User avatar
centip3de
Moderator
Moderator
 
Posts: 1430
Joined: Fri Aug 20, 2010 5:46 pm
Blog: View Blog (0)


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

Post by LoGiCaL__ on Tue May 08, 2012 2:44 pm
([msg=66210]see Re: Advice searching for index in a char array causing seg fault[/msg])

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
User avatar
LoGiCaL__
Addict
Addict
 
Posts: 1061
Joined: Sun May 30, 2010 12:33 pm
Blog: View Blog (0)



Return to C and C++

Who is online

Users browsing this forum: No registered users and 0 guests