Results 1 to 7 of 7

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

Threaded View

  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.

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