Hi there^^ Its my first post on these forums, and as much as I hate it to be a shout for help ive got a problem I could really use someone with a bit of knowledge to help me out with =)
My scenario being ive got to write a code that asks the user for input from a file (in this particular case it will be a number of folat values in .txt format), scan them into an array, and manipulate said array with a few mathematical functions, and whack it through a final calculation to give a value output to the screen.
Right now ive spent days and days trying to code the first part correctly, and its kicking my ass (Im new to any form of programming!). I could make it more basic by defining the array size, but I actually find all this quite interesting, so id rather learn the hard way :cheesy:
Anyway, without further ado, here's what ive got so far, and no, it doesnt work =P
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
FILE *fp;
char ch, *filename (char *buf, size_t length, FILE *f);
long lSize;
float *buffer;
int main(int argc, char *argv[])
{
#define initial_value 0.07
#define increment 0.125
while (1)
{
// Input filename.
printf("\nEnter a filename: ");
gets(*filename);
// Try to open the file.
if ( (fp = fopen(filename, "r")) == NULL )
{
perror("\nError opening file");
puts("\nEnter x to exit, any other to try again.");
if ( (ch = getch()) == 'x')
break;
}
else
{
// obtain file size.
fseek (fp , 0 , SEEK_END);
lSize = ftell (fp);
rewind (fp);
// allocate memory to contain the whole file.
buffer = (float*)malloc(lSize);
if (buffer == NULL) exit (EXIT_FAILURE);
else printf("\n%d bytes of memory allocated to file...\n", lSize);
printf("\nSuccessful opened %s!\n", filename);
// copy the file into the buffer.
// terminate
fclose (fp);
free (buffer);
puts("\nEnter x to exit, any other to continue.");
if ( (ch = getch()) == 'x')
break;
}
}
}
//--------------FUNCTIONS--------------
char *filename (char *buf, size_t length, FILE *f)
{
char buf[40], *p;
if (p = fgets (buf, length, f))
{
size_t last = strlen (buf) - 1;
if (buf[last] == '\n') {
buf[last] = '\0';
}
else {
fscanf (f, "%*[^\n]");
(void) fgetc (f);
}
}
return p;
}
The function gives me the following errors:
19: error: cannot convert `char*(*)(char*, size_t, FILE*)' to `const char*' for argument `1' to `FILE* fopen(const char*, const char*)'
57: error: declaration of 'char buf[40]' shadows a parameter
:: === Build finished: 2 errors, 0 warnings ===
If i hash out the function, and simply replace the *filename code in line 7 with
filename[40];
it works fine!
Also, I cant tell if the code for buffer is working or not, it seems to be, but im too new to all this to tell, until i write the file into an array (which I cant do yet since I do not know how do define an array size for a file that can be of different sizes...)
The code for the array will go in the middle of the program, where ive left room for it under
// copy the file into the buffer.
Im sorry, that was a real mouthful, any help would be greatly appreciated ^^
Chris
edit: you can ignore the define's for now, theyre to be used in the calculations later on =)