Xcode and C programming

Discussion in 'Mac Programming' started by sunbear2, Jan 13, 2005.

  1. sunbear2 macrumors newbie

    Jan 13, 2005
    Hi, I am having trouble adding a text file to my project. I have written a program in C to read from a file. I have added the file name untitled but
    the program says that the file does not exist and can't be open.
    When I looked at the project folder the file is there and when I look at targets it is also there. Thanks for your help. Here is the code.
    I have copied it to the unix terminal and it worked just fine.
    [color=Dark Olive Green]
    #include <stdio.h>
    /* SEEK-SET from beginning of file start at bite zero. From zero 
    	SEEK-CUR Set position to current location plus offset
    SEEK-END Set position of EOF plus offfset */
    #define INFILE  "../Untitled1"
    #define ARRAYSIZE 100
    int main ()
    	FILE *fp;
    	long offset[ARRAYSIZE];
    	int c=0;
    	char iline[81];
    	/* intialize array with sentinel values*/
    	for (c=0; c < ARRAYSIZE; c++)
    		offset[c]=-1; /* setting all arrray to -1. -1 is an invalid file
    		address so that i can recognize if fput a file in theres */
    //	system("clear");
    	printf("%s", INFILE);
    	if((fp= fopen(INFILE,"r")) == NULL){
    		printf("unable to open %s!\n\n",INFILE);
    		//perror(INFILE);  /* display las error message */
    	/* Read all lines from the file and look for the start that indicates the next line is a title.
    		Grab the file pointer to the title and it in the arrray of offssets so we can reposition the file pointer 
    	c=0; /* reset to start of array*/
    	fgets(iline, 81,fp);
    		if(iline[0] == '*'){
    			offset[c]= ftell(fp);
    		}/*end if */
    	} /*end while*/
    /* Revisti all titles by getting the saved offset and positoning the
     file pointer from the beginning of the file */
    for(c=0; offset[c] >=0; c++) /* Walk through the file address position fiel at addres saved and the nprintf */
    	fseek(fp,offset[c], SEEK_SET); /* seek_set long it. fseek postion the file pointer where ever location is given */
    				fgets(iline,81,fp); /* then read the title line */
    				printf("%3d. %s", c +1, iline); /* then print it*/
    }/* end for loop */
    	// insert code here...
        printf("Hello, World!\n");
        return 0;
  2. maya macrumors 68040


    Oct 7, 2004
    somewhere between here and there.

Share This Page