4

My professor is very smart but expects complete noobs like me to just know how to program . I don't understand how the fstream function works.

I will have a data file with three columns of data. I will have to determine with a logarithm whether each line of data represents a circle, rectangle or triangle - that part is easy. The part I don't understand is how the fstream function works.

I think I:

#include < fstream > 

then I should declare my file object?

ifstream Holes;

then I open it:

ifstream.open Holes; // ?

I don't know what the proper syntax is and I can't find straightforward tutorial. Everything seems way more advanced than my skills can handle.

Also, once I've read-in the data file what is the proper syntax to put the data into arrays?

Would I just declare an array e.g. T[N] and cin the fstream object Holes into it?

Shahbaz
  • 46,337
  • 19
  • 116
  • 182
lprater1
  • 65
  • 1
  • 1
  • 7
  • 2
    Maybe check out a tutorial like this guy: http://www.cplusplus.com/doc/tutorial/files/ and ask us any specific questions you may have. – JoeFish Nov 24 '11 at 17:28
  • 2
    One question at a time, please! – Kerrek SB Nov 24 '11 at 17:29
  • 1
    @JoeFish: That tutorial is BS. Don't use it. [Beware of cplusplus.com](http://programmers.stackexchange.com/questions/88241/whats-wrong-with-cplusplus-com). – Kerrek SB Nov 24 '11 at 17:32
  • @KerrekSB Fair enough. Link to a better one? – JoeFish Nov 24 '11 at 17:50
  • 1
    @JoeFish: I actually searched around a bit before posting, deludedly thinking that there must be a good reference already. Unfortunately, everything I found was Total Garbage (e.g. MSDN), full of misleading advice and incorrect loops. For now, I'd recommend [this answer](http://stackoverflow.com/q/8260777/596781) :-) – Kerrek SB Nov 24 '11 at 17:52
  • @KerrekSB: +1 for self-reference :) – JoeFish Nov 24 '11 at 17:59

2 Answers2

10

Basic ifstream usage:

#include <fstream>   // for std::ifstream
#include <iostream>  // for std::cout
#include <string>    // for std::string and std::getline

int main()
{
    std::ifstream infile("thefile.txt");  // construct object and open file
    std::string line;

    if (!infile) { std::cerr << "Error opening file!\n"; return 1; }

    while (std::getline(infile, line))
    {
        std::cout << "The file said, '" << line << "'.\n";
    }
}

Let's go further and assume we want to process each line according to some pattern. We use string streams for that:

#include <sstream>   // for std::istringstream

// ... as before

    while (std::getline(infile, line))
    {
        std::istringstream iss(line);
        double a, b, c;

        if (!(iss >> a >> b >> c))
        {
            std::cerr << "Invalid line, skipping.\n";
            continue;
        }

        std::cout << "We obtained three values [" << a << ", " << b << ", " << c << "].\n";
    }
Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
1

Let me step through each part of reading the file.

#include <fstream> // this imports the library that includes both ifstream (input file stream), and ofstream (output file stream

ifstream Holes; // this sets up a variable named Holes of type ifstream (input file stream)

Holes.open("myFile.txt"); // this opens the file myFile.txt and you can now access the data with the variable Holes

string input;// variable to hold input data

Holes>>input; //You can now use the variable Holes much like you use the variable cin. 

Holes.close();// close the file when you are done

Please note that this example doesn't deal with error detection.

atkins
  • 1,963
  • 14
  • 27
Boundless
  • 2,444
  • 2
  • 25
  • 40