PDA

View Full Version : Cant read text input in xCode 4? C language




Angelus Mortis
Dec 2, 2011, 08:07 AM
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:

#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; }



subsonix
Dec 2, 2011, 08:32 AM
Change your working directory (don't know what Xcode defaults to) in Product > edit scheme > info

or use an absolute path.

Angelus Mortis
Dec 2, 2011, 08:53 AM
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

subsonix
Dec 2, 2011, 09:13 AM
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.

Angelus Mortis
Dec 2, 2011, 09:28 AM
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