Continued issues with Winows Errors when running program

This is a discussion on Continued issues with Winows Errors when running program within the C Programming forums, part of the General Programming Boards category; Hello. I'm continuing to get 'myprogram has encountered an error and needs to close' windowsmessages when trying to run any ...

  1. #1
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33

    Continued issues with Winows Errors when running program

    Hello. I'm continuing to get 'myprogram has encountered an error and needs to close' windowsmessages when trying to run any of my programs compiled in C. The current one is this:

    Code:
    /* References:
    C by Example
    C in a Nutshell
    C Primer 5th Edition
    Stanford CS Guide to Linked Lists
    C Reference Library (Functions)
    */
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    typedef struct filelist
    {
      int val;
      char words[1024];
      struct filetext *next;
    } filetext_t;
    // Create a structure to hold words and frequency, with sub-structure or next pointer
    
    filetext_t *AddToList ( filetext_t *head, char val2[] )
    {
    // Function, create holder for current word
      filetext_t *current = NULL;
    // Set an initialising pointer set to NULL
    
      if ( head == NULL ) {
        head = malloc ( sizeof *head );       
        head->next = NULL;
      }
    // If head pointer is NULL, free memory and link to next pointer
      else {
        current = malloc ( sizeof *current->next );
        current->next = head;
        head = current;
      }
    // Otherwise first node is created so create next node
    
      strcpy ( head->words, val2 );
    
      return head;
    }
    // Copy the string from the head pointer into the container
    
    void PrintList ( wordList_t *head )
    {
      while ( head != NULL ) {
        printf ( "%s ", head->words );
        head = head->next;
      }
    // Display held word, advance pointer to next node
    
      printf ( "\n" );
    }
    // Move carriage to new line
    
    int main ( void )
    {
      char c;
      int i, j = 0;
      char val3[1024];
      filetext_t *list = NULL;
    // Create variables for reading from files and container for words
    
      FILE *fp = fopen ( "example.txt", "r" );
    // Open example file doing away with user input
    
      for ( i = 0; ( c = fgetc ( fp ) ) != EOF; i++ ) {
        if ( isalpha ( c ) )
          val3[j++] = tolower ( c );
    // Copy alphabetic characters into the val3 container
        else {
          val3[j++] = '\0';
          list = AddToList ( list, val2 );
          j = 0;
        }
    // Otherwise insert blank space to seperate words
      }
    
      PrintList ( list );
      fclose ( fp );
    
      return 0;
    // Close open file, and print through list
    }
    I try and run it and I get the error. Also, I strongly believe my malloc functions are corect, but the compiler things they're wrong. I looked in the Standord Linked List guide (which I found to be really useful and would strongly recommend to those like myself who are learning C) but it also shows similar syntax.

    Any help with this would be much appreciated. I just can't wrk out what's wrong and why Windows keeps complaining. I can't even test for correct output as I can't run it in the first place!

    Thanks.

    Hussein.

  2. #2
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    If that code compiles, then you've got issues with your compiler, or you haven't supplied all the code. I direct your attention to the following:

    Code:
    typedef struct filelist
    {
      int val;
      char words[1024];
      struct filetext *next;
    } filetext_t;
    // Create a structure to hold words and frequency, with sub-structure or next pointer
    
    .
    .
    .
    
    void PrintList ( wordList_t *head )
    {
      while ( head != NULL ) {
        printf ( "%s ", head->words );
        head = head->next;
      }
    // Display held word, advance pointer to next node
    
      printf ( "\n" );
    }
    // Move carriage to new line
    
    int main ( void )
    {
    
    .
    .
    .
    
        else {
          val3[j++] = '\0';
          list = AddToList ( list, val2 );
          j = 0;
        }
    .
    .
    .
    
    }
    Figure out what's going on there, and I'll also be very nice and give you a couple of pointers:

    1. fgetc returns int
    2. you should always check to see if a file stream opened before using it - like checking the return value of fopen
    3. If the compiler says that your calls to malloc are wrong, make sure you are compiling the code as C and not C++
    Last edited by Richie T; 11-12-2006 at 04:51 PM.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  3. #3
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Oh my God!

    sorry about that, but I never realised just changing the name of my typedef would have such an effect - how could that have been causing the malloc errors when the compiler didn't even mention anything about it? Actually, I only just found that when I was half way through writing my response here.

    Done: Added a test for the file open command
    Done: Added explicit definition for val2[]
    Done: Changed random variable mismatch declaration

    sorry, but I'm baffled on the fgetc issue. It's an int, and produces unsigned chars. therefore, would it not map those chars to the alpha characters referred to in the if statement, and then cross match them back to their numeric values for the tolower statement? I thought that was automatically handled by C?

    Really baffled on why the incorrect name for the typedef would manifest itself with the malloc bit. As soon as I changed it to match the rest of the program, voila, no errors!

    the problem I'm having now is, oh and by the way for some reason the file is now running without the Windows encountered a problem error, but it gets to the file open check and exits. I.e it doesn't print the list of words in the example file, which is basically This is an example file. So it should be outputting 1 1 1 1 1 but all on a new line.

    Thanks, at least we're getting somewhere. Should give me some encouragement to work through the rest of the night.

    If anyone can suggest an explanation for the relationship between typedef and malloc, and why the compiler kept shut about it, I'd be interested to learn.

    Hussein.

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Link on fgetc

    It returns the ASCII value of the character being read - when you assign it to a char, it becomes the value you expect. If reading from the stream fails, it will return EOF, which the FAQ will tell you is an int, not a char - hence, fgetc returns int for that reason.

    Code:
    FILE *fp = fopen ( "example.txt", "r" );
    for this line of code to work, the file must be in the current directory - what I normally do is compile my code, go to the directory where the executable is produced, and move my data file there (or vice versa), rather than allow the executable to be run from the IDE - long story short, make sure that the file is in the current directory.

    I don't know what errors you got regarding malloc, that's why I suggested you make sure that you are compiling as C, not C++ - That's the most common problem with malloc in my experience. The problem in the struct that I was referring to was that filetext is not a valid type (caused by a typo probably, noting the lack of _t) - in fact, inside the struct, filetext_t is not even valid - this is because the typedef occurs after the struct is declared, and you would be trying to use filetext_t inside the struct, so it doesn't make sense. When I compiled your code, I changed the filetext (inside the struct) to filelist. Hopefully that clears things up, if only just a little.

    In any case, I recommend posting your compiler/OS/original compiler error messages (so we can pinpoint what you are misunderstanding), and also perhaps updating your code in your first post (rather than reposting it again with small differences) so we can help fix the current problems.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Living to Learn
    Join Date
    Feb 2006
    Location
    Aberdeen, UK
    Posts
    33
    Ok. Thanks for your reply. I think I'm starting to understand C functions returning different data types than you are actually using them for (e.g using fgetc to get characters but the function returning ints).

    I have no idea what I've done, but the problem is fixed. No errors with the compiler. However, the problem now is that the problem stops at "Test file opened successfully" and won't actually print the nodes, with their frequency (referred to with int val in the struct).

    I'm even more baffled now, as I didn't actually do anything to the malloc part, and stupid me I don't have the original copy of the program to check comparisons - Bloodshed Dev C++ doesn't have a CVS or revision monitor facility.

    Here's the code anyway:

    Code:
    /* References:
    C by Example
    C in a Nutshell
    C Primer 5th Edition
    Stanford CS Guide to Linked Lists
    C Reference Library (Functions)
    */
    
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    
    typedef struct filetext
    {
      int val;
      char words[1024];
      struct filetext *next;
    } filetext_t;
    // Create a structure to hold words and frequency, with sub-structure or next pointer
    
    char val2[1024];
    filetext_t *AddToList ( filetext_t *head, char val2[] )
    {
    // Function, create holder for current word
      filetext_t *current = NULL;
      // Set an initialising pointer set to NULL
    
      if ( head == NULL ) {
        head = malloc ( sizeof *head );       
        head->next = NULL;
      }
    // If head pointer is NULL, free memory and link to next pointer
      else {
        current = malloc ( sizeof *current->next );
        current->next = head;
        head = current;
      }
    // Otherwise first node is created so create next node
    
      strcpy ( head->words, val2 );
    
      return head;
    }
    // Copy the string from the head pointer into the container
    
    void PrintList ( filetext_t *head )
    {
      while ( head != NULL ) {
        printf ( "%s", head->words );
        head = head->next;
      }
    // Display held word, advance pointer to next node
    
      printf ( "\n" );
    }
    // Move carriage to new line
    
    int main ( void )
    {
      char c;
      int i, j = 0;
      char val3[1024];
      filetext_t *list = NULL;
    // Create variables for reading from files and container for words
    
      FILE *fp = fopen ( "example.txt", "r" );
    // Open example file doing away with user input
      if ( fp == NULL ) {
         printf("Open Failed!");
         } else {
         printf("Test file opened successfully");
         }
      for ( i = 0; ( c = fgetc ( fp ) ) != EOF; i++ ) {
        if ( isalpha ( c ) )
          val3[j++] = tolower ( c );
    // Copy alphabetic characters into the val3 container
        else {
          val3[j++] = '\0';
          list = AddToList ( list, val2 );
          j = 0;
        }
    // Otherwise insert blank space to separate words
      }
    
      PrintList ( list );
      fclose ( fp );
    
      return 0;
    // Close open file, and print through list
    }
    Thanks again for the link - another useful reference to have. Also, could I just say the links in your signature were also useful.

    Hussein.

  6. #6
    Registered User
    Join Date
    Nov 2006
    Posts
    12
    Quote Originally Posted by hpteenagewizkid

    Code:
    // Copy alphabetic characters into the val3 container
        else {
          val3[j++] = '\0';
          list = AddToList ( list, val2 ); <- This should be val3, not val2
          j = 0;
        }
    // Otherwise insert blank space to separate words
      }
    
      PrintList ( list );
      fclose ( fp );

    Hussein.
    I believe the problem is due to a simple variable naming error - you've named your character array val3 but when you send it to AddToList, your actually sending val2.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    I think I'm starting to understand C functions returning different data types than you are actually using them for (e.g using fgetc to get characters but the function returning ints).
    Usually C functions return something reasonable. fgetc() returns an int so that it can return EOF to indicate an error or end of file. EOF isn't a character; if it was, then fgetc() could conceivably return it. EOF can only be stored in an int, not a char, so that's what fgetc() returns.

    BTW, getc() is usually a macro implementation of fgetc() -- so it might be faster to use it instead. Otherwise they're identical.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory not released after running program
    By lehe in forum Linux Programming
    Replies: 21
    Last Post: 01-01-2011, 02:32 AM
  2. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  3. running a program
    By task in forum C Programming
    Replies: 1
    Last Post: 05-31-2003, 08:01 AM
  4. Replies: 0
    Last Post: 04-27-2003, 02:04 AM
  5. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM

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