-sigh- My mac is acting oddly and no one knows why

My professor gave us some code to rewrite using cctype and string functions. When I run the code on a PC, it reads the input file and puts out an output just fine. On my mac, it simply says it cannot open the file
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <fstream>
#include <cstring>
#include <cctype>
#include <cstdlib>
using namespace std;

int main()
{
    const char filename[] = "data.txt";
    char buffer[16], winner[32], loser[32];
    int winnerScore, loserScore;
    
    ifstream fin(filename);
    if (!fin)
    {
        cerr << "Unable to open file " << filename << endl;
        exit(1);
    }
    
    while (!fin.eof())
    {
        // get winner
        fin >> winner;
        if (fin.eof()) break;
        
        // get next token.  Could be score or 2nd part of winner name
        fin >> buffer;
        
        // If buffer has a comma, then it's a score
        if (strchr(buffer,','))
        {
            winnerScore = atoi(strtok(buffer,","));
        }
        else
        {
            strcat(winner," ");
            strcat(winner,buffer);
            fin >> buffer;
            winnerScore = atoi(strtok(buffer,","));
        }
        
        // get loser
        fin >> loser;
        
        // get next token. Could be score or 2nd part of loser name
        fin >> buffer;
        
        // If first digit is numeric, then it's a score
        if (isdigit(buffer[0]))
        {
            loserScore = atoi(buffer);
        }
        else
        {
            strcat(loser," ");
            strcat(loser,buffer);
            fin >> buffer;
            loserScore = atoi(buffer);
        }
        cout << winner << " over " << loser << ' ' << winnerScore << " to " << loserScore << endl;
    }
}


This is the contents of the text file I'm trying to read.

Cincinnati 27, Buffalo 24
Detroit 31, Cleveland 17
Kansas City 24, Oakland 7
Carolina 35, Minnesota 10
Pittsburgh 19, NY Jets 6
Philadelphia 31, Tampa Bay 20
Green Bay 19, Baltimore 17
St. Louis 38, Houston 13
Denver 35, Jacksonville 19
Seattle 20, Tennessee 13
New England 30, New Orleans 27
San Francisco 32, Arizona 20
Dallas 31, Washington 16
Last edited on
I would check to make sure that the file is in the working directory of program (which would be why it can't find it). Perhaps on your Mac it is different than your PC.
If it can't open the file the most likely reason is because it's looking in the wrong path (or your file is in the wrong path).

If you are running the program from an IDE, look at your project settings to see what the current working directory is. If you are running the program through the file manager (like actually double-clicking it), then the current directory will be whatever directory the exe is in.

If worse comes to worse... you can have your program create a new file with a funny name (like "buttcheese" or something), then look around for a file with that name to see which folder your program is looking.


Bottom line is -- find where the program is looking, and put your file in that directory.
Topic archived. No new replies allowed.