|
Post by Dangermouse on May 16, 2004 8:38:33 GMT -5
basically this is a function where you pass the filename and it will print out the file contents (I know, not very useful but this has taken me an hour to figure out lol) I hope the comments are enough void getFileContents(char *fileName) { /* fp = file pointer, open the file name in read mode */ FILE *fp = fopen(fileName, "r"); /* Number of bytes */ int fSize = 0; /*Current character read*/ char fChar;
/* File buffer, will hold the data in the text file */ char * fBuffer;
/*If we opened the file*/ if (fp != NULL) { /*Read each char until end of file*/ while ((fChar = fgetc(fp)) != EOF) { fSize++; } /* Rewing to beginning of file */ rewind(fp);
/* read fSize number of bytes from file pointer (fp)*/ fread(fBuffer, 1, fSize, fp); /* Append a null terminator to make it a string*/ fBuffer[fSize] = '\0'; /* Print the file data */ printf("%s\n\n",fBuffer);
printf("File size: %d bytes\n", fSize); } else { /* Print the fopen error */ perror("fopen"); } }
And use it like br] char fName[] = "C:\\test.txt"; getFileContents(fName);
|
|