• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 345
  • Last Modified:

PASSING ARRAYS FROM FILES

I AM TRYING TO PASS A FILE ON A DISK AS AN ARRAY TO A FUNCTION.  HOW WOULD I START THIS OFF. HOW WOULD I GET IT TO SCAN THE FILE OFF THE DISK AND PASS IT THROUGH AS A FUNCTION
0
shorty225
Asked:
shorty225
2 Solutions
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Shorty,

First, IT'S NOT NECESSARY TO SHOUT!!!  Most of us can read.  Honestly... :)

You're going to want to set up an array of character strings.

First decide what is the longest line that you'll accept.  (There are ways to get around this limitation.  But let's walk before we try and run.)

Then decide on what's the most number of lines that you'll allow.

Now let's create a couple of constants to record what these limits are and create our table.

#define MAX_LINE_LENGTH 80    /*  The longest line we'll process is 80 characters  */
#define MAX_LINES            100  /*  We can handle up to 100 lines  */

char Lines[MAX_LINES][MAX_LINE_LENGTH];

Now read the file into the array.  You'll have to read one line at a time.  Use fopen() and fgets() to read the file into the table.  Keep track of how many lines that you read so that you don't read more than MAX_LINES lines or you'll overflow the table and the program will probably abort.

Now create a function.  You don't say what the function is supposed to do, so lets just assume that you want to print out the table.

MyFunction (char FileTable[MAX_LINES][MAX_LINE_LENGTH], int LineCount)
{
}

Now within this function you can use the table.  To print it out just use a for(;;) statement for your loop control and use printf() to write the line to the terminal.

That's all there is to it.


Good Luck,
Kent
0
 
pankajtiwaryCommented:
Hi,

There are a few possibilities. Why don't you pass the file name to the function and open it and read it inside the function? If there are constraints, you can read the file, store it in the array and pass the array to the function. Lets say the file has 100 lines and eack line has a maximum of 80 charecters. So, you can define a char array of size 100 x 80. To read the file into the array, you can use gets. First open the file using fopen, then in a while loop (which termicates when the end of file is reached), keep on reading on eline at a time and store it into the array. The gets work like this

i = 0;
open the file using fopen;
while(end of file is not reached) {
    gets(file_pointer, 80 , array[i]);
    increase i by 1.
}

when this part is over, you can call your function with array paasing as a parameter. Each array element contains one line from the file which itself is the charecter array.

CHEERS!!!
0
 
shorty225Author Commented:
sorta like this?
#include<stdio.h>


int main()
{
int i;
int temp;
FILE * fp;
fp =fopen("a:temp.dat","r");

for (i=0; i < 31 && fscanf(fp,"%d",&temp)!=EOF;++i);
       putchar(fp);

      return 0;
      }
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
sunnycoderCommented:
Hi shorty225,

the code you have posted will read in 31 integers from the specified file and tries to print out a "char" value .. however the argument to putchar that you have specified is a FILE * which is not correct ... it needs to be an int

here you are not passing anything to a function and also perhaps you would like to use something like

printf ( "%d ", temp );
in place of putchar

Now to pass the information to  a function (I am assuming that you have fixed file format with 31 ints on a single line )
create an array of 31 ints ... store these values in the array
 fscanf(fp,"%d",&array[i])

and pass this array to the function

my_func ( array );
0
 
pankajtiwaryCommented:
You can say...

char array[100][80];

int main() {
   int i = 0;
   FILE* fp  = fopen("filename.txt", "r");
   while(!feof(fp)) {
        gets(fp, 80, array[i]);
        i++;
    }
    my_function(array);
    return 0;
}

Hope this helps...
0
 
Kent OlsenData Warehouse Architect / DBACommented:

Hi pankajtiwary,

This is obviously a homework assignment.  As the membership agreement states, (and is pointed out almost daily), we don't provide complete codes in response to homework questions.  I'm glad that you were able to follow my instructions, but it doesn't help shorty at all.

Kent
0
 
fridomCommented:
Well pankajtiwarys code will not even get compiled. Not a very big help...
Regards
Friedrich
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now