#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; }
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 ===
filename[40];
// copy the file into the buffer.
.
char ch, *filename (char *buf, size_t length, FILE *f);
char filename[40].sizeof(int)).char filename[40] to char input_filename[40].char buf[40] declared on line 57 hides the other variable in same scope which is teh function argument char *filename (char *buf, size_t length, FILE *f). May be you can change line 57 to char* tmp_buf
I wish I could use C++, since it seems to bemuch better for dynamic memory allocation ><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
C Syntax (Toggle Plain Text)
#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:
If i hash out the function, and simply replace the *filename code in line 7 withC Syntax (Toggle Plain Text)
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 ===it works fine!C Syntax (Toggle Plain Text)
filename[40];
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 underC Syntax (Toggle Plain Text)
// 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 =)
ptr->name; /* might hold a name */ (*ptr).name; /* same as above */
Better no, easier perhaps...

#include <stdio.h> #include <stdlib.h> #include <string.h> #include <errno.h> #define initial_value 0.07 #define increment 0.125 int read_one_float( float* out_float, FILE *f ) ; int main(int argc, char *argv[]) { FILE *file_pointer = 0; char ch = 0; char filename[40] = "" ; long lSize = 0; float *buffer = 0; float tmp_float = 0 ; int i = 0 ; int j = 0 ; // Input filename. printf("\nEnter a filename(max 40 chars): "); gets(filename); // Try to open the file. if ( (file_pointer = fopen(filename, "r")) == NULL ) { perror("\nError opening file"); return 1 ; } printf("\nSuccessful opened %s!\n", filename); //if we reach here file is opened successfully.. //given it's an ascii file lets assume that there is //one float number per line. So total number of floats //in file is same as number of new lines + 1. // calculate the size of our buffer. while( EOF != (ch = fgetc(file_pointer)) ) if( ch == '\n' ) lSize++ ; lSize++ ; //for +1 rewind (file_pointer); // allocate memory to contain the whole file. buffer = (float*)malloc(sizeof(float)*lSize); if (buffer == NULL) return 1; else printf("\n%d bytes of memory allocated to buffer for %d entries...\n", sizeof(float)*lSize, lSize); // copy the file into the buffer. while( 0 != read_one_float( &tmp_float, file_pointer ) ) buffer[i++] = tmp_float ; //can be done like this also //while( 0 != read_one_float( &buffer[i++], file_pointer ) ) printf("\nnumber of entries read = %d", --i ) ; // terminate fclose (file_pointer); free (buffer); printf("\nData read from file is.") ; for( j = i; j >= 0; j-- ) printf("\nbuffer[%d] = %f", i-j, buffer[i-j] ) ; //we have the data in the buffer //i contains the number of entries. printf("\n\n") ; return 0 ; } //--------------FUNCTIONS-------------- int read_one_float( float* out_float, FILE *f ) { char buf[100] = "" ; if (0 == fgets(buf, 100, f)) return 0 ; *out_float = atof( buf ) ; return 1; }
This pointer business has got me really confused
Is the general consensus that I cannot feasibly accomplish dynamiclly allocating a string size (see: my attempt at a function at the end of the program)?
Chris
This pointer business has got me really confused
Is the general consensus that I cannot feasibly accomplish dynamiclly allocating a string size (see: my attempt at a function at the end of the program)?
Chris
float *float_pointer; float_pointer = (float *)malloc(sizeof(float));
int i = sizeof(string);
| DaniWeb Message | |
| Cancel Changes | |