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

sunbear2

macrumors newbie
Original poster
Jan 13, 2005
1
0
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.
Code:
[color=Dark Olive Green]
#include <stdio.h>
#include<stdlib.h>
#include<string.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 ()
{
	printf("here1\n");
	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("here2\n");
	printf("%s", INFILE);
	if((fp= fopen(INFILE,"r")) == NULL){
		printf("unable to open %s!\n\n",INFILE);
		//perror(INFILE);  /* display las error message */
		exit(1);
	}
			
	/* 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 
		latter*/
	c=0; /* reset to start of array*/
	fgets(iline, 81,fp);
	while(!feof(fp)){
		
		if(iline[0] == '*'){
			offset[c]= ftell(fp);
			c++;
		}/*end if */
fgets(iline,81,fp);

	} /*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;
}
[/color]
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.