0

I have a code that reads in a text file, and turns each word from the file into a character array (needs to be a character array, no strings allowed) that's a data member of a Word class. When a punctuation is encountered, all the Word objects get stored into a Sentence class as a linked list. When a tab is encountered, all the Sentences before are stored in a Paragraph class as a linked list. What I need help with is a way to check for '\t' in the text file. I'm reading in using

while(myFile >> charArray){
//stuff
}

As this reads in a full word and stores it in charArray. If it is the end of a sentence, charArray[size] (size being the last element) will == '.' || == "!" || == "?" so I can handle making a new Sentence every time (I can assume the text file will be properly formatted every time). What I don't know how to do is how to check for the '\t' character to make a new paragraph. Will '\t' get picked up by myFile >> charArray? I tried messing around with various if statements to see if I could pick up a '\t' character but I haven't been able to. So how can I check for a tab while reading in from a text file into a character array?

Tommy K
  • 1,759
  • 3
  • 28
  • 51
  • 1
    Personally, I'd do it the good old fashioned K&R way: [fgetc()](http://linux.die.net/man/3/fgetc). – FoggyDay Oct 22 '14 at 18:57
  • 4
    I suggest: 1. Read the file line by line. 2. Search for `'\t'` in the line and divide the line at the tabs. 3. Process each section of the divided line using `istrstream`. – R Sahu Oct 22 '14 at 18:58
  • This is C code unless you're using `std::string` which you should be because the alternative is terrifying. – tadman Oct 22 '14 at 19:06
  • 1
    Google "C++ read line into string" and "C++ split string at character". The "no strings allowed" part in non-nonsensical even if this is a homework question. – Kijewski Oct 22 '14 at 19:10
  • [Check here](http://stackoverflow.com/questions/23047052/why-does-reading-a-struct-record-fields-from-stdistream-fail-and-how-can-i-fi) please, for some more sophisticated parsing concepts. – πάντα ῥεῖ Oct 22 '14 at 19:23
  • This is wrong: `== '.' || == "!" || == "?"`. You should only be looking at `chars` like this: `== '.' || == '!' || == '?'`. And to check for `\t` you need: `== '\t'`. – Galik Oct 22 '14 at 19:42
  • @Galik the "" was a typo, I didn't copy paste straight from the code – Tommy K Oct 22 '14 at 20:21

2 Answers2

1

iostreams operator>> will, by default, read and discard leading whitespace. Including your tab character that begins a new paragraph. I suggest that a blank line should also be considered a paragraph separator. And many people use a run of normal space characters at the beginning of a line to produce indentation, instead of a tab, so you should check for that too.

My preferred approach would be to use getline to read an entire line of text, whitespace and all. Test the first character and decide whether to start a new paragraph. Then load that line into a stringstream and use operator>> to pull out individual words. This is pretty similar to R Sahu's comment... but I would not split the line on tab characters. Rather, you're concerned with tabs/spaces at the beginning of a line only.

You can also experiment with noskipws and reading individual whitespace characters

Community
  • 1
  • 1
Ben Voigt
  • 277,958
  • 43
  • 419
  • 720
  • How would I read in using getline? I look around and I'm not sure I get it. I declared a `char line[256];` and then tried using `getline(myFile, line);` but I get an error saying no matching function call. I included . So what would I need to do to get the read-in line into a buffer character array? – Tommy K Oct 23 '14 at 19:07
  • @TommyK: That's the syntax for reading into a `std::string`, which your question says you aren't allowed to use. [The char-array version](http://en.cppreference.com/w/cpp/io/basic_istream/getline) is a member function, so you'd say `myFile.getline(line, 256);` – Ben Voigt Oct 23 '14 at 19:23
0

This may help you/be what your looking for:

http://www.cplusplus.com/reference/cstring/strchr/

your interested in the overload provided by C++:

char * strchr ( char * str, int character );

this will return a pointer to the first occurance of the character, or, a null pointer if not found.

An example of usage (provided by Cplusplus.com as well) can be found here:

/* strchr example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] = "This is a sample string";
  char * pch;
  printf ("Looking for the 's' character in \"%s\"...\n",str);
  pch=strchr(str,'s');
  while (pch!=NULL)
  {
    printf ("found at %d\n",pch-str+1);
    pch=strchr(pch+1,'s');
  }
  return 0;
}