2

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();
}

4 Answers4

1

The reason you're reading '\377' is that octal 377 is decimal 255.
You've reached the end of the file.

std::istringstream file("S S D T 0");
std::string s;
int x;
while (file >> s) { 
  // reads into s until either the next char is whitespace, or EOF is hit.
  // First s is "S"; x = file.get(); x is ' ' until EOF
}

Instead, you can try reading into a char and skipping the space:

while (int x; file >> x >> std::ws) {
  switch (x) {
  case 'S': //...
    break;
  case 'D': //...
    break;
  case 'T': //...
    break;
  default:  //...
  }
}
viraltaco_
  • 814
  • 5
  • 14
  • Lets say I'm trying to store the text of a file into a string, is there a away to do that with whitespaces without getting the '\337' at the end? I need it in order to convert it into binary and if whitespaces get eliminated, my binary numbers will be inaccurate. Appreciate the explanation though. – Adan Vivero Apr 02 '22 at 08:53
  • @AdanVivero That is a different question. OP clearly wants to read them as individual words without whitespaces. If you have a different problem, post you own question. Otherwise people have no idea what you're looking for. – VLL Apr 05 '22 at 10:07
  • My apologies, I just didn't want to ask a duplicate question. Ya dig? – Adan Vivero Apr 05 '22 at 22:07
1

Here, this works as you wish

//
//  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;
    x = file.get();
    word.clear();
    int score, runners = 0;
    int srunners[100];

    do{
        do {
            word = word + x;
            cout << x << endl;
            x = file.get();
            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;
                        }
                    }
                }
            }
        }while (x > 0);
    } while (file >> word);
}

int main(int argc, const char * argv[])
{
    makeFile();
    cout << "file made\n\n";
    readFile();
}
Raul Jerlach
  • 167
  • 2
  • [A code-only answer is not high quality](//meta.stackoverflow.com/questions/392712/explaining-entirely-code-based-answers). While this code may be useful, you can improve it by saying why it works, how it works, when it should be used, and what its limitations are. Please [edit] your answer to include explanation and link to relevant documentation. – Muhammad Mohsin Khan Apr 06 '22 at 12:06
0

Make a note.

int get(); // Reads a single character and returns int not char

returns an integer not a character.

check the documentation http://www.cplusplus.com/reference/istream/istream/get/

So always check for -1 to stop reading the file your second while loop in readFile method should be like below

while ((x = file.get()) != EOF){

}

EOF is defined as -1

#define EOF (-1)
Sadanand
  • 1,080
  • 3
  • 13
  • 30
-1

I`m not sure about

while (file >> word)

Maybe you should use

while (!file.eof())
  • 3
    See http://stackoverflow.com/questions/5837639/eof-bad-practice for why this is a bad answer. –  Feb 11 '14 at 08:03