Can't find the cause of a segmentation fault
Our lab for class today was this simple program, but I got a segmentation fault somewhere. After trying everything listed below, neither I nor my TA could figure it out! Any ideas?
Code:
//Lab Exercise 3
//lab3.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
void selection_sort(int[], int);
int main()
{
ifstream infile;
int numbers[100];
char in_file_name[16];
cout << "Integer Sort\n"
<< "------------\n" //seg fault 1
<< "Please enter the filename to sort: ";
cin >> in_file_name;
//seg fault 2
cout << in_file_name;
cout << "opening file..."<<endl;
infile.open(in_file_name);
cout << "opening file...";
if (infile.fail())
{
cout << "Error opening file. Please try again.\n";
exit (1);
}
cout<<"Adios";
int count = 0;
cout << "Inputting...";
while ((infile >> numbers[count]) && (count < 50))
++count;
cout << "Sorting...";
selection_sort(numbers, count);
ofstream outfile;
outfile.open("sorted.txt");
for (int i = 0; i < count; ++i)
outfile << numbers[[i];
cout << "The sorted numbers have been written to sorted.txt."
<< "Have a nice day!\n"<<endl;
infile.close();
outfile.close();
return 0;
}
Please forgive the odd indenting. The unindented cout statements are for debugging only. I don't indent them so they are easy to remove later.
//seg fault 1 happened when I hardcoded the input filename, like:
Code:
istream infile;
infile.open("int.txt");
instead of asking the user for input.
The file as is seg faults at //seg fault 2.
Now comes the really weird part. If I input a file that does not exist, it acts properly. That is, it outputs the error message as described and exits the program. It does not give me a segmentation fault at all.
Weird #2 - When the filename is hardcoded, and I combine the three separate string literals when asking for the filename to look like:
Code:
cout << "Integer Sort\n------------\nPlease enter blah blah blah";
it still seg faults at //seg fault 1, right in the middle of outputting a string literal! The output is:
Code:
Integer Sort
------------
<segmentation fault> core dumped <--this isn't the exact message, but you get the idea
Weird #3 - If I add endl's to a cout statement, I can make it to the next cout before it crashes. So if I add endls to the request for input, outputting in_file_name, and the lines
Code:
cout << "Opening file...";
and it will run that far, but crash somewhere before
Code:
cout << "Inputting...";
and if I add and endl to that line, I make it to
Code:
cout << "Sorting...";
before I crash.
In my search of the board I found references to permission, which shouldn't be the case (I think), since a) no one else in the class experienced this, and b) I can view the file with "more int.txt" from my directory. I am working in UNIX, of which I have almost no knowledge. I don't think I'm accessing parts of my arrays I shouldn't be, and even if I were I don't imagine it'd give me this kind of behavior. FYI, this is not for credit anymore since the lab is over, so I don't think it falls under the homework clause. I removed the selection sort function to shorten this already-too-long post. I can post that, too, if need be.