Thread: program compiles, runs, but produces no output

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

    program compiles, runs, but produces no output

    Well, here it is. I am having problems only within the for loop at the bottom(in red). I think there could be an error in logic.

    Code:
    #define _CRT_SECURE_NO_WARNINGS // To turn off VC++ 2008 deprecation warnings
    
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define NUMBEROFFILES 1500		// maximum number of files in the directory listing
    #define LINELENGTH 150			// length of a line in the directory listing
    #define FILENAMELENGTH 40		// length of an .html file
    #define BEGINNINGOFHTMLLIST 5	// first html file in directory list
    #define FILELINELENGTH 1000		// lengh of a line in an html file
    
    int main( void )
    { 
        int lastfilenumber1, lastfilenumber2;
        FILE *p_openfile;
    	char *p_comma;				// used to test for occurence of a comma in a line of the directory listing
    	char *p_line;				// stores successive lines from a file from the directory listing
        int  counter1, counter2;
    	char *p_searchstring;		// used for strstr()
    	char *p_searchtext = "every";		// string to search for in a file
    
    	struct data {
            int month, day, year, hour, minute;
            char string1[2+1];
    		int number1, number2;
            char string2[FILENAMELENGTH];
            char stringstorage[LINELENGTH];		// Used to store strings from file
    		char string3[FILENAMELENGTH];		// filenames that contain the search string
        } LineData[NUMBEROFFILES];
    
        // Get a list of files in the directory "testwebsite", and redirect the
        // output to a file called "directorylisting.txt". */
    
    	system("dir c:\\testwebsite\\*.html > directorylisting.txt");
    
        // Test to see that the file "directorylisting.txt" can be opened for reading.
    
        if( (p_openfile = fopen("directorylisting.txt", "rt")) == NULL)
        {
            perror("directorylisting.txt");
            exit( EXIT_FAILURE );
        }
    
        // Copy the lines in the file to the array 
    
        counter1 = 0;
    
        while((fgets(LineData[counter1].stringstorage, LINELENGTH, p_openfile) != NULL) && (counter1 < NUMBEROFFILES))
        {
    		lastfilenumber1 = counter1;
            counter1++;	
        }
    
        fclose(p_openfile);
    
    	// Process the string, and store data items in the structure members
    
    	for( counter1 = BEGINNINGOFHTMLLIST; counter1 < lastfilenumber1; counter1++)
    	{
    		if( (p_comma = strchr( LineData[counter1].stringstorage, ',')) != NULL )
    		{
    			sscanf(LineData[counter1].stringstorage, "%d%/%d/%d %d:%d %s %d,%d %s", &LineData[counter1].month,
    				&LineData[counter1].day,&LineData[counter1].year, &LineData[counter1].hour,
    				&LineData[counter1].minute, LineData[counter1].string1, &LineData[counter1].number1,
    				&LineData[counter1].number2,&LineData[counter1].string2);
    		}
    
    		if( (p_comma = strchr( LineData[counter1].stringstorage, ',')) == NULL )
    		{
    			sscanf(LineData[counter1].stringstorage, "%d%/%d/%d %d:%d %s %d %s", &LineData[counter1].month,
    				&LineData[counter1].day, &LineData[counter1].year, &LineData[counter1].hour,
    				&LineData[counter1].minute, LineData[counter1].string1, &LineData[counter1].number1,
    				&LineData[counter1].string2);
    		}
    	}
    
    	// open up a file, search for p_searchtext, if it exists, store filename(LineData[counter1].string2) in
    	// LineData[counter2].string3 then print LineData[counter2].string3. open up next file etc.
    
    	counter2 = 0;
    
    	for( counter1 = BEGINNINGOFHTMLLIST; counter1 < (lastfilenumber1 - 1); counter1++) 
    	{	
    		if((p_openfile = fopen(LineData[counter1].string2, "rt")) == NULL)
    		{
    			printf("\nError opening file %s", LineData[counter1].string2);
    			exit(EXIT_FAILURE);
    		}
    
    		p_line = malloc(FILELINELENGTH * sizeof(char));
    
    		while((fgets(p_line, FILELINELENGTH, p_openfile) != NULL) && 
    			(p_searchstring = strstr(p_line, p_searchtext) != NULL)) 	
    		{
    			
    			strcpy(LineData[counter2].string3, LineData[counter1].string2); 
    			printf("\n%s", LineData[counter2].string3);
    			lastfilenumber2 = counter1;
    			counter2++;
    			free(p_line);
    			p_line = malloc(FILELINELENGTH * sizeof(char));
    		}
    
    		fclose(p_openfile);
    	}
    
        return 0;
    }
    Here is what I want to achieve with the for loop:

    1. open an .html file for reading.
    2. allocate memory to a pointer to a string(p_line)
    3. use fgets() to read the first string(line) of the file into p_line
    4. search p_line for the occurence of 'every'
    5. if p_line(and therefore the file) contains the word 'every', copy that filename to LineData[counter2].string3, then printf LineData[counter2].string3
    6. free memory to p_line
    7. allocate memory to p_line again
    8. go to step 3 to read the next line in the file
    9. go to 4.
    10. go to 5...and so on.
    11. keep reading files until the EOF
    12. go to 1 again to open up the next file and repeat until all files have been opened and searched.

    What I am really trying to achieve is to print the names of all the files that contain the word 'every'.

    However, nothing gets printed at all.

    Where did I go wrong?

    Thanks in advance.
    Last edited by happyclown; 01-26-2009 at 04:18 AM. Reason: deleted some content
    OS: Linux Mint 13(Maya) LTS 64 bit.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GPL license: how does it cover the output of a program?
    By psychopath in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 05-25-2006, 01:37 AM
  2. receiving notification when user runs a program
    By hanhao in forum C++ Programming
    Replies: 4
    Last Post: 01-13-2006, 11:23 PM
  3. Replies: 3
    Last Post: 07-11-2005, 03:07 AM
  4. How to make a program that runs in the process
    By Makoy in forum Windows Programming
    Replies: 12
    Last Post: 12-20-2004, 10:19 AM
  5. program output is empty!!!
    By ayesha in forum C++ Programming
    Replies: 4
    Last Post: 10-15-2001, 10:42 PM