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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T14:32:30+00:00 2026-05-14T14:32:30+00:00

When trying to read tokens from a file in C++, I receive a seg

  • 0

When trying to read tokens from a file in C++, I receive a seg fault. Just to play with some things, I tried reading over the file and just printing and surprisingly if you uncomment the code for the first file read and then read the second file everything works fine. If you leave the commented code commented, you receive the seg fault upon closing the first file. This could be an issue with the library on my school machine… The stack trace is below for the segfault (below the code itself). It’s also interesting to note that I do not have to do this ‘hack’ for each file I open, once the dummy-commented-code has been executed all subsequent streams open just fine.

int main()
{
ifstream myfile1;
ifstream myfile2;

int m; //number of elements in 1st file
int n; //number of elements in 2nd file
int counter = 0;

int x = 0;
int y = 0;
int theta = 0;

Minutiae* minutiae;
minutiae = new Minutiae(x,y,theta,0);

Minutiae* file1_minutiaes;
Minutiae* file2_minutiaes;

int num;

//Some Error is caused with segfault if i Try to fill the minutiae array and then close the file unless i do this first
/*////////////////////
  myfile1.open("2a");


  if (myfile1.is_open())
  {

    counter = 0;

    while (!myfile1.eof() )
    {

     myfile1 >> x >> y >> theta;
     minutiae = new Minutiae(x,y,theta,counter);
     counter++;
    }

    myfile1.close();

  }
  else
  {
  cout << "unable to open file1" << endl;
  return(0);
  }
*/////////////////////////////////////




  myfile1.open("2a");

  if (myfile1.is_open())
  {
    counter = 0;
    myfile1 >> m;

    file1_minutiaes = new Minutiae[m];

    while (!myfile1.eof() )
    {

     myfile1 >> x >> y >> theta;

     //minutiae = new Minutiae(x,y,theta,counter);
     //file1_minutiaes[counter] = *minutiae;
     file1_minutiaes[counter] = *(new Minutiae(x,y,theta,counter));
     counter++;

    }
    myfile1.close();
    cout << "closing file1" << endl;

  }
  else
  {
  cout << "unable to open file1" << endl;
  return(0);
  }

////////////////////////////////
}

And the stack trace:

Starting program: /cs/student/dick_man_chini/Desktop/a.out 

Program received signal SIGSEGV, Segmentation fault.
0x00c47a72 in _int_free () from /lib/libc.so.6
(gdb) up
#1  0x006d9342 in operator delete(void*) () from /usr/lib/libstdc++.so.6
(gdb) up
#2  0x006d939e in operator delete[](void*) () from /usr/lib/libstdc++.so.6
(gdb) up
#3  0x00686130 in std::basic_filebuf<char, std::char_traits<char> >::_M_destroy_internal_buffer() () from /usr/lib/libstdc++.so.6
(gdb) up
#4  0x006874d1 in std::basic_filebuf<char, std::char_traits<char> >::close() () from /usr/lib/libstdc++.so.6
(gdb) up
#5  0x006894a6 in std::basic_ifstream<char, std::char_traits<char> >::close() () from /usr/lib/libstdc++.so.6
(gdb) up
#6  0x08048e96 in main ()
(gdb) up
Initial frame selected; you cannot go up.
(gdb) 

Thanks in advance.

  • 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-05-14T14:32:30+00:00Added an answer on May 14, 2026 at 2:32 pm

    The common process for reading files:

    myfile1.open("2a");
    // Note the change in the next line
    if (!myfile1)
    {
        cout << "unable to open file1" << endl;
        return(0);
    }
    
    counter = 0;
    
    // Note, this line differs between the two examples.
    myfile1 >> m;
    
    file1_minutiaes = new Minutiae[m];
    
    // Note the change in the following line
    while (myfile1 >> x >> y >> theta)
    {
        //minutiae = new Minutiae(x,y,theta,counter);
        //file1_minutiaes[counter] = *minutiae;
        file1_minutiaes[counter] = *(new Minutiae(x,y,theta,counter));
        counter++;
    }
    myfile1.close();
    cout << "closing file1" << endl;
    

    I suggest you use a std::vector instead of an array:

      std::vector<Minutiae> file1_minutiaes;
    

    When processing data files, a good idea is to use dynamic containers.

    • 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 some XML code from a website, and am having
I am trying to read from a file and encrypt the data using AES
i'm trying to read long types from a text file with using readLine() method
I am Trying to read data from text files and save tokens in SQLite
I am trying to check if a value I read from a text file
I am trying to read data from a file, tokenize it and sort it,
I am trying to read from a text file and tokenize the input. I
Trying to read headers for a csv file with: reader = csv.DictReader(open(PATH_FILE),skipinitialspace=True) headers =
Im trying to read a column of String values from my DB. Im trying
Iam trying to read a binary file to memory and pass the starting address

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.