C++ File Input in Xcode

In my programming class, we're learning file input in C++ and I can't get it to work in Xcode. The code works perfectly on the PC's running Visual Studio in the class but I don't know how to get it to work on my macbook. I tried putting the file in the same directory as the source but it didn't work. Here's my code, how do I get the program to use the information from a file called "Student.txt".

#include <stdio.h>
#include <string.h>
struct student
{
char studentID [4+1];
char firstname [25+1];
char lastname [25+1];
int grade;
char dob [10+1];
};
struct course
{
char code [5+1];
char name [50+1];
int grade;
float credit;
};
int main (void)
{
student people[4];
int x;
x=0;
FILE *fp;
fp=fopen ("Student.txt", "r");
if (fp!=NULL)
{
printf("Got here");
while (x<5)
{
printf ("hi");
fscanf (fp, "%s, %s, %s, %d, %s", people[x].studentID, people[x].firstname, people[x].lastname, people[x].grade, people[x].dob);
printf("ID: %s, Name: %s, %s, Grade: %d, DOB: %s", people[x].studentID, people[x].lastname, people[x].firstname, people[x].grade, people[x].dob);
x++;
}

fclose (fp);
}
}

2 GHz Intel Macbook White, Mac OS X (10.5.3)

Posted on May 30, 2008 10:13 AM

Reply
6 replies

May 30, 2008 10:55 AM in response to RonenA

Xcode is intended more for full-fledged applications. The expectation is that there will always be a file dialog to select a file. It is very hard to tell when running an application from Xcode or the Finder just what the current working directory is. It is never going to be the directory where the source code is located.

The easiest thing is you put your data in /tmp or or /data and hard-code the full path to it.

There are numerous other options as well. You can specify the working directory in Xcode. You can build the code using g++ instead of Xcode.

This thread has been closed by the system or the community team. You may vote for any posts you find helpful, or search the Community for additional answers.

C++ File Input in Xcode

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple Account.