Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Angelus Mortis

macrumors newbie
Original poster
Dec 2, 2011
3
0
I use Xcode4, and my program won't seem to open a text input file. I tried everything from renaming the file name to "sample.txt" and "sample" only, but it still doesn't work.
I also tried putting it in all directories.

It worked in windows Dev C++ BLood shed, but doesn't seem to work in Xcode4.


Please help, machine project is due soon.
P.S. Begginner programmer here, so please don't use too technical terms, I would appreciate a step by step. Thanks.

Nothing wrong in my code I'm sure, but here you go.

Heres my code:
Code:
#include <stdio.h>
#include <stdlib.h>

int main() {
char filename[40];
char ch;
FILE *fp;

fprintf(stdout, "Input filename: "); 
fscanf(stdin, "%s", filename);

if ((fp = fopen(filename, "r")) == NULL) 
{
    fprintf(stderr, "ERROR: %s does not exist.\n", filename); exit(1);
}
while ((fscanf(fp, "%c", &ch)) == 1) fprintf(stdout, "%c", ch); 

fclose(fp);
return 0; }
 
Last edited by a moderator:
Change your working directory (don't know what Xcode defaults to) in Product > edit scheme > info

or use an absolute path.
 
Change your working directory (don't know what Xcode defaults to) in Product > edit scheme > info

or use an absolute path.

Sorry but how do you change your working directory?
I went to Product > edit scheme > info
but it just says
Build configuration : Debug
Executable : Filename
Debugger: GDB
 
Sorry but how do you change your working directory?
I went to Product > edit scheme > info
but it just says
Build configuration : Debug
Executable : Filename
Debugger: GDB

But why don't you just use an absolute path, it's just to test the functionality right. You could as well run the executable in the terminal, then you would be working in home directory by default. And perhaps use the terminal to compile as well, then you don't have to learn Xcode as well as C at the same time, just a thought.
 

Attachments

  • screen.png
    screen.png
    92.8 KB · Views: 1,620
But why don't you just use an absolute path, it's just to test the functionality right. You could as well run the executable in the terminal, then you would be working in home directory by default. And perhaps use the terminal to compile as well, then you don't have to learn Xcode as well as C at the same time, just a thought.

OMG Thanks so much. haha.
Wish me luck in this field! :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.