Solved

PASSING ARRAYS FROM FILES

Posted on 2003-12-07
9
287 Views
Last Modified: 2010-04-15
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
Comment
Question by:shorty225
9 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
ID: 9893873
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
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 9894668
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
 

Author Comment

by:shorty225
ID: 9894711
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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 45

Assisted Solution

by:sunnycoder
sunnycoder earned 250 total points
ID: 9894858
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
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 9894976
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
 
LVL 45

Expert Comment

by:Kdo
ID: 9896003

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
 
LVL 24

Expert Comment

by:fridom
ID: 9995637
Well pankajtiwarys code will not even get compiled. Not a very big help...
Regards
Friedrich
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now