Reading in a text file into a char array

I have a project which is about linked lists in the order of: a Story is made of Paragraphs which is made of Sentences which is made of Words. We must treat Words specifically as character arrays, not strings. So I need to read in a story from a text file and make Words by finding the first whitespace/punctuation(everything before the whitespace up until the whitespace or punctuation is the Word), all the Words up to the punctuation are a Sentence (Sentence is a linked list of Words), Paragraph is all the Sentences up until an empty line, and a Story is all the Paragraphs. So I know doing this will let me take a line and put it into a character array:
1
2
3
4
5
6
7
8
9
10
11
12
13
	char charArray[25]; //we are allowed to assume a word won't be longer than 25 characters
	int i = 0;
	ifstream myFile(fileName.c_str());
	if(myFile.is_open()){
		while(!myfile.eof()){
			myFile >> charArray[i];
			i++;
		}
		myFile.close();
	}
	else{
		cout << "Unable to open file";
	}

But how can I break this up so that at a whitespace or punctuation it will take the charArray and package it into a Word? Like what is the syntax I can use to see if the current character it is reading in is ws or punctuation i.e
1
2
3
4
5
if(xxx == ' ' || xxx == '.' || etc){
      {
            take charArray and make it a Word
      }

I suppose I could look at charArray[i] and if it is ws or punctuation then make the Word = charArray[i-1], but is there an if statement I could do that would prevent the ws or punctuation from being read into charArray in the first place? Because a problem I see with the charArray[i-1] method already is that "This is a story." would get put into the array as Thisisastory. and thus I'd be unable to break up at a space.

So to summarize:
I want to read in a text file character by character into a char array, which I can set to length 25 due to context of the project. I want to read in each character and at a whitespace or punctuation, I want to take everything already in charArray and feed that into a Word object constructor (the next Word gets linked to the previous Word, and at a punctuation all the Words linked together become one Sentence, and all the Sentences linked before an empty line become a Paragraph, etc). So how can I get charArray to be only characters in a word, then after the word being read-in ends, charArray resets to empty, and then is populated by the next word and so on.
You can read whole words from the stream up to a whitespace (either space or newline) in C++, so you could dramatically simplify what you already have by, instead of going character by character, read in words like such:
myFile >> charArray;
Use the <cstring> library and utilize the strlen function to check if the last character is punctuation (this assumes the file is formatted like a story so you won't have strings like "a.BCEd.fgh"):
1
2
3
myFile >> charArray; //take in up until whitepsace;
int size = strlen( charArray ); //will count how many characters there are up until the '\0' char
if ( charArray[ size - 1 ] == '.' ) charArray[size - 1] = '\0'; //checks the index before the '\0' and sees if it is a punctuation mark 

And then from there, do what you need to do with that word.
@Keene

That's a fantastic solution but does that method fall into a C-style string function since you need to include <cstring>? My project explicitly states: You may not use the C++ string class nor any C-style string manipulation functions

So to not use strlen I'm going to try
while(charArray[i]){
size++; //assuming size is declared earlier
i++;
}
It seems to work fine this way. (of course having to reset size and i back to 0 after each word) Thanks for your help! I didn't know I could just do myFile >> charArray;
Topic archived. No new replies allowed.