Using a Files Contents

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kcddoorman
    New Member
    • Nov 2006
    • 76

    #1

    Using a Files Contents

    I'm learning C++ and I have hit a road block. I have started to play around with file I/O and I have a program that I want to make.

    My Internet is very flakey were I work so I was going to make a program that will tell me when the internet is working and when it's offline.

    The plan is to use ping continuously on a target like google and output the results to a file, ping.txt. This is easy to do even for me.

    Now, I can read the file and output to the file using c++, but what I want is only the last line of the file. It will say one of the following; host unreachable, timeout, our reply from. Based on this string from the last line of the file I want to create an action such as Internet is ON or Internet is OFF. It also has to run in a loop so that the program updates with each new entry into the file. I think I can do everything except for getting the last line of the ping.txt file into the program.
    this syntax is wrong but it gives you an idea of what I want:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
    char state [56];
    char pass [56];
    pass[56] =("Reply from ........");
      ofstream myfile;
      myfile.open ("ping.txt", ios::in); 
      myfile.gotoEOF
      myfile.readTheLastLine >> state [56];
      if state[56]=pass[56]
      then internet is working
      else internet is broke
    thanks for any help,
    James
  • phiefer3
    New Member
    • Jun 2007
    • 67

    #2
    Originally posted by kcddoorman
    I'm learning C++ and I have hit a road block. I have started to play around with file I/O and I have a program that I want to make.

    My Internet is very flakey were I work so I was going to make a program that will tell me when the internet is working and when it's offline.

    The plan is to use ping continuously on a target like google and output the results to a file, ping.txt. This is easy to do even for me.

    Now, I can read the file and output to the file using c++, but what I want is only the last line of the file. It will say one of the following; host unreachable, timeout, our reply from. Based on this string from the last line of the file I want to create an action such as Internet is ON or Internet is OFF. It also has to run in a loop so that the program updates with each new entry into the file. I think I can do everything except for getting the last line of the ping.txt file into the program.
    this syntax is wrong but it gives you an idea of what I want:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main () {
    char state [56];
    char pass [56];
    pass[56] =("Reply from ........");
      ofstream myfile;
      myfile.open ("ping.txt", ios::in); 
      myfile.gotoEOF
      myfile.readTheLastLine >> state [56];
      if state[56]=pass[56]
      then internet is working
      else internet is broke
    thanks for any help,
    James
    I would say a while loop would work, something like this:

    while (!myfile.eof())
    getline(myfile, state[56]);

    just start reading at the start of the file, and then read in each line. When you reach the end of the file, state will have the last line. (get rid of the statement to go to the end of the file, as that will go right past the last line). And unless you have a reason to keep the previous ping results, just have it rewrite the file each time, to reduce the file length. (I'm not sure how many lines the ping results would be)

    Comment

    • kcddoorman
      New Member
      • Nov 2006
      • 76

      #3
      If I run it all day then there will be many many lines.

      Comment

      • phiefer3
        New Member
        • Jun 2007
        • 67

        #4
        Originally posted by kcddoorman
        If I run it all day then there will be many many lines.
        That's why I said that you should have it overwrite the file for every ping. That way it will only ever have the amount of lines as a single ping result.

        If you want to keep track of all of the results, I would suggest that after the ping, and finding the result (last line) store the last line of each result in another file.

        Unless there's a reason for you to keep the rest, just overwrite them.

        Comment

        Working...