edit: took out the code since I posted updated code a few posts down
edit: took out the code since I posted updated code a few posts down
Last edited by Sholcomb1; 11-05-2006 at 02:40 PM.
> FileOpen(&fileinp, dataFile, &length);
You need an extra * in several places then.
Code:void FileOpen(FILE **fileptr, char *file_name, int *lengthptr) { printf("Please enter the filename you wish to open : "); scanf("%s", file_name); *fileptr = fopen(file_name, "r"); if (*fileptr == NULL) { printf("An error has occurred while attempting to open the file\n"); exit(-1); } while(fgetc(*fileptr) != EOF) { length++; } }
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
ooooh, so that extra * in the prototype is what was needed, Ok. Thanks alot!
Ok, so the extra * allowed the file input to work out well, but I'm still having issues with the length pointer counter actually incrementing.
I pass the variable by reference "length" to the function, and the value of length is always zero. However, when I copy/paste that exact same code into main and swap out the pointers for the regular variables it works perfectly fine. Am I not allowed to do an increment to lengthptr? Am I missing something that would keep lengthptr from incrementing?Code:void FileOpen(FILE *fileptr, char *file_name, int *lengthptr) { printf("Please enter the filename you wish to open : "); scanf("%s", file_name); fileptr = fopen(file_name, "r"); if (fileptr == NULL) { printf("An error has occurred while attempting to open the file\n"); exit(-1); } while(fgetc(inp1) != EOF) { length++; } }
(this is what I would put into main, which would work out fine)Code:while ( fgetc(fileinput) != EOF { length++; }
Same thing - use a *
Though watch out for
++*p
is different from
*p++
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.