Improve company productivity with a Business Account.Sign Up

x
?
Solved

Create a function that passes a text file.

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

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 33

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
Easily Design & Build Your Next Website

Squarespace’s all-in-one platform gives you everything you need to express yourself creatively online, whether it is with a domain, website, or online store. Get started with your free trial today, and when ready, take 10% off your first purchase with offer code 'EXPERTS'.

 

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 33

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 85

Assisted Solution

by:ozo
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
 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 33

Accepted Solution

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

Simple Misconfiguration =Network Vulnerability

In this technical webinar, AlgoSec will present several examples of common misconfigurations; including a basic device change, business application connectivity changes, and data center migrations. Learn best practices to protect your business from attack.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
The goal of this video is to provide viewers with basic examples to understand and use pointers 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.

601 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