Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- struct stat file_status;
- char *buf = NULL;
- FILE * pFile;
- stat("tmp.xml", &file_status);
- buf = (char*)malloc(file_status.st_size);
- pFile = fopen ("tmp.xml","r");
- fread (buf,1,file_status.st_size,pFile);
- fclose(pFile);
- #include <stdlib.h>
- #include <stdio.h>
- long fsize(FILE * file)
- {
- if(fseek(file, 0, SEEK_END))
- return -1;
- long size = ftell(file);
- if(size < 0)
- return -1;
- if(fseek(file, 0, SEEK_SET))
- return -1;
- return size;
- }
- size_t fget_contents(char ** str, const char * name, _Bool * error)
- {
- FILE * file = NULL;
- size_t read = 0;
- *str = NULL;
- if(error) *error = 1;
- do
- {
- file = fopen(name, "rb");
- if(!file) break;
- long size = fsize(file);
- if(size < 0) break;
- if(error) *error = 0;
- *str = malloc((size_t)size + 1);
- if(!*str) break;
- read = fread(*str, 1, (size_t)size, file);
- (*str)[read] = 0;
- *str = realloc(*str, read + 1);
- if(error) *error = (size != (long)read);
- }
- while(0);
- if(file) fclose(file);
- return read;
- }
- #include <stdio.h>
- #include <stdlib.h>
- int main (int argc, char *argv[]) {
- char *buffer; /* holds the file contents. */
- size_t i; /* indexing into buffer. */
- size_t buffer_size; /* size of the buffer. */
- char *temp; /* for realloc(). */
- char c; /* for reading from the input. */
- FILE *input; /* our input stream. */
- if (argc == 1) {
- fprintf(stderr, "Needs a filename argument.n");
- exit(EXIT_FAILURE);
- }
- else if (argc > 2) {
- fprintf(stderr, "Well, you passed in a few filenames, but I'm only using %sn", argv[1]);
- }
- if ((input = fopen(argv[1], "r")) == NULL) {
- fprintf(stderr, "Error opening input file %sn", argv[1]);
- exit(EXIT_FAILURE);
- }
- /* Initial allocation of buffer */
- i = 0;
- buffer_size = BUFSIZ;
- if ((buffer = malloc(buffer_size)) == NULL) {
- fprintf(stderr, "Error allocating memory (before reading file).n");
- fclose(input);
- }
- while ((c = fgetc(input)) != EOF) {
- /* Enlarge buffer if necessary. */
- if (i == buffer_size) {
- buffer_size += BUFSIZ;
- if ((temp = realloc(buffer, buffer_size)) == NULL) {
- fprintf(stderr, "Ran out of core while reading file.n");
- fclose(input);
- free(buffer);
- exit(EXIT_FAILURE);
- }
- buffer = temp;
- }
- /* Add input char to the buffer. */
- buffer[i++] = c;
- }
- /* Test if loop terminated from error. */
- if (ferror(input)) {
- fprintf(stderr, "There was a file input error.n");
- free(buffer);
- fclose(input);
- exit(EXIT_FAILURE);
- }
- /* Make the buffer a bona-fide string. */
- if (i == buffer_size) {
- buffer_size += 1;
- if ((temp = realloc(buffer, buffer_size)) == NULL) {
- fprintf(stderr, "Ran out of core (and only needed one more byte too ;_;).n");
- fclose(input);
- free(buffer);
- exit(EXIT_FAILURE);
- }
- buffer = temp;
- }
- buffer[i] = '