I've been asked to design a simple program in C that can open a text file, write to it and then display the text. I'm nearly there but I can't seem to `printf` the contents of the file.
Heres what I've got so far:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
FILE *fp;
size_t count;
char const *str = "This is text";
fp = fopen("sample.txt", "wb");
if(fp == NULL) {
perror("Unable to open file");
return EXIT_FAILURE;
}
count = fwrite(str, strlen(str), 1, fp);
int fclose(FILE *fp);
fp = fopen("sample.txt", "rb");
printf("Here are the contents of the file: %s", fp);
getchar();
return 0;
}
It just comes up with `Here are the contents of the file:` and an empty space...any help would be much appreciated :)


Sign In
Create Account

Back to top









