variable scope

This is a discussion on variable scope within the C Programming forums, part of the General Programming Boards category; Hey all! If I have the below code snippets: Code: #include <stdlib.h> #include <stdio.h> #include <string.h> #define FILELINELENGTH 1000 void ...

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

    variable scope

    Hey all!

    If I have the below code snippets:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    #define FILELINELENGTH 1000
    
    void insertstring(struct data *data, int counter2);
    FILE *p_openfile;
    
    int main(void)
    {
    .
    .
    .
    .
    // Extra code here //
    .
    .
    .
    if((p_line = malloc(FILELINELENGTH * sizeof(char))) == NULL )
    	{
    		perror("Error allocating memory");
    		exit (EXIT_FAILURE);
    	}
    
    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) 	
    		{
    			if((p_searchstring = strstr(p_line, p_searchtext) != NULL))
    			{
    				strcpy(LineData[counter1].filename1, LineData[counter].storefilename);
    				found = 1;
    				counter1++;
    				break;
    			}
    		}
    
    		if(!found)  // if search srting(p_searchtext) not found 
    		{
    			strcpy(LineData[counter2].filename2, LineData[counter].storefilename);
    			insertstring(LineData, counter2);
    			counter2++;
    		}
    		fclose(p_openfile);
    	}
    	free(p_line);
    return 0;
    }
    
    void insertstring(struct data *data, int counter2)
    {
    
    	char *p_line1;				
    
    	printf("\n%s", LineData[counter2].filename2);
    
    	if((p_line1 = malloc(FILELINELENGTH * sizeof(char))) == NULL )
    	{
    		perror("Error allocating memory");
    		exit (EXIT_FAILURE);
    	}
    
    	while((fgets(p_line1, FILELINELENGTH, p_openfile)) != NULL) 	
    		{
    			printf("\n%s", p_line1); 
    
    		}
    }
    All the code in the main() works fine.

    What I want to do is to print out every line of the files that don't contain the search string(p_searchtext).

    Why does the last line of code(in red) not print? I've used the debugger, and the debugger just skips the while loop to the closing brace. At this point, p_line1 contains garbage, instead of a line of text.

    I can post the complete code, if necessary.

    Thanks in advance.
    Last edited by happyclown; 03-01-2009 at 10:13 PM.
    OS: Linux Mint 13(Maya) LTS 64 bit.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,886
    Quote Originally Posted by happyclown
    Why does the last line of code(in red) not print? I've used the debugger, and the debugger just skips the while loop to the closing brace. At this point, p_line1 contains garbage, instead of a line of text.
    It looks like the file pointer is already at end of file when you call insertstring().

    By the way, turn p_openfile into a local variable and indent your code more consistently. Avoid such "Extra code here" comments by providing the smallest and simplest (compilable) program that demonstrates the problem.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    > if(!found)
    Look at your while loop.
    If this is true, then so is feof() on the file you've been reading.

    So trying to read more of the file later on is a no-brainer.

    Oh, and I might add, your indentation STILL needs work.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    In my head happyclown's Avatar
    Join Date
    Dec 2008
    Location
    In my head
    Posts
    391
    Excellent! Thanks for everybody's help!
    OS: Linux Mint 13(Maya) LTS 64 bit.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Also, you're allocating memory for p_line1, but you don't free it.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. making a wstring variable global scope
    By stanlvw in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2008, 02:25 PM
  2. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Variable scope
    By Axel in forum C Programming
    Replies: 2
    Last Post: 09-19-2005, 08:41 PM
  5. variable in file scope ???
    By howhy in forum C++ Programming
    Replies: 5
    Last Post: 08-30-2005, 04:46 AM

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