Thread: Beginner Text File Input Question

  1. #1
    Registered User
    Join Date
    Nov 2006

    Question Beginner Text File Input Question

    I am trying to read a text file and store each different word seperately using an array and whitespace as a delimiter. Example: Input from file is "I wish I knew how to do this" and I want to store "I" as word[0], "wish" as word[1], "I" as word[2], "knew" as word[3], "how" as word[4] etc.

    Here is what I have so far:
    #include <iostream>
    #include <fstream>
    #include <string>
    using namespace std;
    void main()
    	string word[7];
    	int index = 0;
    	ifstream myfile("sentance.txt");
    	if (myfile.is_open())
    		while(! myfile.eof())
    			cin.get(myfile, word[index], ' ');
    			cout << word[index] << endl;
    when I compile I get:
    .\main.cpp(17) : error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem *,std::streamsize,_Elem)' : cannot convert parameter 1 from 'std::ifstream' to 'char *'
    No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    Any help is greatly appreciated. Thanks in advance for your time.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    >void main()
    int main(). There are no excuses not to declare main correctly.

    >while(! myfile.eof())
    This is a bad way to control your loop.

    >cin.get(myfile, word[index], ' ');
    This is where your error is coming from. What you probably wanted is:
    getline ( myfile, word[index], ' ' );
    If myfile is about to go out of scope anyway, there's no reason to close the stream. The destructor will handle it for you.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Nov 2006
    Thanks Prelude! It works great.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can someone help me with these errors please code included
    By geekrockergal in forum C Programming
    Replies: 7
    Last Post: 02-10-2009, 02:20 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 04:49 PM
  4. simulate Grep command in Unix using C
    By laxmi in forum C Programming
    Replies: 6
    Last Post: 05-10-2002, 04:10 PM
  5. Outputting String arrays in windows
    By Xterria in forum Game Programming
    Replies: 11
    Last Post: 11-13-2001, 07:35 PM