Thread: funcion runs 14 times, then program crashes

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #1
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391

    funcion runs 14 times, then program crashes

    Hey all.

    The aim of this program gets a string that doesn't contain the word "every", breaks up the string into 2 strings, appends "every" onto the first string, then appends the second string onto the first(modified string).

    Here's the compilable code.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define FILELINELENGTH 1000		
    #define NUMBEROFFILES 1000		
    #define FILENAMELENGTH 100	
    
    #define STRING1LENGTH 200
    #define STRING2LENGTH 600
    
    void insertstring(void);
    char *p_searchtext = " every ";		// string to search for in a file
    FILE *p_openfile;
    char *p_line1;
    
    int main(void)
    { 	
    	struct data {
            char storefilename[FILENAMELENGTH];		
    		char filename1[FILENAMELENGTH];			
    		char filename2[FILENAMELENGTH];			
        } LineData[NUMBEROFFILES];
    
    	int lastfilenumber1, lastfilenumber2;
    	char *p_line;						
    	char *p_newline;
        int  counter;						
    	int	 counter1 = 0;					
    	int	 counter2 = 0;					
    	char *p_searchstring;
    							
    	system("dir /b c:\\testwebsite\\*.html > directorylisting.txt");
    
        if((p_openfile = fopen("directorylisting.txt", "rt")) == NULL)
        {
            perror("directorylisting.txt");
            exit( EXIT_FAILURE );
        } 
        counter = 0;
    
    	// Copy the filenames from the file to the array; replace the newline character with a terminating
    	// null character to make it a string which can be opened by fopen().
    
        while((fgets(LineData[counter].storefilename, FILENAMELENGTH, p_openfile) != NULL) && (counter < NUMBEROFFILES))
    	{
    		if((p_newline = strchr(LineData[counter].storefilename, '\n')) != NULL)
    			*p_newline = '\0';
    
            counter++;	
    		lastfilenumber1 = counter;
        }
        fclose(p_openfile);
    
    	if((p_line = malloc(FILELINELENGTH * sizeof(char))) == NULL )
    	{
    		perror("Error allocating memory");
    		exit (EXIT_FAILURE);
    	}
    
    	// Open up a file, search for p_searchtext, if it exists, store filename(LineData[counter].storefilename)
    	// in LineData[counter2].filename1. Open up next file etc.
    
    	for( counter = 0; counter < lastfilenumber1; counter++) 
    	{	
    		int found = 0;
    		if((p_openfile = fopen(LineData[counter].storefilename, "r")) == NULL)
    		{
    			printf("error opening file: %s", LineData[counter].storefilename);
    			exit(EXIT_FAILURE);
    		}
    
    		while((fgets(p_line, FILELINELENGTH, p_openfile)) != NULL) 	//// search string found
    		{
    			if((p_searchstring = strstr(p_line, p_searchtext) != NULL))
    			{
    				strcpy(LineData[counter1].filename1, LineData[counter].storefilename);
    				found = 1;
    				counter1++;
    				break;
    			}
    		}
    
    		if(!found) // search string NOT found
    		{
    			strcpy(LineData[counter2].filename2, LineData[counter].storefilename);
    			rewind(p_openfile);
    			insertstring();
    			counter2++;
    		}
    		fclose(p_openfile);
    	}
    	free(p_line);
    
    	printf("\nThere are %d files containing the search string\n", counter1);
    	printf("\nThere are %d files NOT containing the search string\n", counter2);
    
        return 0;
    }
    
    void insertstring(void)
    {
    	char *p_searchstring = "lists";
    	char storestring[FILELINELENGTH];
    	char string1[STRING1LENGTH];
    	char string2[STRING2LENGTH];
    	char *location;
    	int cursorposition;
    	int counter = 0;
    
    	while((fgets(storestring, FILELINELENGTH, p_openfile)) != NULL) 	
    	{
    		if((location = strstr(storestring, p_searchstring)) != NULL) 
    		{
    			cursorposition = location - storestring;
    			break;
    		}
    	}
    
    	memmove(string1, storestring, cursorposition+5);  // 5 is to allow space for the string "lists"
    	string1[cursorposition+5] = '\0';
    	strcat(string1, p_searchtext);
    
    	for(counter = (cursorposition+5); storestring[counter] != '\0'; counter++)	
    		string2[counter - (cursorposition+5)] = storestring[counter];   // copy string into the start of string2
    
    	string2[counter - (cursorposition+5)] = '\0';
    	strcat(string1, string2);
    	printf("%s", string1);
    }
    When I run this program in debugger, after the insertstring runs 14 times, the program crashes. A debugger window pops up with:
    Unhandled Exception at 0x7c90e8b6(ntdll.dll) in testing.exe: 0xC0000005: Access violation writing location 0x00030ffc
    When I click the "break" button, another window pops up with:
    There is no source code available for the current location
    And then there is a button to "Show Disassembly", but I don't know what that does.

    Can anyone offer a guess as to why the program would behaviour in this manner?

    Thanks in advance.

    PS: I've made some variables global because I am experimenting with variable scope.

    Indentation Disclaimer
    Sometimes weird translation happens when I post my code from VC++ 2008 into a forum thread. The indentations are magnified by an order of magnitude, which pushes the code far to the right of the screen. My indentation is good, but the weird translation makes it look bad. It's not my fault. Honest.
    Last edited by happyclown; 03-03-2009 at 09:38 PM.
    OS: Linux Mint 13(Maya) LTS 64 bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Utter newb?: Program crashes after input
    By deductible in forum C++ Programming
    Replies: 5
    Last Post: 12-13-2008, 10:27 PM
  2. Program crashes, why?
    By Mahdi123 in forum C Programming
    Replies: 5
    Last Post: 04-18-2007, 02:56 PM
  3. Scheduling Algo
    By BigDaddyDrew in forum C++ Programming
    Replies: 41
    Last Post: 03-08-2003, 11:00 AM
  4. Replies: 5
    Last Post: 08-05-2002, 07:14 PM
  5. multiple runs of a program
    By unregistered in forum Linux Programming
    Replies: 5
    Last Post: 03-15-2002, 07:18 AM