Thread: Problems with getline

  1. #1
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35

    Problems with getline

    I have a program that wants to read in 30 strings from a text file and store each string in an element of an array.

    The array is declared as this...

    Code:
    char *command[MAX_COMMANDS]; //commands
    MAX_COMMANDS is defined as the number 30.


    Here is my function that loads the text file and tries to fill up the array...

    Code:
    int LoadCommands()
    {
      int i;
      ifstream file ("install.txt");
      
      if (!file.is_open())
        return ERROR_DOESNOTEXIST;
        
      //get the commands
      for (i=0; i<MAX_COMMANDS; i++)
      {
        file.getline(command[i],128);
      }
      
      //done here
      file.close();
      //double check
      if (file.is_open())
        file.close();
        
      return 1;
    }

    I know the answer must be obvious, but i can't figure out how to fix it. I want it so that each line in the text file can be of any length, blank or around 64 characters.

    The problem is, when the program gets to the file.getline point, it crashes with an illegal operation i presume to be caused by trying to read something that isn't there. Any help?
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

  2. #2
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    you could just do:
    Code:
    if(!file)
    but try this: (not sure if it will fix your problem though)
    Code:
    getline(file, command[i])
    is the error an access violation?

  3. #3
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35
    Tried that, compiler just gave an error. The problem is an illegal operation, invalid page fault.
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

  4. #4
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227

    Re: Problems with getline

    Originally posted by Ion Blade
    The array is declared as this...
    Code:
    char *command[MAX_COMMANDS]; //commands
    The problem is, when the program gets to the file.getline point, it crashes with an illegal operation i presume to be caused by trying to read something that isn't there. Any help?
    'command' is an array of pointers for which you seem to have neglected to allocate memory. So:
    Code:
    //get the commands
      for (i=0; i<MAX_COMMANDS; i++)
      {
      command[i]=new char[YOUR_STR_LEN + 1];
      file.getline(command[i],128);
      }
    and don't forget to delete[] each 'command' pointer when you're done.
    CProgramming FAQ
    Caution: this person may be a carrier of the misinformation virus.

  5. #5
    Registered User Ion Blade's Avatar
    Join Date
    May 2002
    Posts
    35
    ahhh

    i thought at one point i wasn't assiging a size to the commands, and couldn't figure out how to go about doing it then forgot it lol.

    anyway that worked, thanks
    "Im going to have peaceful dreams of brackets and semicolons strapped on crucifixes, screaming for mercy" - Someone who doesn't like programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 01-02-2009, 07:24 AM
  2. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  3. cin and getline() problems....
    By AdampqmabA in forum C++ Programming
    Replies: 3
    Last Post: 10-06-2008, 05:55 PM
  4. Problems with getline
    By MarlonDean in forum C++ Programming
    Replies: 5
    Last Post: 07-01-2008, 05:04 AM
  5. getline, while, string problems
    By Makachu in forum C++ Programming
    Replies: 7
    Last Post: 09-23-2007, 04:17 PM