Thread: runtime error, windows XP, borland C++ 5.5

  1. #1
    Registered User
    Join Date
    May 2002
    Posts
    51

    runtime error, windows XP, borland C++ 5.5

    I really can't understand why this code genereates a runtime error. The program actually outputs the text in the file and then the error occurs. Thankful for help.

    Oh, the file deck.txt looks like this:

    Code:
    diabolic edict#
    circle of protection#
    nantuko vigilante#
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <vector>
    
    using namespace std;
    
    void add(vector<string> &rDeck, char *card);
    void readfile(vector<string> &rDeck);
    
    int main()
    {
    vector<string> deck;
    readfile(deck);
    
    cout<<deck.at(0);
    cout<<deck.at(1);
    cout<<deck.at(2);
    
    return 0;
    }
    
    void add(vector<string> &rDeck, char *card)
    {
    string s;
    s.assign(card);
    rDeck.push_back(s);
    }
    
    void readfile(vector<string> &rDeck)
    {
    char *card;
    string s;
    ifstream fin;
    fin.open("deck.txt");
    
    	for(int i = 0;i < 3;i++)
    	{
    	fin.get(card, 40, '#');
    	fin.ignore();
    	add(rDeck, card);
    	}
    
    fin.close();
    }
    Last edited by finnepower; 08-05-2005 at 10:45 AM.
    I abuse:

    Borland C++ Builder 6 Enterprise Edition

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>char *card;

    Change that to:

    char card[40];
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    51
    Quote Originally Posted by JaWiB
    >>char *card;

    Change that to:

    char card[40];
    Well that was easy, it works. Can you tell me why the error occured in the first place? I might learn something.

    If you don't have time to do that: thanks anyway!
    I abuse:

    Borland C++ Builder 6 Enterprise Edition

  4. #4
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You were trying to copy information into unallocated memory on this line:
    Code:
    fin.get(card, 40, '#');
    You either needed to dynamically allocate the pointer as an array, or statically size it (which made more sense since you are already limiting the length that you read from the file on that line)

    Reading a good pointer tutorial might shed more light on the subject.
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  5. #5
    Registered User
    Join Date
    May 2002
    Posts
    51
    I understand now, thanks. But why did the program still output the text correctly? Isn't that impossible?
    I abuse:

    Borland C++ Builder 6 Enterprise Edition

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    The pointer pointed at some random place in memory, which just happened to allow you to modify it to hold the string you read from the file. You basically got lucky (or actually unlucky) that it was able to read in the string and not crash immediately.

    By the way, is there a reasaon you aren't using getline to read into the string directly?
    Code:
    getline(fin, s, '#');

  7. #7
    Registered User
    Join Date
    May 2002
    Posts
    51
    Quote Originally Posted by Daved
    By the way, is there a reasaon you aren't using getline to read into the string directly?
    No, but this isn't a really important program so I use anything that works. But I'll keep that in mind next time. I use the program to shuffle a deck of cards and then draw a hand. It makes things easier if you want to find out statistics for the deck.
    I abuse:

    Borland C++ Builder 6 Enterprise Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. Need help with program
    By HAssan in forum C Programming
    Replies: 8
    Last Post: 06-10-2007, 08:05 PM
  3. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  4. FILES in WinAPI
    By Garfield in forum Windows Programming
    Replies: 46
    Last Post: 10-02-2003, 06:51 PM
  5. Windows XP regression over time
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 12-17-2002, 10:49 AM