So here's a pretty basic question. How do I access a file using C++? I want to be able to have the text file typed into the window of the program. I want to be able to write a string variable to a file, too.

And another question, maybe not as basic: How do I put a timer function into a program? Every n seconds I want the program to check if a variable has changed.

-M

C++ only natively supports file access through iostreams. You create an object of the *fstream family of classes and open them using a file name and orientation. To open a file for reading you would probably do this:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
  std::ifstream file("file");

  if (file.is_open()) {
    std::string s;

    while (getline(file, s))
      std::cout << s << '\n';
  }
}

To write to a file, you use ofstream instead of ifstream, and the corresponding operators:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
  std::ifstream file("file");
  std::ofstream log("log");

  if (file.is_open()) {
    std::string s;

    while (getline(file, s)) {
      std::cout << s << '\n';
      log << s << '\n';
    }
  }
}

Along with unidirectional streams, you can also have a bidirectional stream that allows input and output with intervening flushes or seeks:

#include <iostream>
#include <fstream>
#include <string>

int main()
{
  std::fstream file("file", std::ios::in | std::ios::out);

  if (file.is_open()) {
    file << "First line\nSecond line" << '\n';
    file << "Third line" << '\n';

    file.seekg(0, std::ios::beg);

    std::string s;

    while (getline(file, s))
      std::cout << s << '\n';
  }
}

Once opened, file streams are used just like cin and cout unless they're bidirectional. In that case you also have to consider the flush or seek when you change direction so that the stream buffer is synchronized. Otherwise, even the bidirectional file streams are used just like cin and cout.

Timers are tricky. If you need good precision, ie. REALLY every N seconds rather than just an approximation of every N seconds, then you'll probably have to resort to some platform dependent function like the Windows API Sleep() or Unix sleep(). You can get pretty good precision with the standard C++ library <ctime>, but it requires a busy loop using the tm struct. Otherwise you're just guessing or relying on a non-portable representation of time_t. I'd recommend just using a platform dependent function. :)

For the timmer try writting TSR's. TSR's are program that always resides in the memory once executed. These are very small but tricky codes and are useful when we want them to run on some events. Try a site funducode.com and search for TSR's.

If you write some TSR's on your own then it will very easy and useful for u to capture events

If you simply want the program to do something every N seconds, just have it sleep ( N ) seconds. (On unix it's sleep(seconds), on windows it's Sleep ( milliseconds)

>> For the timmer try writting TSR's.
A TSR is only relevant for DOS and other single-tasking operating systems. In a modern OS you would use either threads or separate processes.

This article has been dead for over six months. Start a new discussion instead.