Solved

Create a function that passes a text file.

Posted on 2013-01-29
7
292 Views
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.
0
Comment
Question by:lostinspace9
7 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 38832049
What snippet?
0
 
LVL 16

Expert Comment

by:HooKooDooKu
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.
0
 
LVL 32

Expert Comment

by:phoffric
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,
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:lostinspace9
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?

Thanks
function.c
0
 
LVL 32

Expert Comment

by:phoffric
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.
0
 
LVL 84

Assisted Solution

by:ozo
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
 fdata[i]

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]);
     exit(1);
  }
  if( !(solar_wh = fopen(argv[1],"r")) ){
      perror(argv[1]);
      exit(2);
   }
  for (i = 0; ...
0
 
LVL 32

Accepted Solution

by:
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:
2.5456
3.55
etc.

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.
     http://www.cplusplus.com/reference/cstdlib/atof/?kw=atof

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).
     http://www.cplusplus.com/reference/cstdio/fscanf/
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
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 and use structures in the C programming language.
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.

746 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