Jump to content

Segmentation fault problem

- - - - -

  • Please log in to reply
3 replies to this topic

#1
Black Wolf

Black Wolf

    Newbie

  • Members
  • Pip
  • 6 posts
Alright so I am having this problem with segment fault with my code it happens inside the loop but I just can not figure out why it keeps happening


#include <iostream>

#include <fstream>

using namespace std;


void format_print (const char text [], int text_len, int line_len);


void load_text (char text[],int& charcount, bool& eof);

int main()


{

    const int buffersize = 250;

    char text[buffersize];

    int charcount;

    bool eof;

    int print_length;

    int text_length;


    print_length = 35;

    text_length = 1000000;

    charcount = 0;


    load_text(text,charcount,eof);


    return 0;



}


void load_text(char text[], int& charcount, bool& eof)

{



    int index;

    ifstream selectedfile;

    char filename[51];

    eof = false;

    index = 0;


    cout << "Please enter a file name: ";

    cin >> filename;



    selectedfile.open(filename);



    if (selectedfile.fail())

    {

        cout << "The file has failed to Open!\n";

    }

else

 {


        while (!eof)

        {


            selectedfile.get(text[index]);

            index ++;

            charcount ++;


            cout << "It's working!"


            if (selectedfile.eof())

            {

                eof = true;

            }

            else

                eof = false;

        }


        selectedfile.close();

    }


}


void format_print (const char text [], int text_len, int line_len)

{

    int index;


    for (index = 0; index < text_len; index++)

    {

        if (index != 0 && index % line_len == 0)

            cout << endl;


        cout << text[index];

    }

    cout << endl;

    return;

}



#2
Black Wolf

Black Wolf

    Newbie

  • Members
  • Pip
  • 6 posts
Errr.... nevermind >.> when I removed that last if statment towards the end it started working

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
First there were quite a few syntax errors, you never ended the load_text() function and also it appears an ELSE had no opening brace as well.

This appears to say "It's working! It's working!" (twice) when I enter a valid file:
#include <iostream>
#include <fstream>
using namespace std;

void format_print (const char text [], int text_len, int line_len);

void load_text (char text[],int& charcount, bool& eof);
int main()

{
    const int buffersize = 250;
    char text[buffersize];
    int charcount;
    bool eof;
    int print_length;
    int text_length;

    print_length = 35;
    text_length = 1000000;
    charcount = 0;

    load_text(text,charcount,eof);

    return 0;


}

void load_text(char text[], int& charcount, bool& eof)
{


    int index;
    ifstream selectedfile;
    char filename[51];
    eof = false;
    index = 0;

    cout << "Please enter a file name: ";
    cin >> filename;


    selectedfile.open(filename);


    if (selectedfile.fail())
    {
        cout << "The file has failed to Open!\n";
    }
else
 {

        while (!eof)
        {

            selectedfile.get(text[index]);
            index ++;
            charcount ++;

            cout << "It's working!";

            if (selectedfile.eof())
            {
                eof = true;
            }
            else {
                eof = false;
        }

        selectedfile.close();
    }

  }
}

void format_print (const char text [], int text_len, int line_len){
    int index;

    for (index = 0; index < text_len; index++)
    {
        if (index != 0 && index % line_len == 0)
            cout << endl;

        cout << text[index];
    }
    cout << endl;
    return;
}

If you were experiencing a segmentation fault instead of these syntax errors though, you may not be defining a large enough buffer. If your loop happened to access memory beyond the buffer it would cause a segmentation fault.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#4
Black Wolf

Black Wolf

    Newbie

  • Members
  • Pip
  • 6 posts
Yeah the problem ended up being with that final if statment for the pass by reference boolean it didn't like being in the loop very much so when I took it out, it actually worked hence the it works it works it works over and over again as it pumping in character after character to the array




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users