Xcode can't read the txt file

Im typing a code from my textbook just to test my Xcode. i put the txt file in right path and nothing wrong with code, but when i hit run i see this
The numbers are : 0 0 0 0 1606416384
Program ended with exit code: 0
and i put 10 20 30 40 50 in the txt file .

any one can help me with this problem
.


#include <iostream>
#include <fstream>

using namespace std;
int main()
{
const int ARRAY_SIZE = 5;
int numbers[ ARRAY_SIZE];
int count = 0;
ifstream inputFile;
inputFile.open("FiveNumbers.txt");
while (count < ARRAY_SIZE && inputFile >> numbers [ count])
count++;
inputFile.close();

cout<< "The numbers are : ";
for (count = 0; count < ARRAY_SIZE; count++)
cout << numbers[count]<< " ";
cout<< endl;
return 0;
}

Most likely the file failed to open.

1. after the file.open statement, check whether or not it succeeded.
1
2
3
4
5
6
    ifstream inputFile("FiveNumbers.txt");
    if (!inputFile)
    {
        cout << "error opening input file\n";
        return 1;
    }


2. rather than printing the whole array, use the value of count as the loop limit.
1
2
3
4
    cout<< "The numbers are : ";
    
    for (int i = 0; i < count; i++)
        cout << numbers[i] << " ";
Still the same problem
#include <iostream>
#include <fstream>

using namespace std;
int main()
{
const int ARRAY_SIZE = 5;
int numbers[ ARRAY_SIZE];
int count = 0;
ifstream inputFile;
inputFile.open("FiveNumbers.txt");
if (!inputFile)
{
cout << "error opening input file\n";
return 1;
}
while (count < ARRAY_SIZE && inputFile >> numbers [ count])
count++;
inputFile.close();

cout<< "The numbers are : ";
for (int i = 0; i < count; i++)
cout << numbers[i] << " ";

cout<< endl;
return 0;
}

Still the same problem

Really? Maybe the code needs to be recompiled/rebuilt. I'd expect either the error message or the correct output now.

I get this:
The numbers are : 10 20 30 40 50


Same program:
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
#include <iostream>
#include <fstream>

using namespace std;
int main()
{
    const int ARRAY_SIZE = 5;
    int numbers[ARRAY_SIZE];
    int count = 0;
    ifstream inputFile("FiveNumbers.txt");
    if (!inputFile)
    {
        cout << "error opening input file\n";
        return 1;
    }
    
    while (count < ARRAY_SIZE && inputFile >> numbers [ count])
        count++;
        
    inputFile.close();
    
    cout<< "The numbers are : ";
    for (int i = 0; i < count; i++)
        cout << numbers[i] << " ";    
               
    cout<< endl;
    
    return 0;
}

I guess the problem is the file and the path
because i got this



The numbers are :
Program ended with exit code: 0



the result came without numbers.
ANY HELAP PLEASE < IM STOCK AND DONT UNDERSTAND AT ALL
closed account (48T7M4Gy)
If the file is actually being written then use Finder to locate it. From there you can either use it there and/or add add that directory to the Xcode directories. Somewhere in the Xcode preferences you can setup directories properly but if I'm right at least the bad cycle is broken. :)

PS I just tried you program (the latest post of yours) using Xcode and it works perfectly
The numbers are : 10 20 30 40 50 
Program ended with exit code: 0
Last edited on
I guess the problem is the file and the path
because i got this
The numbers are :
Program ended with exit code: 0

the result came without numbers.

From that, it would appear that the file exists and was opened successfully, but there was something wrong with whatever was in the file, because it the data wasn't able to be read.

If in doubt, you need to get more information about what is happening. One way is to use a debugger. But perhaps that is unnecessary here, you might instead add extra cout messages. Here, I've taken the code from the above post, http://www.cplusplus.com/forum/general/196482/#msg943493 and added some more messages:
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
#include <iostream>
#include <fstream>

using namespace std;
int main()
{
    const int ARRAY_SIZE = 5;
    int numbers[ ARRAY_SIZE];
    int count = 0;
    cout << "1. before opening file\n";
    ifstream inputFile;
    inputFile.open("FiveNumbers.txt");
    if (!inputFile)
    {
        cout << "error opening input file\n";
        return 1;
    }
    cout << "2. after opening file\n";    
    
    cout << "3. before reading file, count = " << count << '\n';
    while (count < ARRAY_SIZE && inputFile >> numbers [ count])
        count++;
    inputFile.close();
    cout << "4. after reading file, count = " << count << '\n';    

    cout<< "The numbers are : ";
    for (int i = 0; i < count; i++)
        cout << numbers[i] << " ";

    cout<< endl;
    cout << "5. Program ending" << endl;    
    return 0;
}

and this is the result which I get:
1. before opening file
2. after opening file
3. before reading file, count = 0
4. after reading file, count = 5
The numbers are : 10 20 30 40 50
5. Program ending

The one final piece of information, the contents of the file, this is what is in my file:
10 20 30 40 50

closed account (48T7M4Gy)
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
#include <iostream>
#include <fstream>

using namespace std;
int main()
{
    const int ARRAY_SIZE = 5;
    int numbers[ ARRAY_SIZE];
    int count = 0;
    
    cout << "Let's create the data file\n";
    ofstream makefile("FiveNumbers.txt");
    for(int i = 10; i <= 50; i+= 10)
        makefile << i << ' ';
    makefile.close();
    
    cout << "1. before opening file\n";
    ifstream inputFile;
    inputFile.open("FiveNumbers.txt");
    if (!inputFile)
    {
        cout << "error opening input file\n";
        return 1;
    }
    cout << "2. after opening file\n";
    
    cout << "3. before reading file, count = " << count << '\n';
    while (count < ARRAY_SIZE && inputFile >> numbers [ count])
        count++;
    inputFile.close();
    cout << "4. after reading file, count = " << count << '\n';
    
    cout<< "The numbers are : ";
    for (int i = 0; i < count; i++)
        cout << numbers[i] << " ";
    
    cout<< endl;
    cout << "5. Program ending" << endl;
    return 0;
}


I've just made a rough and ready addition to the above which might help also. It runs on my Xcode with the following output. So, what it means is delete all or any copies of FiveNumbers.txt and then see where it appears when the program runs.

Let's create the data file
1. before opening file
2. after opening file
3. before reading file, count = 0
4. after reading file, count = 5
The numbers are : 10 20 30 40 50
5. Program ending
Program ended with exit code: 0


Also:
In Xcode
1. Click on the project
2. Top menu: Product -> Scheme -> Edit Scheme -> Run debug (LH panel)
3. Working directory -> hit select custom and enter the directory details you want or hit the icon on the right of that textbox to select from directory view.

This all works for Mac OSX & Xcode latest, sorry Chervil to cut across :)

Last edited on
This all works for Mac OSX & Xcode latest, sorry Chervil to cut across :)

You're welcome. I don't have access to a Mac so the additional input is essential.
Topic archived. No new replies allowed.