0

Here is what I got so far:

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    int characterList = 0;
    char* dynamo = new char[1000];
    char* buffer = dynamo;
    ifstream input("wordlist.txt");
    if (input.is_open())
    {
        input >> dynamo[characterList];
        while (input.eof())
        {
            characterList++;
            input >> dynamo[characterList];
            cout << dynamo[characterList];
        }
    }
    else
    {
        cout << "File not opened" << endl;
    }
    return;
}

I'm a beginner so I do apologize if this looks like terrible coding practice. I created a text file with a quote from Bill Cosby that I'm trying to read one word at a time. The quote is "I don't know the key to success, but the key to failure is trying to please everybody." I'm trying to read one word at a time from a text document ignoring punctuation. I know there are a lot of questions similar to this, but they are using code that I have not learned so I'm sorry for having a repeating question. I have not learned getline (I used cin.getline) and #include <string>.

Edit: I forgot to mention, so I'm sorry for not doing so earlier, but I'm studying dynamic memory allocation which is why I'm using the new char[1000].

Rie Kumar
  • 37
  • 10
  • 1
    I'd recommend learning how to use C++ string abstraction. You might not have learned it yet, but now's the time. – duffymo Jan 07 '15 at 18:42
  • @Rie Kumar The code would be simple if you use std::vector instead of the array. – Vlad from Moscow Jan 07 '15 at 18:44
  • 2
    ... and fixed `while (input.eof())`, [which is wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – WhozCraig Jan 07 '15 at 18:45
  • @Rie Kumar As for you code then you have to declare a two-dimensional array each element of which would have the size that at least equal to the size of the maximum data in the file. – Vlad from Moscow Jan 07 '15 at 18:45
  • What is the problem you're having with this code currently? – David G Jan 07 '15 at 18:50
  • Working strings as buffers and pointers is unecessary and retrograde. This is C++ not C, use the tools it offers. – Havenard Jan 07 '15 at 19:17
  • Well, my instructor would dock me big time for using the std::string and the code is not outputting any of the text stored on the text file. – Rie Kumar Jan 07 '15 at 20:05

2 Answers2

2

I'd suggest you to use std::string instead of manually allocating buffers on the heap with new[] and trying to read text manually from the file into those buffers (and don't forget to free the buffer with proper delete[] calls!).

C++ input stream classes like std::ifstream can simply read text into std::string instances thanks to a proper overload of operator<<.
The syntax is as simple as:

    string word;
    while (inFile >> word)
    {
        cout << word << endl;
    }

Here's a complete compilable sample code for you to experiment and learn:

#include <fstream>
#include <iostream>
#include <string>
using namespace std;

int main()
{
    ifstream inFile("test.txt");
    if (inFile.is_open())
    {
        string word;
        while (inFile >> word)
        {
            cout << word << endl;
        }
    }
    else
    {
        cout << "Can't open file." << endl;
    }    
}

This is the output I got on a test text file having the content specified in your question:

I
don't
know
the
key
to
success,
but
the
key
to
failure
is
trying
to
please
everybody.

NOTE

Of course, once you have your words read into a std::string instance, you can store them in a container like std::vector<std::string>, using its push_back() method.

Mr.C64
  • 41,637
  • 14
  • 86
  • 162
  • I can't use #include & std::string. I really wish I could because from most of the answers so far it seems the easiest. Unfortunately, my instructor would dock me quite a bit of points for using something we haven't learned yet. – Rie Kumar Jan 07 '15 at 20:08
0

I would do something like this:

#include <iostream>
#include <string>
#include <fstream>

int main() {
  std::string array[6];
  std::ifstream infile("Team.txt");
  std::string line;
  int i = 0;
  while (std::getline(infile, line)) {
    array[i++] = line;
  }
  return 0;
}

based on this answer.

Here, we assume we have to read 6 lines from the file "Team.txt". We use std:::getline() and we put inside a while so that we read all the file.

At every iteration, line holds the current line of the file read. Inside the body we store it in array[i].

Community
  • 1
  • 1
gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • I can't use #include & std::string. I really wish I could because from most of the answers so far it seems the easiest. Unfortunately, my instructor would dock me quite a bit of points for using something we haven't learned yet. – Rie Kumar Jan 07 '15 at 20:08