Need help w/ array passing and file streams

Gem4lyf
Gem4lyf used Ask the Experts™
on
I'm doing a program that involves both C and inline assembly. My question is, i'm trying load information from the file, store it in an array, and pass it to several functions w/out changing it so that i may do different things to display info on the screen. I've already set up the  dataFile = fopen(filename,"r"); line. The first function is a boolean to actually get that data...Then I am supposed to send it to another fucntion to sort it. I'm not totally sure how to go about this. I have the outline also to both the boolean function and the sort function
Boolean getData( FILE* f, int x[], int* cursize, int maxsize )
void sortData( int x[], int cursize )
Any help would be much appreciated
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Ehh... Could you be a bit more speciffic? How far have you got? How much C do you know? You don't "get" the data just by doing fopen. You need to read it as well... What format is the datafile? Passing arrays is very simple. Just put the name of the array as function argument, and declare the array as (for example) int array[] int the function prototype. Just remember that you will receive the same array. This means that you cant change the values if you want to keep it intact.

Author

Commented:
Thank you for the advice on reading it...The data file that is being used is a txt file. The thing about it, that I need to do it in assembler but I figured, it would be easier to see it in C first b4 i did it in assembler. So far i have initialized my variables, set up my functions(i have a skeleton of my program). I'm trying to work w/ fscanf because that is the another function I have to call in my assembler language. here's what I have so far...
int main(int argc, char* argv[])
{
  int SECRET_NUM = 100;  /* WHAT IS THIS FOR?  */
  int x[MAX];
  int cursize;
  int avg, highest, lowest, median;
  FILE* dataFile;      /* internal file pointer for the file */
  char filename[100];  /* string to hold the filename */
  Boolean loadOK;

  /* Ask the user for a filename, then open the data file.
     Be sure to check that the open was successful.
     If not, display a message, and terminate the program.
  */
     printf("Enter your filename > ");
     scanf("%s", filename);       /* get name from user */
    dataFile = fopen(filename,"r");
 


  if (dataFile == NULL)
  {
    printf("No such file - program terminated\n\a");
    exit(1);
  }
 
  else
  loadOK = getData( dataFile, x, &cursize, MAX );

  /* If the load was not OK, we ran out of room.
     Display a message, and terminate the program.
  */

  sortData( x, cursize );
  displayGrades( x, cursize );
  avg = getAvg( x, cursize );
  otherStats( x, cursize, &highest, &lowest, &median );
  displayStats( cursize, avg, highest, lowest, median );

  //printf("\n\nSECRET_NUM SHOULD BE 100: %d\n", SECRET_NUM);

  return 0;

Boolean getData( FILE* f, int x[], int* cursize, int maxsize )
{
         

}
void sortData( int x[], int cursize )
{

}
int getAvg( const int x[], int cursize )
{

}
void otherStats( const int x[], int cursize,
                 int* highest, int* lowest, int* median )
{
     
}
void displayStats( int numberOfScores, int avg, int highest,
                   int lowest, int median )
                       {

}
void displayGrades( const int x[], int cursize )
{

}


}
obg

Commented:
Wow!? - Score already...?

Anyway, in getData, you'll need to do something like:

*cursize = 0;
while (*cursize < maxsize && fscanf(f, "%d", &x[*cursize]) != EOF)
   ++*cursize;

In your sortData, use qsort, which is a very useful function. It is not trivial, since it needs a compare function, but it is not that hard either. Just look it up in some docs, or ask again...

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial