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

In summary, the conversation is about using Eclipse in the Ubuntu operating system to print out data from a file, but the program does not give the expected output. The issue seems to be with the different file formats, with one file working and the other getting stuck in a while loop. Suggestions were made to check for hidden characters and to use a boolean variable in the while loop.
  • #1
Nate Duong
126
3
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

  • copy2.zip
    107.6 KB · Views: 180
Last edited:
Technology news on Phys.org
  • #2
Please use code tags when you include code. They look like this:
[code=c]
// Your code
[/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.
 
  • #3
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.
 
  • #4
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:
  • #5
Check out your files in a HEX editor. It's likely that there are hidden characters in your files.
 
  • Like
Likes Nate Duong
  • #6
  • 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
  • #7
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" ?
 
  • #8
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.
 
  • #9
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:

1. Why is my data not printing out with Eclipse in Ubuntu?

There could be several reasons for this issue. Some common causes include incorrect printer settings, outdated printer drivers, or a problem with the printing software. It is also possible that there is an issue with the connection between your computer and the printer.

2. How can I fix the issue of not being able to print data from Eclipse in Ubuntu?

The first step to resolving this issue is to check your printer settings. Make sure that the correct printer is selected and that the print quality, paper size, and other settings are correct. If the settings appear to be correct, try updating your printer drivers or reinstalling the printing software. It may also be helpful to restart both your computer and printer.

3. Is it possible that my printer is not compatible with Eclipse in Ubuntu?

It is possible that there may be compatibility issues between your printer and Eclipse in Ubuntu. Check the documentation for both your printer and Eclipse to see if there are any known compatibility issues. You may also want to try using a different printer to see if the issue persists.

4. Could the issue be related to my operating system?

While it is unlikely, it is possible that there may be a problem with your operating system that is preventing data from printing in Eclipse. You can try updating your operating system or using a different one to see if the issue is resolved. If the problem persists, it is more likely that there is an issue with your printer or printer settings.

5. How can I prevent this issue from occurring in the future?

To prevent this issue from happening again, make sure to regularly check and update your printer drivers and software. It is also important to double-check your printer settings before printing. If the issue continues to occur, you may want to consider using a different printing software or contacting the manufacturer of your printer for further assistance.

Similar threads

  • Programming and Computer Science
Replies
5
Views
811
  • Programming and Computer Science
Replies
4
Views
5K
  • Programming and Computer Science
Replies
6
Views
2K
  • Science and Math Textbooks
Replies
1
Views
923
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Programming and Computer Science
Replies
6
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
6K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Programming and Computer Science
Replies
16
Views
3K
  • Programming and Computer Science
Replies
13
Views
6K
Back
Top