I've got a question about functions.....here is the current code that I have...
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
main()
{
int tall,wide,position,x=0,y=0,a=0;
char line[50],common[50];
FILE *ifp;
printf("\n\n Opening OBJECT.DAT ");
if ((ifp=fopen("object.dat", "r")) == NULL)
{
printf("\n * error opening file object.dat *");
}
fgets(line,100,ifp);
position=(strlen(line)-1);
while(x<position)
{
common[a]=line[x];
if (line[x]='\0')
{ a++; break; }
x++; a++; position--;
}
tall=atoi(common); common==0; a=0;
while(a<position)
{
common[a]=line[x];
if (line[x]='\0')
{ x++; break; }
x++; a++; position--;
}
wide=atoi(common);
printf("\n\n\tHeighth: %i",tall);
printf("\n\tWidth: %i\n\n\t",wide);
}
This code is made to read in 2 numbers on the first line of the objects.dat file...these 2 numbers will be the Heighth and the Width of our giant rectangle...I know there is a much better way to write that code to not involve 2 giant while statements...Can anyone suggest a method?
Also...we will be needing to do a very similar thing with that code for leading lines 2 up to lines 40 or so.....but these lines contain 3 numbers seperated by spaces....since I will be calling it over and over again i figured it would be best to make that entire thing as a function which we can pass things too and have constantly reading from the file when we need to call it...but how far can variables in a function go? If we call a function we make...and it reads the file...seperates the 2 items...and stores them in a variable....will the scope and range of the variable allow us to use that variable in the rest of our main() function? anyone suggest a way to write that code into a nice compact function? Thanks.