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.

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);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.