weird output in linux (works fine in windows)

i really have no clue what's going on here. at the very beginning of my main() i have these two function calls
1
2
3
    populateWorld(FILE_NAME);

    showWorld();


here's the code for the two functions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
void populateWorld(string file) {
    ifstream fin(file.c_str());    //.c_str() because constructor needs c-string

    //make sure the file opened
    if (fin.fail()) {
        cout << "The file was not found.nPress Enter to quit.";
        getchar();
        exit(0);
    }

    //determine the dimensions of the world and create new two-dimensional array
    getRow(fin);
    getColumn(fin);
    allocateMemory();

    //fill the world with the contents of the file
    for(int i = 0; i < numRows; i++) {
        for(int j = 0; j < numColumns; j++) {
            world[i][j].status = fin.get();
        }
        fin.ignore();   //ignore the newline character
    }
    fin.close();
}

1
2
3
4
5
6
7
8
9
void showWorld() {
    for(int i = 0; i < numRows; i++) {
        for(int j = 0; j < numColumns; j++) {
            cout << world[i][j].status;
        }
        cout << endl;
    }
    getchar();
}



here's the includes that i have:
1
2
3
4
5
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <limits> 


here's what the very first output looks like in windows 7 (the way it should look):
1
2
3
4
5
00000000100000001010
00000000010000001001
11100000010100000010
10100100101010101010
00101010010010101000


and here's what it looks like in ubuntu 10.10:
1
2
3
4
5
6
7
8
9
00000000100000001010

00000000010000001001

1110000001010000001
0
101001001010101010
10
00101010010010101000


the weird thing is that right after these two function calls, main enters a loop that calls showWorld() at the end of the loop and the output is what's expected every time after the first. any ideas?
Last edited on
okay, never mind. the input file was created in windows. i created a new one with gedit and it worked except the program thought there was one more line in the file than there actually was, so i had a line of garbage.

i have a feeling this has something to do with the difference between windows and unix newline characters, but does anyone have an explanation? i'm curious

also, any reason why the code thinks there one more line than there is in linux, but not in windows? i know how to fix it, but it's weird
Last edited on
Topic archived. No new replies allowed.