Reading in a Char*[] from file

This is a discussion on Reading in a Char*[] from file within the C++ Programming forums, part of the General Programming Boards category; Hey. I"m trying to read a file of words into the array char* array[80] and I can't seem to make ...

  1. #1
    Registered User
    Join Date
    Dec 2002
    Posts
    28

    Reading in a Char*[] from file

    Hey. I"m trying to read a file of words into the array char* array[80] and I can't seem to make it work. I keep getting runtime errors when I use this:

    Code:
    char* word[80];
    char str[80];
    
    while(b_file.eof()==0)
    	{
    		b_file>>str;
    
    		len = strlen(str);
    		word[i] = new (char[len]);
    		
    		word[i++] =str;
    }

    What am I doing wrong?

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,506
    > word[i] = new (char[len]);
    You forgot to count the \0
    so its
    word[i] = new (char[len+1]);

    > word[i++] =str;
    Use strcpy
    strcpy( word[i++], str );

    > while(b_file.eof()==0)
    Make sure you don't go too far
    while( i < 80 && b_file.eof()==0)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  3. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 04:02 AM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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