Create a function that passes a text file.

Posted on 2013-01-29
Medium Priority
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 85

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 33

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,
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.


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 33

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 85

Assisted Solution

ozo earned 750 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 33

Accepted Solution

phoffric earned 750 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

Independent Software Vendors: 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

Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
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.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

580 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