Thread: help

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    help

    Code:
    int main()
    {
    FILE *file;
    file = fopen("C:/Users/dell/Desktop/File/init.txt","r");
    int xcount;
    char c[4],brown_spec[2],brown_conc[1];
    float *x=NULL;
    float ***brown;
    int line_read=0,xtime=0,xconc_range=0,xspecies=0,i=0,j=0;
    if (file!=NULL)
    {
    		
    		while(line_read<3 && fgets(c,10,file)!=NULL )
    		{
                line_read++;
    			if (line_read==1)
    			{
    				xtime=atoi(c);
    			}
    			if (line_read==2)
    			{
    				xconc_range=atoi(c);
    			}
    			if (line_read==3)
    			{
    				xspecies=atoi(c);
    			}
    		}
    
    		brown=(float***)malloc(xspecies*sizeof *brown);
    		for( i=0; i<xspecies ; i++ )
    		{
    			brown[ i ] =(float**)malloc(xtime* sizeof **brown);/* Allocate 'xtime' number of pointers to int */
    			 /*Validate malloc's success/failure using the return value*/
    			for( j=0; j<xtime; j++)
    			{
    				brown[ i ][ j ]= (float*)malloc(xconc_range* sizeof ***brown);/* Allocate 'xconc_range' number of ints */
    				/*Validate malloc's success/failure using the return value*/
    			}
    		}
    
    		while(fgets(c,10,file)!=NULL && line_read>=3)
    		{
    		  strncpy(brown_spec,&c[2],1);
    		 if(brown_spec[0]=='=')
    		 {
    			 strncpy(brown_spec,&c[1],1);
    			 strncpy(brown_conc,&c[3],1);
    		 }
    		 else
    		 {
                strncpy(brown_spec,&c[1],2);
                strncpy(brown_conc,&c[4],1);
    		}
    
    		 brown[atoi(brown_spec)][0][atoi(brown_conc)]=1;
    
    		 line_read++;
    
    		}
    }  
    
    
    
    exit(0);
    return 0;
    
    }
    Hi The problem with the above code is that if i dont put the exit function before return it gives an error , but when i put the exit function it works fine... can u tell me why such is the case?

    Tnks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by doubty View Post
    Hi The problem with the above code is that if i dont put the exit function before return it gives an error , but when i put the exit function it works fine... can u tell me why such is the case?
    Not sure exactly why you get an error, but you certainly do not need exit(0) at the end of this code. "return" in main() signifies the end of the program, so anything after an unqualified return statement in main will never happen.

    exit() is usually used outside of main to end the program or to trigger atexit() functions.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What error do you get?

  4. #4
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    It would help to post init.txt so we could try to reproduce the problem.

    I would start by removing parts of the program until the error disappeared. You may be getting stack corruption somewhere so that the return address becomes invalid.

  5. #5
    Registered User
    Join Date
    Jun 2009
    Posts
    39

    The contents of the init file

    Thanks all
    Last edited by doubty; 07-19-2009 at 09:03 PM.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
     fgets(c,10,file)
    Why are you trying to read 10 characters into a buffer of 4 characters?


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    pwning noobs Zlatko's Avatar
    Join Date
    Jun 2009
    Location
    The Great White North
    Posts
    132
    Yes, as quzah pointed out, c is the problem. It needs to be large enough to hold the line, including the newline character and the null terminator. It should be 6 bytes long at least. The fgets line should read:
    fgets(c, sizeof(c), file);
    assuming that c stays a character array.

  8. #8
    Registered User
    Join Date
    Jun 2009
    Posts
    39
    Thnks all.. it is wrking fine now...

  9. #9
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Perhaps the problem was, that an early exit(0) prevented any checking of broken addresses and corrupted memory. Still strange though as any necessary clean-up should have caught that.

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    That's why it's called undefined behavior.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jul 2009
    Posts
    40
    x is assigned value but it's not used.

Popular pages Recent additions subscribe to a feed