lostinspace9
asked on
Create a function that passes a text file.
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.
What snippet?
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.
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.
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,
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,
ASKER
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
Thanks
function.c
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.
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.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.