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

Click For Summary

Discussion Overview

The discussion revolves around a programming issue encountered while using Eclipse on Ubuntu to read data from a specific file. Participants are trying to understand why the program does not exit a while loop as expected when processing the file "GANP00SVK_R_20160100000_01D_EN.rnx," despite a similar file working correctly. The focus is on code behavior, file formats, and potential hidden characters affecting the reading process.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant notes that the two files have the same format but may differ in line endings, with one using LF and the other CR + LF, which could affect how the program reads them.
  • Another participant points out that the while loop does not exit correctly, suggesting the use of a boolean variable to control the loop instead of an infinite loop.
  • A participant suggests checking the files in a HEX editor for hidden characters that might be causing issues.
  • Some participants discuss the use of std::getline() directly with std::string to avoid potential overflow issues with character arrays.
  • There is a mention of the importance of the order of conditions in the while loop to ensure proper evaluation and exit conditions.

Areas of Agreement / Disagreement

Participants express differing views on the cause of the issue, with some attributing it to file format differences and others suggesting code logic errors. No consensus is reached on the exact reason for the problem.

Contextual Notes

There are unresolved questions regarding the handling of different line endings and potential hidden characters in the files, as well as the effectiveness of the proposed code modifications.

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;
       }
   }

  count << "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;
       }
   }
   count << "headlines = " << head_lines<<endl;
   count << "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   Reactions: 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   Reactions: 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:

Similar threads

  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 11 ·
Replies
11
Views
2K
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
7K
  • · Replies 6 ·
Replies
6
Views
3K