Results 1 to 7 of 7

Thread: [resolved] can you spot the problem with my newbie code

  1. #1

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    51

    Unhappy [resolved] can you spot the problem with my newbie code

    I've been bashing at this for a while, and i got rid of all the compile errors, except it crashes when i run it (doh!). right down the bottom of the code is a getchar(), after i hit enter to pass it is when it crashes. I'm pretty sure the error is in the readline function - befroe i added the funtion it worked.

    The code is probably riddled with syntax errors and dumb newbie mistakes - I'm normally a vb programmer. So please, criticism of programming style is welcome too.

    Oh, I have to do all this in basic C (ANSI C if possible) - it has to run in dos, or at least a command shell.

    Code:
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #define SIZE 80
    char *readline(char *buff, FILE *fpin);
    
    int main(void)
    {
        FILE *fpin, *fpout;
        char buff[SIZE];
    	long lcounter;
    
    	fpin = fopen("in.txt","r");       /* open for reading */
    	if( fpin == NULL)
    	{
    		printf("cannot open in.txt\n");
    		exit(1);
    	}
    
    	fpout = fopen("out.txt","w");     /* open for writing */
    	if( fpout == NULL)
    	{
    		printf("cannot open out.txt\n");
    		exit(2);
    	}
    
    
        for(lcounter=0;lcounter<=62;lcounter++)
    	{
    		fgets(buff,SIZE,fpin);
    	}
    
    	// main file reading loop
    	while(readline(buff,fpin)!= NULL)
        {
    		fputs(buff,fpout);
        }
        //finished, close files
    	fclose(fpin);
        fclose(fpout);
    	return 0;
    }
    
    char *readline(char *buff, FILE *fpin)
    {
    	char newpagecheck[24];
    	char *fgetsret;
    	//_strnset( newpagecheck, " " , 23 );
    	fgetsret = fgets(buff,SIZE,fpin);
    	strncpy(newpagecheck,(buff+42),23);
    	newpagecheck[24] = '\0';
    	printf("line: %s",buff);
    	if (newpagecheck=="Department of Education")
    	{
    		fgetsret = fgets(buff,SIZE,fpin);
    		printf("yay");
    	}
    	getchar();
    	return fgetsret;
    }
    cheers. did i mention that you guys here are the coolest?
    Last edited by Herbatic; Jan 30th, 2003 at 06:28 PM.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I didn't look at it closely, but I can say this:
    There aren't any syntax errors, else it wouldn't compile.
    It runs ok for me and doesn't crash.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    51
    Thanks CornedBee. I assume you made a 'in.txt' for it to process so that it actually ran thru?

    Well, I suppose what this means is that something weird in the in.txt file must be the cause of the problem.

    I'll look into that, but in the meantime, any other ideas are still welcome.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Maybe the first line in in.txt is more than 79 characters long.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5
    Fanatic Member twanvl's Avatar
    Join Date
    Dec 2001
    Posts
    771
    Code:
    char newpagecheck[24];
    strncpy(newpagecheck,(buff+42),23);
    newpagecheck[24] = '\0';
    This code does:
    1. Create space for an array of chars, indices 0 to 23
    2. Copy 23 characters into that array (into elements 0 to 22)
    3. Set element 24 of that array to '\0'

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Good eye, twanvl.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Member
    Join Date
    Aug 2001
    Posts
    51
    Thanks twanvl! That was the problem. I am so used to vb when you declare a 24 element array it actually makes a 25 element from 0 to the 24.

    Guess what element 24 of newpagecheck actually is? the start of pointer fgetsret! (I declared it right after newpagecheck) No wonder my program performed an illegal operation the next time it tried to access fgetsret.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width