0

I have this program compiled on latest macOS to read strings from a file.

commands I use: clang++ -std=c++17 -stdlib=libc++ sc_test.cc

followed by: ./a.out document1_short.txt

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

int testSpellingWrapper(int argument_count, char** argument_list) {
     const string document_filename(argument_list[1]);
     ifstream indoc;
     string sentence;
     indoc.open(document_filename);
     while(getline(indoc, sentence)){
          cout<<sentence<<"111"<<endl;
     }
     indoc.close();
     return 0;
}

int main(int argc, char **argv) {
  testSpellingWrapper(argc, argv);
  return 0;
}

However, it does not cout properly. The 111 shows up on top of the sentence covering the first two letters. I have to cout the strings along with something else for a programming assignment.

text from document

 This is a comlete transcript of Patel's decision, which was
 issued orally, in which she explains the basis for granting
 the injunction. Because it marks one of the first times a
 judge has issued a deciasion in a case of online
 music-swapping, Patel's interpretation of the lwa is likely
 to be widely cited in subsequent cases and will be crucial
 to the Court of Appeals as it grapples with the case.

output

111is is a comlete transcript of Patel's decision, which was
111sued orally, in which she explains the basis for granting
111e injunction. Because it marks one of the first times a
111dge has issued a deciasion in a case of online
111sic-swapping, Patel's interpretation of the lwa is likely
111 be widely cited in subsequent cases and will be crucial
111 the Court of Appeals as it grapples with the case.
  • 1
    I recommend crafting a [mcve] to help you sort this out. If making the MRE doesn't reduce the noise enough for you to see and fix the problem, edit the question to add the MRE. – user4581301 Apr 02 '20 at 22:26
  • 2
    Your input file has dos line endings. Check if `sentence.back() == '\r'` or similar. – KamilCuk Apr 02 '20 at 22:33

1 Answers1

1

(I'm assuming you're not running on Windows or DOS.)

What's happening is that your input file lines end with the sequence of characters 0x13 0x10, or "carriage return" and "line feed". This is DOS-style line ending, which tells an old-time printer to: 1. Move the printing head to the start of the line. 2. Move down by one line ("feed" paper).

Now, std::getline() gets rid of the 0x10 character (line feed, or newline on most operating systems), but you get the "carriage return" character in your sentence. When you print it, you move back to the start of the line before printing the last two characters, the "aa".

So, the program is doing what you told it too...

Suggestion: Do one of the following:

  • Run dos2unix on your input files
  • Generate your input files differently
  • Remove \r's (0x13, carriage return) characters from the end of your sentence's.

See also: Getting std :: ifstream to handle LF, CR, and CRLF?

Edit: Here on the site, the CRLF pairs you pasted appear as pairs-of-line-endings - a different "interpretation".

einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • 1
    If the code were running on Windows, `ifstream` in text mode (its default mode) would normalize the CRLF line breaks to LF before `std::getline()` could see them. – Remy Lebeau Apr 02 '20 at 22:40
  • @RemyLebeau: Ok, but - that's not what's happening. – einpoklum Apr 02 '20 at 22:41
  • your explanation is plausible, even likely. But since we don't actually know what platform the OP is running their code on, this is just an (educated) assumption at this point. – Remy Lebeau Apr 02 '20 at 22:41
  • I used sentence.back() and it is \r endings causing the problem. Should I delete the \r ending entirely or replace it with \n? – user13204465 Apr 02 '20 at 23:08
  • @user13204465 The real solution is to convert your file to the text file that your runtime expects to see. If you open a file in text mode, the runtime is trusting that the file is truly a text file. If it isn't, then you're opening yourself up for some weird behavior happening. – PaulMcKenzie Apr 03 '20 at 00:36
  • @user13204465: Depends on what you want to have in those strings. Probably delete it. – einpoklum Apr 03 '20 at 08:17