Xcode and Files

I am confused about the inability for this code to work. All others that have used visual studio have gotten this to function, however I am trapped in an Xcode universe (which I am discovering is the programming equivalent of being trapped inside a laundry bag of used jock straps)

I put the code down below, but cannot go any further since the ability for Xcode to even open a file doesn't seem to work.

I cannot include screenshots, but I have tried placing the files in the File folder, the Product folder, copies of both in both folders, and each time it fails.

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
#include <iostream>
#include <fstream>
using namespace std;

// Prototypes
void openFile(ifstream &infile, string filename);
void openFile(ofstream &outfile, string filename);
double calcAverage(double total, int count);

int main()
{
    ifstream infile;
    ofstream outfile;
    string inputFilename;
    string outputFilename;
    
    //Ask user name of file open
    cout << "Name of file to open" <<endl;
    cin >> inputFilename;
    cout << endl;
    cout << "Name of file to write to" << endl;
    cin >> outputFilename;
    
    openFile(infile, inputFilename);
    
    
    return 0;
}

//Test ifconnection
void openFile (ifstream &infile, string filename)
{
    infile.open(filename);
    if (infile.is_open() == 0)
    {
        cerr << filename<< " did not open";
        exit(1);
    }
}

//Test ofconnection
void openFile (ofstream &outfile, string filename)
{
    outfile.open(filename);
    if (outfile.is_open() == 0)
    {
        cerr << filename<< " did not open";
        exit(1);
    }
}

//Do some math
double calcAverage(double total, int count)
{
    double avg;
    avg = total / count;
    return avg;
}
There are a number of possibilities.

(1) Your compiler (like mine) may be set to compile the original C++ standard (C++98). Opening files requires a C-string, not the newer string. See if changing line 33 to
infile.open(filename.c_str());
and line 44 to
outfile.open(filename.c_str());
works. (The member function c_str() converts a string to the older null-terminated character string type - look it up.)

(2) I had to add
#include <cstdlib>
amongst the headers in order to use exit(1).

(3) I assume that whatever file you are trying to read from should be in the same folder/directory as your source code. Check this.


Visual studio and Xcode are unnecessarily complex suites of software for learning. I should use g++ (from MinGW), run from the command line. Then at least you know where all your files are.

Please provide feedback as to whether any of this works. I had no problems running your program once I had made changes (1) and (2) above, and adding some input/output lines to check that I was reading correctly.
I still could not get the files to open.

testscores = x / report = y
I tested it the following ways (my best as the visual representation of the left column):

File:
main.cpp
x.txt
y.txt

Products
File

---AND---

File:
main.cpp

Products
File
x.txt
y.txt

I attempted to open the files as x/x.txt/ and y/y.txt

Every time I got the same error message that <filename> would not open. I also tried to make a two variations of the file in a .txt format and a .rtf format.
If the input file is called x.txt then reply to the prompt as x.txt
I assume this is just a simple text file (that you have already created outside of your program).

Nothing wrong with your C++ code. Open Windows explorer and check that main.cpp (presumably the name of your source code?) and x.txt are in the same folder. Use the notepad text editor to check that they are the files you require. Your output file is irrelevant, since it is to be created by the program.

I have no idea what you mean by File: and Products:, but then I don't use Xcode.

Topic archived. No new replies allowed.