Home > database >  Strange behavior found in Sublime and Vim at EOF
Strange behavior found in Sublime and Vim at EOF

Time:12-22

Ι 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?

CodePudding user response:

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?

  • Related