Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

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

  1. Feb 13, 2017 #1
    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.
    Code (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;
    }
     
     

    Attached Files:

    Last edited: Feb 13, 2017
  2. jcsd
  3. Feb 13, 2017 #2

    Mark44

    Staff: Mentor

    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.
     
  4. Feb 13, 2017 #3

    Svein

    User Avatar
    Science Advisor

    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.
     
  5. Feb 13, 2017 #4
    @Svein : following you suggestion, I tried to put more condition for the while loop:
    Code (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: Feb 13, 2017
  6. Feb 13, 2017 #5
    Check out your files in a HEX editor. It's likely that there are hidden characters in your files.
     
  7. Feb 13, 2017 #6

    Svein

    User Avatar
    Science Advisor

    • 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!)
     
  8. Feb 13, 2017 #7
    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" ?
     
  9. Feb 13, 2017 #8
    I agree with you, it maybe have some hidden characters in this file, but I still dont see additional characters on the HEX editor.
     
  10. Feb 14, 2017 #9

    jtbell

    User Avatar

    Staff: Mentor

    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.

    Code (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:

    Code (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:

    Code (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: Feb 15, 2017
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Why the data does not print out with eclipse in unbuntu?
  1. Print space (Replies: 8)

Loading...