you have opened a file and then written the text "fopen example" to it OK.
from your title "Writing a program to split a text file into words" it sounds more like you need to open a file, read text from it and count the words read.
This modified version of your program, which reads a word from a file and prints it, should help you get started
CODE
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE *p;
int numOfWords;
char word[20];
numOfWords = 0;
p = fopen("words.txt", "rd");
if(p != NULL)
{
fscanf(p," %s",word); // read a word (stops at next white space)
printf("%s", word);
fclose(p);
}
return 0;
}
the statement
fscanf(p," %s",word);
skips leading 'white space' (spaces and new lines), reads characters into word[] stopping at next 'white space'.
put this in a loop and count the words read until end of file
This post has been edited by horace: 6 Nov, 2006 - 01:20 PM