Thread: compiler disappears

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    13

    compiler disappears

    Hello

    I am trying to find a string in a text file.
    The program is working fine when I search small text files.

    But the turbo C compiler is just disappearing when the text file to be searched is large.

    Why is this happening?

    How can I fix it?


    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
    	int ch,i=0,j=0,k;
    	char store[300][100];
    	char one_file[] = "one.txt";
    	char two_list[] = "two.txt";
    	FILE *one,*two;
    	clrscr();
    	one = fopen(one_file,"r");
    	two = fopen(two_list, "w");
    	while((ch=getc(one))!=EOF)
    	{
    		if(ch=='s')
    		{
    			if((ch=getc(one))=='r')
    				if((ch=getc(one))=='c')
    					if((ch=getc(one))=='\=')
    						if((ch=getc(one))=='\"')
    						{
    							j=0;
    							while((ch=getc(one))!='\"')
    							{
    								store[i][j]=ch;
    								fprintf(two, "%c",ch);
    								j++;
    							}
    						 store[i][j]='\0';
    						 fputc('\n',two);
    						 i++;
    						 }
    		}
    	}
    	for(k=0;k<i;k++)
    		printf("string[%d]=%s\n",k,store[k]);
    	fclose(one);
    	fclose(two);
    	return(0);
    }
    Last edited by spveer; 07-07-2005 at 09:17 AM.

  2. #2
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    What the heck to you mean by "But the turbo C compiler is just disappearing when the text file to be searched is large."?

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    Sorry,
    I mean, when I run the program with a large text file to be searched, it doesnt work out.

    But I am able to run the program for small text files to be searched.

  4. #4
    Registered User mitakeet's Avatar
    Join Date
    Jun 2005
    Location
    Maryland, USA
    Posts
    212
    Take a look at my reformatted version and see if this is what you want (logically, this is how the program executes):

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
        int ch,i=0,j=0,k;
        char store[300][100];
        char one_file[] = "one.txt";
        char two_list[] = "two.txt";
        FILE *one,*two;
    //    clrscr(); // not available on my machine
        one = fopen(one_file,"r");
        two = fopen(two_list, "w");
        while((ch=getc(one))!=EOF) {
            if(ch=='s') {
                if((ch=getc(one))=='r'){
                    if((ch=getc(one))=='c'){
                        if((ch=getc(one))=='\='){//warning C4129: '=' : unrecognized character escape sequence
                            if((ch=getc(one))=='\"') {
                                j=0;
                                while((ch=getc(one))!='\"') {
                                    store[i][j]=ch;
                                    fprintf(two, "%c",ch);
                                    j++;
                                }//end while((ch=getc(one))!='\"')
                                store[i][j]='\0';
                                fputc('\n',two);
                                i++;
                            }//end if((ch=getc(one))=='\"')
                        }//end if((ch=getc(one))=='\=')
                    }//end if((ch=getc(one))=='c')
                }//end if((ch=getc(one))=='r')
            }//end if(ch=='s')
        }//end while((ch=getc(one))!=EOF)
        for(k=0;k<i;k++){
            printf("string[%d]=%s\n",k,store[k]);
        }
        fclose(one);
        fclose(two);
        return(0);
    }
    It is a very bad idea to have multiple lines in a statement and not surround them by braces (many people think you should always use braces, even for single line statements) even when the parser will handle things according to your expectations as any tiny change to the code can wind up changing the logical flow of the code. Also note that I got a compiler warning, this is almost certainly an error in your program. It is also a bad idea to ignore warnings as they are almost always pointing out bugs (compiler writers are some pretty smart dewds, it is a bad idea to second guess them).

    Free code: http://sol-biotech.com/code/.

    It is not that old programmers are any smarter or code better, it is just that they have made the same stupid mistake so many times that it is second nature to fix it.
    --Me, I just made it up

    The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man.
    --George Bernard Shaw

  5. #5
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by spveer
    Sorry,
    I mean, when I run the program with a large text file to be searched, it doesnt work out.
    With your large file are you finding more than 300 strings such that you are overrunning your store array? Something like this would be safer.
    Code:
    if ( ++i >= sizeof store / sizeof *store )
    {
       break; /* maybe print a message saying the array is full */
    }
    You may also want to consider the same sort of thing with the string length.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    13
    now i get it, thanks. Yes the array is full!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  2. Compiler Paths...
    By Cobra in forum C++ Programming
    Replies: 5
    Last Post: 09-26-2006, 04:04 AM
  3. C Compiler and stuff
    By pal1ndr0me in forum C Programming
    Replies: 10
    Last Post: 07-21-2006, 11:07 AM
  4. I can't get this new compiler to work.
    By Loduwijk in forum C++ Programming
    Replies: 7
    Last Post: 03-29-2006, 06:42 AM
  5. how to call a compiler?
    By castlelight in forum C Programming
    Replies: 3
    Last Post: 11-22-2005, 11:28 AM