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 8735571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T10:05:22+00:00 2026-06-13T10:05:22+00:00

I am trying to do the following: Read a text file containing integer data

  • 0

I am trying to do the following:
Read a text file containing integer data using a function such that the code doesn’t mess up my main function and then parsing the data to the main function.

my code looks like this

void Readata(char* fname, int* n, int* TheArray){
  int nn=0;
  int anyInt=0;

  ifstream InFile( fname );
  if ( ! InFile.is_open() ) throw myErrHandler("Could not open input file");

  InFile >> anyInt;
  if ( ! InFile ) throw myErrHandler( "Could not read size of array" );
  nn=anyInt;
  (*n)=nn;    

  int* AnArray = (int*) calloc(nn,sizeof(int));
  for(int i=0; i<nn; i++){
    InFile >> anyInt;
    if ( !InFile ) throw myErrHandler( "Could not read data" );
    AnArray[i]=anyInt;
  }
  TheArray = AnArray;
}

And the main function looks like

int main(int argc, char **argv){
  if ( argc < 2 ){
    cerr << "Usage: " << argv[0] << " input file" << endl;
    return 777;
  }

  int n;
  int* TheArray;
  ReadData(arg[1], &n, TheArray);
  return 1;
}

My problem is, that when I try to access TheArray from the main function, I get a “Segmentation fault (core dumped)” message. Obviously, I do not point to the right place with the “TheArray” pointer after the ReadData function has been called. If I print the data to the screen in the ReadData function, both AnArray and TheArray points to the same (and the right thing). What is the right way to do this?

  • 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-13T10:05:23+00:00Added an answer on June 13, 2026 at 10:05 am

    When you call ReadData the TheArray pointer is passed by value, so inside the function it behaves like any other non-pointer argument which means changes to it will not be visible outside the function. You have to pass the actual pointer as a reference (i.e. the address of the pointer) for the argument to change it:

    void Readata(char* fname, int* n, int** TheArray){
        ...
    
        *TheArray = AnArray;
    }
    

    And call it like

    Readdata(arg[1], &n, &TheArray);
    

    Edit: Using the suggestion in the comment from Pete is probably better. However, even better would be to stop using raw pointers at all, and start using std::vector.

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

Sidebar

Related Questions

I am trying to read a text file using Dynamics AX. However, the following
I am trying to read in data from a text file using numpy.loadtxt with
I'm trying to use the following code of java to read image data on
I'm trying to use urllib and urllib2 to read from a text file that
I'm trying to read a text file containing the name of the image, and
I am trying to read a UTF-8 text file in Visual C++ 2010 using
I am running the following code to try and read from a text file.
I have a text file that contains the following data. The first line is
I'm trying to read a text file built with the following format in every
I'm trying to read a text file line by line using InputStream from the

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.