1

Objective:

I am reading a text file word by word, and am saving each word as an element in an array. I am then printing out this array, word by word. I know this could be done more efficiently, but this is for an assignment and I have to use an array.

I'm doing more with the array, such as counting repeated elements, removing certain elements, etc. I also have successfully converted the files to be entirely lowercase and without punctuation.

Current Situation:

I have a text file that looks like this:

beginning of file




more lines with some bizzare     spacing
some lines next to each other
while

others are farther apart
eof

Here is some of my code with itemsInArray initialized at 0 and an array of words refered to as wordArray[ (approriate length for my file ) ]:


ifstream infile;
infile.open(fileExample);

while (!infile.eof()) {

    string temp;
    getline(infile,temp,' ');  // Successfully reads words seperated by a single space
    
    
    if ((temp != "") && (temp != '\n') && (temp != " ") && (temp != "\n") && (temp != "\0") {
            wordArray[itemsInArray] = temp;
            itemsInArray++;
    }

The Problem:

My code is saving the end of line character as an item in my array. In my if statement, I've listed all of the ways I have tried to disclude the end of line character, but I've had no luck.

How can I prevent the end of line character from saving as an item in my array?

I've tried a few other methods I have found on threads similar to this, including something with a *const char that I couldn't make work, as well as iterating through and deleting the new line characters. I've been working on this for hours, I don't want to repost the same issue, and have tried many many methods.

Community
  • 1
  • 1
wrightMatthew
  • 94
  • 1
  • 1
  • 8
  • My .txt file contains an entire book without uppercase letters, at this point. There are several "returns" or new lines to separate chapters. I have already bypassed saving extra spaces as items in my array, I am only struggling to **not** save the new line characters. Again, thank you. – wrightMatthew Feb 01 '15 at 03:33
  • *Related*: http://stackoverflow.com/q/5605125/78845 – Johnsyweb Feb 01 '15 at 03:40

3 Answers3

3

The standard >> operator overloaded for std::string already uses white-space as word boundary so your program can be simplified a lot.

#include <iostream>
#include <string>
#include <vector>

int
main()
{
  std::vector<std::string> words {};
  {
    std::string tmp {};
    while (std::cin >> tmp)
      words.push_back(tmp);
  }
  for (const auto& word : words)
    std::cout << "'" << word << "'" << std::endl;
}

For the input you are showing, this will output:

'beginning'
'of'
'file'
'more'
'lines'
'with'
'some'
'bizzare'
'spacing'
'some'
'lines'
'next'
'to'
'each'
'other'
'while'
'others'
'are'
'farther'
'apart'
'eof'

Isn't this what you want?

5gon12eder
  • 21,864
  • 5
  • 40
  • 85
0

The stream's extraction operator should take care of that for you

std::ifstream ifs("file.txt");
while (ifs.good())
{
    std::string word;
    ifs >> word;
    if (ifs.eof())
    {
        break;
    }

    std::cout << word << "\n";
}
James Adkison
  • 8,891
  • 2
  • 25
  • 39
  • Thank you very much. This helped. I am new to coding and didn't know about the break feature. A problem I was getting is that sometimes a return (new line), or multiple returns would read in as its own string. – wrightMatthew Feb 01 '15 at 22:03
0
int main()
{  
    char *n;
    int count=0,count1=0;
    ofstream output("user.txt");
    output<<"aa bb cc";
    output.close();
    ifstream input("user.txt");
    while(!input.eof())
    {
        count++;
        if(count1<count)
        cout<<" ";
        count1=count;

        input>>n;
        cout<<n;
    }
    cout<<"\ncount="<<count;
    getch();
}
Tom Sabel
  • 3,426
  • 28
  • 40