xCode File Reading Frustration

I code on two different systems, Windows in class, macOS at home. Basically, xCode will not read from files correctly (or at all).

I've ran this code on both systems
"bacteria.txt" is a list of indeces for the array
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
void start(bool game[][SIZE])
{
    ifstream bacteria;
    int row, colm;
    
    init (game);
    
    //open file
    bacteria.open("bacteria.txt");
    
    //test for file
    if (!bacteria)
        cout<<"File not found"<<endl;
    
    //taking indices from file
    bacteria>>row>>colm;
    
    while (bacteria)
    {
        game[row][colm]=true;
        bacteria>>row>>colm;
        
    }
    
    
    //close file
    bacteria.close();
    
}

The array gets the correct information on Windows, the array remains entirely false in xCode (it is initialized as such).
I've never gotten a file to read correctly in xCode. Is there some sort of different type of system command that is necessary to do this properly?

I (also frustrating) use a custom directory. The final DOES open, but I cannot get anything to read from it.

Also, yes, I've tried .rtf, doesn't work.

Any help would be appreciated!
Last edited on
Well since you just "report" a file opening error instead of stopping the program it is possible that you missed the message. Also your read loop is dangerous, what happens if row or colm (from the file) are equal to or larger than the SIZE of the array? You really should verify the values before you blindly try to change the array elements.

I do something more like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// You really should be passing the "row" and "column" to the function to help avoid array overflows.

// Returns 1 if the file fails to open, 0 otherwise.
int start(bool game[][SIZE])
{

    int row, colm;

    // What is this actually doing? Wouldn't it be better in the calling function?
    // Perhaps initialize the game[][] when you declare it?
    init (game);

    //open file, use the constuctor instead of the open function.
    // I would also consider opening the file in the calling function to allow
    // earlier error detection for the file failing to open. Pass an open file
    // stream to the function instead of opening the file stream here.
    ifstream bacteria("bacteria.txt");

    //test for file
    if (!bacteria)
    {
        cout<<"File not found"<<endl;
        return 1;
    }

    // ROW_SIZE and COLM_SIZE should either be passed into the function or should be global constants.
    while(bacteria >> row >> colm && row < ROW_SIZE && colm < COLM_SIZE)
    {
        game[row][colm] = true;
    }

    // Note no need to close the file, let the destructor do it's job (think RAII!).

    return 0;
}
Thanks for the formatting help, I tend to get a little hasty and messy when I'm frustrated.

Anyway, I used that code verbatim and I still have the same issue. It really is just an xCode thing...or MY xCode.

Any other ideas? I've been dealing with this all semester, it's made homework impossible.

Also, that init function just initializes the array. I personally wouldn't have put it there, but it's what the professor suggested.
Last edited on
Please post a small sample of your input file.

Also a small complete program that illustrates the problem would be helpful.

Does your program print the "File not found" message?

Have you tried printing the values that you're trying to read to the console to insure you're actually reading the values correctly?

Also have you "reformatted" the input file for your MAC. Remember the line endings will be different and this can cause problems if you try to read a file created in Windows on a MAC.

Topic archived. No new replies allowed.