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
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,
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.


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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C hashtable library 3 100
Super Scope, DHCP 5 91
C++ :Change value from  DisableCMD registry 4 66
Secondary drive is failing... D Volume 11 17
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…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

827 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