Why the data does not print out with eclipse in unbuntu?

AI Thread Summary
The issue with printing data from the file "GANP00SVK_R_20160100000_01D_EN.rnx" in Eclipse on Ubuntu stems from differences in line endings between files. The problematic file uses carriage return and linefeed (CR + LF) endings, while the working file uses only linefeed (LF) endings, which Ubuntu expects. The original code does not exit the while loop correctly, as the break statement only exits the if condition, not the loop itself. Suggestions include using a boolean variable to control the loop and directly reading lines into a string to avoid character array issues. It is also recommended to check for hidden characters in the file using a HEX editor.
Nate Duong
Messages
125
Reaction score
4
Dear group,

I am using Eclipse in the Ubuntu operation system and trying to print out this data from the begin to the string "END OF HEADER" but I do not understand why it did not give me the one as I expected.

I also used this file "albe0320_1.17n" to test and it worked, but the file that I am trying to use is "GANP00SVK_R_20160100000_01D_EN.rnx" and did not work and the data still stucking in the while loop. 2 these data files have exactly the same format, but maybe different tail format, such as: ".17n" and "rxn" I do not know how to get this out.

If anyone whoever can help, I appreciated for that help!

Here are my code and attached data file.

Thank you very much.

Very best regards.
C:
    #include <iostream>
   #include <iomanip>
   #include <fstream>
   #include <cstring>
   #include <string>
   #include <sstream>
   #include <csignal>
   #include <cstdio>
   using namespace std;

   const int MAX_CHARS_PER_LINE = 81;

   nt main () {
  string filename = "GANP00SVK_R_20160100000_01D_EN.rnx";

   ifstream            fin;
   fin.open(filename.c_str());
   int head_lines = 0;
   std::string header_end =("END OF HEADER");
   char line[MAX_CHARS_PER_LINE];

   while (1) // scanning data per line until hit the message "END OF HEADER"
   {
       head_lines++;
       fin.getline(line, MAX_CHARS_PER_LINE);
       std:: string line1(line);
       std::size_t found = line1.find(header_end);

       if (found!=std::string::npos)
       {
           break;
       }
   }

  cout << "done!"<<endl;
  return 0;
}
 

Attachments

Last edited:
Technology news on Phys.org
Please use code tags when you include code. They look like this:
C:
// Your code
I have edited your post to include code tags.

Both files appear to be ordinary text files, but each line in the albexxx file ends with a LF (linefeed) character, while each line in the other file ends with carriage return and linefeed characters (CR + LF). Ubuntu is a Linux variety, and as such it expects each line in a text file to end with LF. The other file, with the CR + LF) line ends, is what Windows expects. Your C++ code is running under Linux (Ubuntu), so is expecting text files to adhere to the usual form for Linux text files.
 
You never exit the while loop. The break statement just exits the if statement.

Hint: Define a boolean variable more before the while. Set more = TRUE. Instead of while(1), use while(more). Set more=FALSE when you have found the header_end.
 
Svein said:
You never exit the while loop. The break statement just exits the if statement.

Hint: Define a boolean variable more before the while. Set more = TRUE. Instead of while(1), use while(more). Set more=FALSE when you have found the header_end.
@Svein : following you suggestion, I tried to put more condition for the while loop:
C:
int main() {
   string filename = "GANP00SVK_R_20160100000_01D_EN.rnx";   ifstream             fin;
   fin.open(filename.c_str());
   int noeph;
   int head_lines = 0;
   std::string header_end =("END OF HEADER");
   char line[MAX_CHARS_PER_LINE];

   while (fin.good()) // scanning data per line until hit the message "END OF HEADER"
   {
       head_lines++;
       fin.getline(line, MAX_CHARS_PER_LINE);
       std:: string line1(line);
       std::size_t found = line1.find(header_end);

       if (found!=std::string::npos)
       {
           break;
       }
   }
   cout << "headlines = " << head_lines<<endl;
   cout << "done!"<<endl;
}

The result I received was out of the while loop but it just read and stopped 1st line of the data and did not at the "END OF HEADER".

Do you have any ideal?

Thank you.
 
Last edited by a moderator:
Check out your files in a HEX editor. It's likely that there are hidden characters in your files.
 
  • Like
Likes Nate Duong
  • while (fin.good()) - what is the value of this before you have read anything?
  • if (found!=std::string::npos) { break; } does nothing! Either it does not match and skips the interior of the statement - or it matches and only skips right out again (break means jump to the end of this block - not the outer block!)
 
  • Like
Likes Nate Duong
Svein said:
  • while (fin.good()) - what is the value of this before you have read anything?
  • if (found!=std::string::npos) { break; } does nothing! Either it does not match and skips the interior of the statement - or it matches and only skips right out again (break means jump to the end of this block - not the outer block!)

This code obviously worked with the file "albe0320_1.17n" (attached) but I do not know why the file "GANP00SVK_R_20160100000_01D_EN.rnx" with the same but just different tail ".rnx" ?
 
newjerseyrunner said:
Check out your files in a HEX editor. It's likely that there are hidden characters in your files.
I agree with you, it maybe have some hidden characters in this file, but I still don't see additional characters on the HEX editor.
 
If you want to read a line into a std::string, you don't need to read it into an array of chars first. There is a version of getline() that reads directly into a std::string. It automatically makes the string long enough to accommodate the data, so you don't have to worry about overflow, as you do with an array.

C:
std::getline (fin, line1);

Also, a more idiomatic way to test for end of file in C++ is to use the input expression as the condition for the while() statement. In a Boolean context, (I think) all input operations evaluate as true or false depending on whether the input succeeded. Therefore if you need to stop only at the end of file (or if there is an input error), you would write something like this:

C:
std::string line1;
while (std::getline (fin, line1))
{
   // do something with line1
}

If you also want to stop when you encounter some other condition in your data, you can include another Boolean variable or expression in the while() statement:

C:
std::string line1;
bool some_condition = true;
while (some_condition && std::getline (fin, line1))
{
   // do something with line1
   // set some_condition = false if you want to exit the loop before end of file
}

Note the order of the conditions in the while() is important. The && operator does not evaluate the second condition if the first condition is false. So in this case, if some_condition becomes false inside the loop, the program will not try to read the next line.
 
Last edited:
Back
Top