0

Ι have a file called position.txt which has been formatted in a particular fashion. This is the content of position.txt:

START POLYMER 1
1 0 0 
2 0 0
3 0 0
4 0 0
4 1 0
5 1 0
5 0 0 
6 0 0 
END POLYMER 1

However, when I look at position.txt using vi, I see this:

enter image description here

When I look at positions.txt in sublime, I see:

enter image description here

Clearly, there seems to be an additional line added in Sublime, which is not even visible in Vi. This shouldn't be a problem, but I am running the following code:

int Grid::ExtractNumberOfPolymers(std::string filename){
    std::regex start("START");
    std::regex end ("END");
    std::ifstream myfile (filename);

    if ( !myfile ){
        std::cerr << "File named " << filename << " could not be opened!" << std::endl;
        exit(EXIT_FAILURE);
    }

    std::string myString;
    // std::vector <std::string> StartContents; 
    // std::vector <std::string> EndContents; 

    int numberOfStarts {0}, numberOfEnds {0};

    if (myfile.is_open() && !myfile.eof() ) {
        while (myfile.good()) {
            std::getline(myfile, myString); // pipe file's content into stream 

            if (std::regex_search(myString, start)){
                numberOfStarts++;
            }
            else if (std::regex_search(myString, end)){
                numberOfEnds++;
            }
            else if ( myString.empty()){
                std::cerr << "ERROR: Empty line found. Bad positions file. " << std::endl;
                exit (EXIT_FAILURE);
            }
        }
    }

    if (numberOfStarts==numberOfEnds){
        return numberOfStarts;
    }
    else {
        std::cerr << "Number of starts is not the same as number of ends. Bad input file." << std::endl;
        return 0;
    }

}

When I ran this code, I was exiting with the

std::cerr << "ERROR: Empty line found. Bad positions file. " << std::endl;
exit (EXIT_FAILURE);

error. I could not locate the error until I opened positions.txt and DELETING the final line. What is going wrong here? Why is Vim showing something like this?

I need the empty line error message because I do not want an interruption in the input file. I want coordinates at each line to not corrupt other parts of the code.

Any advice you have for me will be appreciated.

edit: using xxd on positions.txt, I get the following

[faraway_server] src $ xxd positions.txt 
00000000: 5354 4152 5420 504f 4c59 4d45 5220 310a  START POLYMER 1.
00000010: 3120 3020 3020 0a32 2030 2030 0a33 2030  1 0 0 .2 0 0.3 0
00000020: 2030 0a34 2030 2030 0a34 2031 2030 0a35   0.4 0 0.4 1 0.5
00000030: 2031 2030 0a35 2030 2030 200a 3620 3020   1 0.5 0 0 .6 0 
00000040: 3020 0a45 4e44 2050 4f4c 594d 4552 2031  0 .END POLYMER 1
00000050: 0a         

What should I do next?

bad_chemist
  • 283
  • 1
  • 7
  • Dump the file with `xxd` or some similar hexdump tool. – hobbs Dec 21 '21 at 05:00
  • @hobbs, thank you for your response. I have no idea what ```xxd``` means, looking it up right now. What is the nature of the problem here? – bad_chemist Dec 21 '21 at 05:02
  • Your output from `xxd` shows that there is no extra newline in the file. It ends with one `0x0a` (`\n`). – Ted Lyngmo Dec 21 '21 at 05:11
  • Thank you for your comment @TedLyngmo. But... why is the newline not showing up in vi? How do I correct for it? – bad_chemist Dec 21 '21 at 05:13
  • `vi` shows it like it should. The problem (if it's a problem) is in sublime that shows a line that isn't there. – Ted Lyngmo Dec 21 '21 at 05:15
  • It's because the line ending styles in vi and sublime are different. `vi` is using CRLF while `sublime` is using LF. – Nguyen Thai Binh Dec 21 '21 at 05:17
  • @NguyenThaiBinh `vi` only does that in dos mode. – Ted Lyngmo Dec 21 '21 at 05:22
  • @bad_chemist, if you are interested by [an explanation](https://stackoverflow.com/a/16224292/546861). In short, the extra line shown by ST is not in your file, it is just an artefact of how it interprets EOL. – romainl Dec 21 '21 at 06:39

1 Answers1

3

There is only one newline (\n) after the last line with text so the fact that you read one empty line is due to a bug in your program.

This:

    while (myfile.good()) {
        std::getline(myfile, myString);

should be this:

    while (std::getline(myfile, myString)) {

See Why is iostream::eof() inside a loop condition (i.e. while (!stream.eof())) considered wrong?

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108