-1

I'm sorry for the initial post. This is tested and reproducible.

I'm trying to get cout to work within a fstream while loop while detecting each character its parsing, but it's exhibiting an odd behavior with the Text getting overrided by the first variable that I'm trying to put into cout.

main.cxx

#include <string.h>
#include <fstream>
#include <iostream>
#include <stdio.h>

using std::string;
using std::fstream;
using std::noskipws;
using std::cout;

int main(int argc, const char *argv[]){

    char pipe;
    string word; // stores the word of characters it's working on at the moment
    string filename = "Directory.dat";
    int type = 0; // 2 types counter, starts at 0
    int newindicator = 0; // for detecting a new * for the data set
    fstream fin(filename.c_str(), fstream::in);
    while(fin >> noskipws >> pipe){
        if(pipe == '*'){ // if the character is an asterisk
            type++;
            newindicator = 0;
            word.clear();
        }else if (pipe == '\n'){ // if the character is next line
            if(newindicator == 0){ // tells the reader to know that it just finished reading a *, so it doesn't print anything.
                newindicator = 1;
            }else {
                if(type == 1){ 
                    cout << "new word as: ";
                    cout << word << "\n";

                }else if (type == 2){ 
                    cout << "new word as: ";
                    cout << word << "\n";
                }
                word.clear(); // clears the word string as it's reading the next line.
            }
        }else{
            word+=pipe;
        }
    }
    return 0;
}

Directory.dat

*
Chan
Johnathan
Joespeh
*
Betty
Lady Gaga

Output

Chanword as:
new word as: Johnathan
new word as: Joespeh
Bettyord as:
new word as: Lady Gaga

Note that how "Chan" is overriding the characters "new " on the first line, but it's fine after that. This seems to happen on every new type I'm doing, and when its recalling a new set of type. Same with Betty on the next set, which overrides "new w" with "Betty" on that cout.

Any feedback would be much appreciated. Thank you!

M.M
  • 138,810
  • 21
  • 208
  • 365
Syeung
  • 1
  • 1
  • Your example is not reproducible. Besides, it is probably not minimal. – user31264 Dec 12 '16 at 05:24
  • 1
    Where is `pipe` declared? Please post a **complete** but minimal example. For now voting to close as lacking a reproducible example. – Cheers and hth. - Alf Dec 12 '16 at 05:29
  • 1
    One not onlikely reason for the behavior you see is that the `word` that should hold `"Chan"` instead holds `"\rChan"`, that is, a byte with value 13 at the start. – Cheers and hth. - Alf Dec 12 '16 at 05:33
  • Thank you, can you try again. I am sorry for the incomplete example – Syeung Dec 12 '16 at 05:33
  • 1
    @Syeung `word+=pipe;` -- Except for `word.clear()`, this is the only place where `word` is being modified. Why not just use your debugger and see exactly what characters you're adding to `word`? Are you putting in any effort in debugging your code, given that the issue is on one single line? – PaulMcKenzie Dec 12 '16 at 05:35
  • I see! Thank you! I was not aware of how to use \r properly to cout text. I will look up the behavior of this. Thank you. – Syeung Dec 12 '16 at 05:49
  • why do you use `using namespace std;` when you already have `using std::*` below? [Why is “using namespace std” considered bad practice?](http://stackoverflow.com/q/1452721/995714) – phuclv Dec 12 '16 at 06:16
  • @Cheersandhth.-Alf Thank you, I understand why it was doing that because of \r. Appreciate everyone's patience. – Syeung Dec 12 '16 at 06:40
  • Please do not edit the question to include a solution, or "solved" messages. I have rolled back that edit. Instead, accept a posted answer. If there are no answers posted you can write your own one based on the understanding you gained from the comments, and self-accept it. – M.M Dec 12 '16 at 06:42

2 Answers2

0

I suspect your input file has Windows line endings. These contain the carriage return character that's handled differently on Unix.

https://superuser.com/questions/374028/how-are-n-and-r-handled-differently-on-linux-and-windows

Community
  • 1
  • 1
NPE
  • 486,780
  • 108
  • 951
  • 1,012
0

Thank you all for the comments and feedback. Made the changes as suggested:

Corrected

#include <string.h>
#include <fstream>
#include <iostream>
#include <stdio.h>


using std::string;
using std::fstream;
using std::noskipws;
using std::cout;

int main(int argc, const char *argv[]){

    char pipe;
    string word; // stores the word of characters it's working on at the moment
    string filename = "Directory.dat";
    int type = 0; // 2 types counter, starts at 0
    int newindicator = 0; // for detecting a new * for the data set
    fstream fin(filename.c_str(), fstream::in);
    while(fin >> noskipws >> pipe){
        if(pipe == '*'){ // if the character is an asterisk
            type++;
            newindicator = 0;
            word.clear();
        }else if (pipe == '\n'){ // if the character is next line
            if(newindicator == 0){ // tells the reader to know that it just finished reading a *, so it doesn't print anything.
                newindicator = 1;
            }else {
                if(type == 1){ 
                    cout << "new word as: ";
                    cout << word << "\n";

                }else if (type == 2){ 
                    cout << "new word as: ";
                    cout << word << "\n";
                }
                word.clear(); // clears the word string as it's reading the next line.
            }
        }else{
            if (pipe != '\r'){
                word+=pipe;
            }
        }
    }
    return 0;
}

Output

new word as: Chan
new word as: Johnathan
new word as: Joespeh
new word as: Betty
new word as: Lady Gaga
Syeung
  • 1
  • 1