2

I would like to open a file and read a line from it. There will be only one line in the file so I don't really need to worry about looping, although for future reference it would be nice to know how to read multiple lines.

int main(int argc, const char* argv[]) {

    // argv[1] holds the file name from the command prompt

    int number = 0; // number must be positive!

    // create input file stream and open file
    ifstream ifs;
    ifs.open(argv[1]);

    if (ifs == NULL) {
        // Unable to open file
        exit(1);
    } else {
        // file opened
        // read file and get number
        ...?
        // done using file, close it
        ifs.close();
    }
}

How would I do this? Also, am I handling the file open correctly in terms of successful open?

Thanks.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Hristo
  • 45,559
  • 65
  • 163
  • 230
  • 2
    Do you have a C++ book? If so, have you looked in its chapter discussing the standard I/O library? If not, I'd strongly recommend getting one of the introductory books listed in [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/). – James McNellis Jul 25 '10 at 02:04
  • I don't have a C++ book. I was looking at http://www.cplusplus.com/reference/iostream/istream/getline/ and I was confused by the `streamsize n` parameter. – Hristo Jul 25 '10 at 02:29
  • If you click to the `streamsize` page, it says: "The type is an implementation-defined synonym of one of the signed basic integral types (generally signed int or signed long)." – John Kugelman Jul 25 '10 at 05:30
  • 1
    `n` is the size of the buffer that `s` points to. If you use the global `getline` instead of the member `getline`, then you don't have to worry about that parameter, because global `getline` reads into a `std::string` object rather than a buffer, and `std::string` objects are resizeable. – Steve Jessop Jul 25 '10 at 10:01

2 Answers2

5

A couple of things:

  1. You can read a number with the >> stream extraction operator: ifs >> number.

  2. The standard library function getline will read a line from a file, if you want a full line of text.

  3. To check if the file opened, just write if (ifs) or if (!ifs). Leave out the == NULL.

  4. You don't need to explicitly close the file at the end. That will happen automatically when the ifs variable goes out of scope.

Revised code:

if (!ifs) {
    // Unable to open file.
} else if (ifs >> number) {
    // Read the number.
} else {
    // Failed to read number.
}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

For what you're doing here, simply:

ifs >> number;

Will extract a number from the stream and store it in 'number'.

Looping, depends on the content. If it was all numbers, something like:

int x = 0;
while (ifs >> numbers[x] && x < MAX_NUMBERS)
{
 ifs >> number[x];
 x++;
}

Would work to store a series of numbers in an array. This works because extraction operator's side effect is true if the extraction succeeds, or false if it fails (due to end of file or disk errors, etc.)

Iain
  • 985
  • 2
  • 9
  • 20