0
FILE *ranch;
FILE *myfile;
char line[500];
case 1:
		ranch=fopen("garrudaRanch.txt ","w+");
		if(ranch != NULL)
		{
		printf("Enter amount of Corral: \n");
		scanf("%d",&corral);
		fprintf(ranch,"%d\n",corral);
		printf("Enter size of Corral: \n");
		scanf("%d",&corrals);
                fprintf(ranch,"%d\n",corrals);
		}
		fclose(ranch);
		break;
	case 2:
		myfile=fopen("garrudaRanch.txt ","r+");
		if(myfile != NULL)
		{
		//fscanf(myfile,"%s",line[1]);
		printf("Number of Corral: %s",line[1]);
		printf("Size of Corral: %s",line[2]);
		}
		else
			printf("File didnt open");
		fclose(myfile);
		break;

Can someone show me how to read from the file line by line please. It write the information to the file but i did some research but i keep failing. I try the fscanf and the gets but to no success.

2
Contributors
1
Reply
3
Views
5 Years
Discussion Span
Last Post by Ancient Dragon
1

Call fgets() to read entire lines of a file

char line[255] = {0};
myfile=fopen("garrudaRanch.txt ","r+");
if( myfile != NULL)
{
   while( fgets(line, sizeof(line), myfile )
   {
         // do something
   }
   fclose(myfile);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.