
-----------------------------------
ssy
Sun Jun 20, 2010 7:14 pm

Linking a File to a C++ Program
-----------------------------------
I have to write a C++ program that opens a file and counts all the whitespace separated words in that file.

I am using GNU C++ compiler on Mac, using the terminal instead of an IDE.  What command do I use to link a file to be opened in my C++ program at the command line?  And I don't have to use an ifstream object right?  Since I am not really opening up a file from within my program?  Kind of confused...

-----------------------------------
TerranceN
Sun Jun 20, 2010 7:43 pm

RE:Linking a File to a C++ Program
-----------------------------------
Usually you don't link the file to your program, you open it with ifstream. ifstream does not require the file to be "within [your] program." Though I'm pretty sure you can include any data you want in your .exe, I just don't have a clue how to do it.

I'm sure this could explain it a lot better.

-----------------------------------
ssy
Sun Jun 20, 2010 9:39 pm

Re: Linking a File to a C++ Program
-----------------------------------
Thanks for the reply, but the reason I ask is because this program is supposed to be able to open ANY text files within the same directory.  All the example codes I have seen only opens a specific file.

This is supposed to be the answer:
    
#include 
#include 
using namespace std;

int main() {
   int nwords = 0;
   string word;

   while (cin >> word) {
      ++nwords;
   }

   cout 