Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8965513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:49:10+00:00 2026-06-15T16:49:10+00:00

I am trying to read a text file into a two dimensional character array.

  • 0

I am trying to read a text file into a two dimensional character array. When I call close on the ifstream object after extracting the data, I get a segmentation fault.

This works:

problem::problem(obj *o1, obj* o2, char *state_file)
{
ifstream infile;
string line;
infile.open(state_file, ios::in);
getline(infile,line);
infile.close();
}

This doesnt:

problem::problem(obj *o1, obj* o2, char *state_file)
{

ifstream infile;
string line;

//data is char data[6][7] and is declared in the header
//line is EXACTLY 7 characters lone

infile.open(state_file, ios::in);
for(int i = 5;i >= 0;i--)
{
    getline(infile,line);
    for(int j = 0;j < 7;j++)
        data[i][j] = line[j];
}
cerr << "PROGRAM OK" << endl;
infile.close();
cerr << "The program doesn't get here" << endl;
//Some more constructor code    

}

Why am I getting a segmentation fault when I call infile.close()?

SSCCE version that works with the same input file:

#include <string>
#include <fstream>

using namespace std;

class func
{
    public:
        func(char *);   
    private:
        char data[6][7];

};

func::func(char *state_file)
{
    ifstream infile;
    string line;

    infile.open(state_file, ios::in);   
        for(int i = 5;i >= 0;i--)
    {
        getline(infile,line);
        for(int j = 0;j < 7;j++)
            data[i][j] = line[j];
    }

    infile.close();
}

int main(int argc, char *argv[])
{
    func *obj = new func(argv[1]);  
    delete obj;
    return 0;
}

From main:

obj *p1 = new obj(&something);
obj *p2 = new obj(&something);

problem *p;

if(argc == 3)
    p = new problem(p1, p2, argv[2]); //SEGFAULTS HERE
else
    p = new problem(p1, p2);

from the header with the class declaration:

public:
    problem(obj *, obj *);
    problem(obj *, obj *, char *);
private:
        char data[6][7];
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:49:11+00:00Added an answer on June 15, 2026 at 4:49 pm

    I figured it out. The problem had something to do with how the object file was being linked during the build process. Removing all the .o files and rebuilding from scratch solved the problem. Thank you everyone for your input. I apologize for not doing a clean build from the beginning and wasting your time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to read values from a text file into a hashtable, I want
I am trying to read a text file into matlab where the text file
I am trying to read a simple text file into a String. Of course
i am trying to read jpg file and convert into byte array this is
trying to read text into memory from a text file the user selects MODEL:
Im trying to read a text file, x bytes at a time into a
I am trying to read a text file in Matlab and convert it into
I'm trying to read a text file that contains integers and store them into
I'm trying to read a text file into R so I can use the
Part 1 I'm trying to read a text file and then copy into another

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.