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;
}
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
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:
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
constint 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:
#include <iostream>
#include <fstream>
usingnamespace std;
int main()
{
constint 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 :)