Thread: Debug assertion failure (EXPRESSION: Str ! = null )

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    17

    Debug assertion failure (EXPRESSION: Str ! = null )

    My program is supposed to read from a file to check number of words sentences ETC. It does this so far until I move the code into separate functions. It gives me the error that is in the topic title. Any ideas??

    Code:
    void findFile (char filename[], FILE *fp){
    while (1){
        char filename[100];
       
        puts("Enter the name of the file to open");
    
    
        scanf("%s", &filename); 
    
    
        fp=fopen(filename, "r");
     
    
    
         
              if (fp == 0)
                {
                  printf("\n\n%s file name not found!\n\n", filename); 
                } 
            
              else 
              {
                puts("File successfully opened");
                break; 
              }
    
    
      
       
    }//End while loop
    
    
    }//end function
    
    
    /*************************Character counter function*************************************
    Purpose: To count number of characters in a file
    Parameters: 
    Return: none
    Input: 
    Output:
    ***********************************************************************************/
    
    
    void countChar (int charCount, int ch, FILE *fp)
    {
           while (1) {
            ch = fgetc(fp);
           ++charCount;
     
            if (ch == -1)
                break;
              }
    ch = 0;
             
      
     
    
    
    printf("\nNumber of characters: %i\n", charCount);
        
        
    
    
     
    }
    
    
    void wordCount (char i, int wCount, FILE *fp)
    {
    while((i = getc(fp)) != EOF)
           {  
    
    
    if ((i = getc(fp)) == ' ')
    ++wCount;
     
     }
      printf("Number of words: %i\n", wCount);
    }

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Your spacing is horrific.
    And you haven't posted your main for some reason.
    At any rate, you'll need to pass a double pointer to the badly-named "findfile" routine if you want the file pointer to be modified wherever you're calling it from.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    lol ive made it a mess from messing with different things to figure this out

    searchFile

    thanks, ill try that

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    any examples of a double pointer with this file pointer?

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Code:
    void findFile (char filename[], FILE **fp){
    // ...
    *fp = fopen(...); // this will modify fp in caller
    //...
    }
    
    // call it (from main?) like this
    FILE *fp;
    findFile("afile", &fp);
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User
    Join Date
    Dec 2011
    Posts
    17
    Thats what i just figured out. thanks for the help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debug Assertion failure problem
    By uldaman in forum C++ Programming
    Replies: 8
    Last Post: 01-21-2008, 02:22 AM
  2. debug assertion failure
    By talz13 in forum Windows Programming
    Replies: 2
    Last Post: 07-20-2004, 11:23 AM
  3. Debug Assertion Failure - bad pointer
    By ventolin in forum C++ Programming
    Replies: 5
    Last Post: 05-24-2004, 10:10 AM
  4. Debug Assertion Failure
    By gozlan in forum C Programming
    Replies: 2
    Last Post: 09-08-2002, 11:10 AM
  5. How to tackle a debug assertion failure?
    By juhigarg in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2001, 12:59 PM