Ccode C++ input file

Joined
Nov 12, 2009
Messages
1
Reaction score
0
Points
1
Question why is this not working?

//***********************************************
// Input
//
// Noll Roberts 2009-11-12
/*
*/
//************************************************
#include <iostream> // endl cout
#include <iomanip> // setw
#include <fstream> //
#include <string> // string variables
using namespace std;

int main (int argc, char * const argv[]) {
//Declarations
float a;
float b;
float c;

// Input
ifstream inData;
inData.open("data.dat");
if (!inData)
{
cout << "Can't open input file." << endl;
return 1;
}

inData >> a;
inData >> b;
inData >> c;

return 0;
}

data file
19.5 18.7 19

I added the data file to my Xcode project

error message
/Users/nollroberts/Xcode/indata/build/Release/indata ; exit;
noll-roberts-imac-2:~ nollroberts$ /Users/nollroberts/Xcode/indata/build/Release/indata ; exit;
Can't open input file.
logout

[Process completed]


Question
Why is this not working?
 
Joined
May 2, 2009
Messages
480
Reaction score
13
Points
18
Your Mac's Specs
MBP 2.33 4GB: MacPro 8 Core 2.8, 16GB: MacMini 2.26 4GB: MacMin 2.53 4GB: iPhone3GS 32GB
Hi, welcome to the forums.

Where does 'data.dat' live? I was able to compile and run your code, however, you need to give an absolute path for your data file. Something like:

inData.open("/Users/blah/data.dat");
 

Raz0rEdge

Well-known member
Staff member
Moderator
Joined
Jul 17, 2009
Messages
15,770
Reaction score
2,108
Points
113
Location
MA
Your Mac's Specs
2022 Mac Studio M1 Max, 2023 M2 MBA
Is the file data.dat located in the same directory as the executable? A "ls" in that directory will clear that up..

Regards
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top