I'm trying to write something in a text document and extract its words and then print them but when ever i do so, it does print out the words except for the first word, then it infinitely starts printing out "\377" i think this has to do with the file not closing properly but I don't know how to close it properly, I teach my self c++ and i just discover this stuff on my own but this has confused me. I'm trying to write the letters S, D, T on a text document and then read them each as a individual to be them saved into a string to be then printed out.
//
// main.cpp
// test prep
//
// Created by Sylvain Jones on 2/10/14.
//
#include <iostream>
#include <fstream>
using namespace std;
void makeFile () {
fstream outfile("file.txt", fstream::out);
outfile << "S S D T 0";
outfile.close();
}
void readFile () {
ifstream file;
file.open("file.txt");
string word;
char x ;
word.clear();
int score, runners = 0;
int srunners[100];
while (file >> word) {
x = file.get();
while (x != 0) {
word = word + x;
x = file.get();
cout << x << endl;
if (x == ' ') {
if (word == "S") {
score = score + 1;
runners++;
srunners[runners] += 1;
}
else {
if (word == "D") {
score = score + 2;
runners++;
srunners[runners] += 2;
}
else {
if (word == "T") {
score = score + 3;
runners++;
srunners[runners] += 3;
}
}
}
}
}
}
}
int main(int argc, const char * argv[])
{
makeFile();
cout << "file made\n\n";
readFile();
}