Since this apparently isn't homework, here's some sample code of how I'd do it. I'm just allocating a huge block of memory for the entire file, since you're going to read the whole thing eventually anyway, but if you're dealing with large files it's usually better to handle them a line at a time.
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
// first, get the name of the file as an argument
if (argc != 2) {
printf("SYNTAX: %s <input file>\n", argv[0]);
return -1;
}
// open the file
FILE* fp = fopen(argv[1], "r");
if (fp == NULL) {
printf("ERROR: couldn't open file\n");
return -2;
}
// seek to the end to get the length
// you may want to do some error-checking here
fseek(fp, 0, SEEK_END);
long length = ftell(fp);
// we need to seek back to the start so we can read from it
fseek(fp, 0, SEEK_SET);
// allocate a block of memory for this thing
// the +1 is for the nul-terminator
char* buffer = malloc((length + 1) * sizeof(char));
if (buffer == NULL) {
printf("ERROR: not enough memory to read file\n");
return -3;
}
// now for the meat. read in the file chunk by chunk till we're done
long offset = 0;
while (!feof(fp) && offset < length) {
printf("reading from offset %d...\n", offset);
offset += fread(buffer + offset, sizeof(char), length-offset, fp);
}
// buffer now contains your file
// but if we're going to print it, we should nul-terminate it
buffer[offset] = '\0';
printf("%s", buffer);
// always close your file pointer
fclose(fp);
return 0;
}
Whew, it's been a while since I've written C code. Hopefully people will chime in with helpful corrections/notices of massive problems. :)