Create a function that passes a text file.

Posted on 2013-01-29
Last Modified: 2013-02-13
I need to know how to change the code in the attached snippet into a function.  The text file passed contains a column of real numbers that I need to sum. I cannot figure out how to pass the text file to the function.
Question by:lostinspace9
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
LVL 84

Expert Comment

ID: 38832049
What snippet?
LVL 16

Expert Comment

ID: 38836255
You can't pass a text file to a function.  You can only pass intrinsic variables (int, char, double), and pointers.

So you have to do something like the following:

1. Load the text file into memory (perhaps a char array) and pass a pointer where the data is loaded in memory.

2. Load the name of the text file into a string array, and pass that array to the function.  The function can then open the file from within the function.

3. Open the file using a file object or via a file handle, then pass a pointer to the object or the file handle to the function.

Can't say much more without the details likely in the missing snippet.
LVL 32

Expert Comment

ID: 38837250
But you can pass a text file handler to a function.

Anyway, are you aware that your attachment is not attached? Try attaching again, and double-check to confirm that it is attached. Some extensions will not attach. Extensions .c and .txt should give no trouble,
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.


Author Comment

ID: 38839611
Sorry about not attaching file.  I have not tried the "for" loop I found on the web to use because the one I had intended to use was frowned upon by several C gurus.  Does it look like it will work?

LVL 32

Expert Comment

ID: 38840466
What reason did the gurus give for the frown?

You could write the accumulation as:
    solar  +=  fdata[ i ]; // accumulate solar values

You can define a function that passes in the file handler. It might look something like this:

float accumulateSumFromFile( FILE * fh ) {
   return solar;

Just move the appropriate code from the main() function into this function.
LVL 84

Assisted Solution

ozo earned 250 total points
ID: 38841515
.txt seems a misleading name for a file that contains raw float data

It seems  a little strange to be summing the first item of the first block of data,
the second item of the second block of data, etc.

If there are more blocks of data in the file than there are items in a block, then

Open in new window

may generate undefined behavior

You never do anything with the sum you calculate.

To pass a filename to the program, you might use

int main(int argv,char *argv[]) {
  FILE * solar_wh;
  if( argv<2 ){
     printf("usage: %s filename\n",argv[0]);
  if( !(solar_wh = fopen(argv[1],"r")) ){
  for (i = 0; ...
LVL 32

Accepted Solution

phoffric earned 250 total points
ID: 38846331
Earlier, I was focusing on your need for passing a text file to a function. Looking at your code and question I am guessing that you have a text file with a text decimal number in each line, like:

In this case if all you want to do is add these numbers in the function, then you don't need an array of floats; you only need an accumulator variable.

 float solar = 0.;
 float newSolarVal;
 char buffer[100]; // assumes the text line read in is less than 100 chars

Use fgets(buffer, 100, solar_wh)
Now buffer has the text value, for example, "2.5456", which you need to convert to a float.
You can use atof(buffer) to do this.

There is also fscanf which can read the text file converting the floating point text into binary float values directly (but IMO, it is sometimes a little harder to use and not so easy to detect errors).

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at  To view more iPhone tutorials, visit This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

626 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