Beginner Text File Input Question

This is a discussion on Beginner Text File Input Question within the C++ Programming forums, part of the General Programming Boards category; I am trying to read a text file and store each different word seperately using an array and whitespace as ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    2

    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:
    Code:
    #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;
    			index++;
    		}
    		myfile.close();
    	}
    
    	system("pause");
    }
    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 *'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<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
    Posts
    9,796
    >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:
    Code:
    getline ( myfile, word[index], ' ' );
    >myfile.close();
    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
    Posts
    2
    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, 01:20 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. struct question
    By caduardo21 in forum Windows Programming
    Replies: 5
    Last Post: 01-31-2005, 03: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, 06:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21