Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

learn C on the mac file issue.

Hi, I am just a beginners and I was studying my first book for learn C : "Learn C on the mac" by Dave Mark. It was very good until I arrived at the file chapter where my first project doesn't open the file ( in this case My Data File) and show me in the output all the printf (That should't appeare if main read my file) . I get stack with this project:


file://localhost/Users/matteotortora/Downloads/Learn%20C%20Projects%20(Xcode%204 -1.x)/10.01%20-%20printFile.zip


I wanna post the main.c directly so it easier for you to help me:


//

// main.c

// printFile

//

// Created by Dave Mark on 7/4/11.

// Copyright 2011 Dave Mark. All rights reserved.

//


#include <stdio.h>


int main (int argc, const char * argv[]) {

FILE *fp;

int c;


fp = fopen( "../../../../../My Data File", "r" );


if ( NULL == fp ) {

printf( "Error opening ./My Data File\n\n" );

printf( "The file My Data File should be in the same folder as the project file.\n" );

printf( "The executable is buried within the DerivedData folder, five levels deep,\n" );

printf( "which is why there are five .. in the file path above.\n" );

printf( "To learn more about where Xcode builds your executables, go to Xcode\'s\n" );

printf( "Preferences and select the Locations tab.\n" );

} else {

while ( (c = fgetc( fp )) != EOF )

putchar( c );


fclose( fp );

}



return0;

}


I know that there is already one post like mine but I din't find the right answer for me.

I will be really greatfull if you will help me because I would like finish my first book and I am preatty close to do that.

Thank you

Posted on Feb 19, 2013 7:06 PM

Reply
7 replies

Feb 19, 2013 7:33 PM in response to mtortora

What version of Xcode are you running? Things have changed as far as the layout of the project and I beleive the authors expectation of where the data file will be in relation to the executable is wrong for the latest version of Xcode.


If you move the data file to your home folder and then change this


fp = fopen( "../../../../../My Data File", "r" );


to

fp = fopen( "/Users/user/My Data File", "r" );

the code will work.

Feb 19, 2013 8:18 PM in response to Frank Caggiano

i changed it now:


//

// main.c

// printFile

//

// Created by Dave Mark on 7/4/11.

// Copyright 2011 Dave Mark. All rights reserved.

//


#include <stdio.h>


int main (int argc, const char * argv[]) {

FILE *fp;

int c;


fp = fopen( "/Users/matteotortora/My Data File", "r" );


if ( NULL == fp ) {

printf( "Error opening ./My Data File\n\n" );

printf( "The file My Data File should be in the same folder as the project file.\n" );

printf( "The executable is buried within the DerivedData folder, five levels deep,\n" );

printf( "which is why there are five .. in the file path above.\n" );

printf( "To learn more about where Xcode builds your executables, go to Xcode\'s\n" );

printf( "Preferences and select the Locations tab.\n" );

} else {

while ( (c = fgetc( fp )) != EOF )

putchar( c );


fclose( fp );

}


but still:


Error opening ./My Data File


The file My Data File should be in the same folder as the project file.

The executable is buried within the DerivedData folder, five levels deep,

which is why there are five .. in the file path above.

To learn more about where Xcode builds your executables, go to Xcode's

Preferences and select the Locations tab.



Should I change the position of the file:"My Data File"? And move it somewhere else like in the desktop or i just live it in the same folder where is the project ( where is it at now).


I'm really sorry but unlikely there is something wrong.

Feb 19, 2013 8:35 PM in response to Frank Caggiano

I am so sorry but i don't really know how do it..

I just got my first mac one week ago and i'm figuring out how to use it..

if you could explaing me how do it, it will be very nice by your part.


One other Question I saw that the other exercise in my book are all like these, do you know where can I go for get more details about working with the files?


Thanks again, you got a lot of patience! 🙂

Feb 20, 2013 8:56 AM in response to mtortora

Try this first locate the My Data File file in the Finder. You will need to go to the folder that you have the xcode project in. The file should be in that area. Once you have it located go to the project in Xcode and open the main.c source file.


Now delete everything between the quote marks in the fopen statment

That is turn this

fp = fopen( "/Users/matteotortora/My Data File", "r" );

into this

fp = fopen( "", "r" );

Now back in the Finder select the My Data File file and drag it into the xcode editor. The mouse pointer should turn into a green plus âž• like this. Now position the pointer between the quotes and release the mouse button. The path to the file should be placed into the fopen statement between the quotes. Make sure the quotes are on either side of the path string.


Now compile and run the program.

learn C on the mac file issue.

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