0

It would appear that I am encountering an unexpected overflow and I do not understand why this is occurring (much to my dismay):

Windows:

enter image description here

Mac:

enter image description here

These were both compiled using MinGW. I believe that this may be due to the difference in file types between BSD and Windows as I note that if I copy numbers.txt from a Windows OS I recieve a similar fault.

Mac error with windows file:

Mac Shell: Downloads/>$ rm numbers.txt 
# Downloaded new numbers.txt file from windows.
Mac Shell: Downloads/>$ ./Project1 

Raw output:
-----------
Segmentation fault: 11
Mac Shell: Downloads/>$

Windows with Mac file:

enter image description here

Code:

void do_file_magic(string file){
    string line, replace = "", numeral, dump = "", temp;
    ifstream source ("numbers.txt");
    if (!source.is_open()) {
        die_a_clean_death("Unable to open source file:", file);
    };

    // Display output to console:
    sep("Raw output:");

    // read source file and output vowels to destination file.
    while ( getline(source, line) || true) {
        dump = dump + replace;
        if ( line[0] != ' ') {
            temp = num2string(roman2num(line));
            line = remove_whitespace(line);
            replace = line + string(17-line.size(), ' ') + temp + "\n";

        // If you read this then you noticed that there was some shenagins going on.
        } else {
            numeral = num2roman(string2num(line));
            line = remove_whitespace(line);
            replace = numeral + string(17 - numeral.size(), ' ') + line + "\n";
        };
        if(source.eof()) {
            break;
        }
    };
    printf("file dump: \n%s", dump.c_str());


    // Close the files
    source.close();

    // open the file in output mode nuking everything from orbit.
    // (Its the only way to be sure)
    ofstream destination("numbers.txt");
    if (!destination.is_open()) {
        die_a_clean_death("Unable to open destination file:", file);
    };

    // Just dump a variable to the file.
    destination << dump << "\n";

    destination.close();
};

Could someone help me to understand where I am going wrong here?

Robert J
  • 513
  • 6
  • 17
  • 2
    The right tool to solve such problems is your debugger. You should step through your code line-by-line *before* asking on Stack Overflow. For more help, please read [How to debug small programs (by Eric Lippert)](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). At a minimum, you should \[edit] your question to include a [Minimal, Complete, and Verifiable](http://stackoverflow.com/help/mcve) example that reproduces your problem, along with the observations you made in the debugger. – πάντα ῥεῖ Sep 28 '16 at 17:05
  • Most probably undefined behavior somewhere (unitinialized variables?). – πάντα ῥεῖ Sep 28 '16 at 17:06
  • 1
    Why do you have "`|| true`" in your `while` expression? – Thomas Matthews Sep 28 '16 at 17:48
  • I was told that `while( Contition1 && !somefile.eof() )` would always be a bug. I reviewed the logic for behind this at the time and it did seem sound. http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong hence me trying a `while true` with a `break` condition. I did attempt a debug with codeblocks on Windows 10 but did not seem to be able to run the debug. This is really my first time using codeblocks (I usually `vim` a file and rely on my syntax checker and `make` files in a bash shell). – Robert J Sep 28 '16 at 19:09

1 Answers1

2

There may be more problems, but the first one I spotted is:

while ( getline(source, line) || true) {
    ...
    if ( line[0] != ' ') ...

If the call to getline fails, line will be empty, so line[0] is out-of-bounds.

Adrian McCarthy
  • 41,073
  • 12
  • 108
  • 157