Thread: please help

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    11

    please help

    Code:
    this is my school project i think i have almost completed it .. i have to input data from external file and write it in other out put file... i have to fine the max and min of the given data in the input file... 
    the program is getting crashed.. please help
    
    #include <stdio.h>
    
    int main ()
    {
      int i,a[10],val,n=0,max,min;
      FILE *fi,*fo;
    	if((fi=fopen("indata.txt","r"))==NULL)
          {
    		printf("Error in opening the file!!!\n");
           return 1;
          }
    
    	else
    {
    	while(fscanf(fi,"%f",&val))
    	{
    		a[n]=val;
    		n=n+1;
    	}
    
     max = a[0];
      min = a[0];
    
      for (i = 0; i < 10; i++)
        {
          if (a[i] > max)
            {
              max = a[i];
            }
          else if (a[i] < min)
            {
              min = a[i];
            }
        }
    	if((fo=fopen("result.txt","w"))==NULL)
          {
    		printf("Error in opening the file!!!\n");
           return 1;
          }
    	else
    {
      fprintf (fo,"Maximum element in an array : %d\n", max);
      fprintf (fo,"Minimum element in an array : %d\n", min);
    }
    
    fclose(fo);
    
    
    fclose(fi);
    
    return 0;
    }
    }

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. do not put regular text inside the code tags
    2. indent your code
    Code:
    #include <stdio.h>
    
    int main ()
    {
    	int i,a[10],val,n=0,max,min;
    	FILE *fi,*fo;
    	if((fi=fopen("indata.txt","r"))==NULL)
    	{
    		printf("Error in opening the file!!!\n");
    		return 1;
    	}
    	
    	else
    	{
    		while(fscanf(fi,"%f",&val))
    		{
    			a[n]=val;
    			n=n+1;
    		}
    		
    		max = a[0];
    		min = a[0];
    		
    		for (i = 0; i < 10; i++)
    		{
    			if (a[i] > max)
    			{
    				max = a[i];
    			}
    			else if (a[i] < min)
    			{
    				min = a[i];
    			}
    		}
    		if((fo=fopen("result.txt","w"))==NULL)
    		{
    			printf("Error in opening the file!!!\n");
    			return 1;
    		}
    		else
    		{
    			fprintf (fo,"Maximum element in an array : %d\n", max);
    			fprintf (fo,"Minimum element in an array : %d\n", min);
    		}
    		
    		fclose(fo);
    		
    		
    		fclose(fi);
    		
    		return 0;
    	}
    }
    3. "%f" for floats - "%d" is for integers
    4. in your while loop - check that you do not enter more than 10 numbers
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Plus it's obvious you copied large portions. Not sure any sane person uses spaces and tabs.

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    i copied it from borland directly over here thtz y it showed like this and its a project so i have to make it presentable and readable .. so thats why its like this

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by vart
    I see horizontal scroll bar - and it is better than having all the page extended to the right
    You're killing me with this one
    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

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    still the progrm isn't working while compiling it juz blinks and goes away... and i dont get any out put file ... i evn tried 'getch ();' ... still nothing happnd ...

  7. #7
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The easiest way to debug something like this is to scatter some printf statements in appropriate places, like this:
    Code:
    	while(fscanf(fi,"%d",&val))
    	{
    		a[n]=val;
    		n=n+1;
    printf("%d %d\n", val, n);
    	}
    This will allow you to see what is actually happening as opposed to what you hoped was happening, and then you can deal with more specific problems, such as "why didn't fscanf work here?"

    Don't bother indenting the debug printfs, since that makes it easier to go back and erase them all later.
    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

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You may want to hold the console window open with some line of code like this:

    Code:
    printf("\n\n\t\t\t      press enter when ready ");
    i = getchar();  //hold the console window open
    and put those two lines lines of code just before your return line.

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    i tried all the above solution u people gave.. still when i compile i dont get any output nor it shows any error ...
    thank you people for your support .. but i am still not getting any output is there somthing wrong in my code ??..

  10. #10
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ali_1234 View Post
    i tried all the above solution u people gave.. still when i compile i dont get any output nor it shows any error ...
    thank you people for your support .. but i am still not getting any output is there somthing wrong in my code ??..
    If you cannot create a program which produces any output, you need to go back a few steps:
    Code:
    #include <stdio.h>
    
    int main() {
         printf("HELLO WORLD!\n");
         return 0;
    }
    Try that, if it doesn't work then your problem is your IDE or OS, not C.
    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

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    no all other programs are compiling ... and the example u have given even tried that its also working...
    but only my project program isn't working...
    please help ... my mind is just stuck cant figure out any thing

  12. #12
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by ali_1234 View Post
    no all other programs are compiling ... and the example u have given even tried that its also working...
    but only my project program isn't working...
    please help ... my mind is just stuck cant figure out any thing
    Then combine the two. Make HELLO WORLD the first line of your program and see if that prints. You can also pause the execution until you hit return with this:
    Code:
    getchar();
    But if you use more than one, don't hit anything but enter (otherwise there will be characters left in the stdin buffer that will trip the next getchar).

    Keep adding printf/getchar combinations until you reach the point where something unpredictable happens (like the program suddenly ends). I can tell you for sure that your code as originally posted will not do what you want, but probably only you can change that.
    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

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    Code:
    #include <stdio.h>
    
    int main ()
    {
    	int i,a[10],val,n=0,max,min;
    	FILE *fi,*fo;
    	if((fi=fopen("indata.txt","r"))==NULL)
    	{
    		printf("Error in opening the file!!!\n");
    		return 1;
    	}
    	
    	else
    	{
    		while(fscanf(fi,"%f",&val))
    		{
    			a[n]=val;
    			n=n+1;
    		}
    the program is working perfectly fine till here but then it does not calculate max n minimum instead it goes on reading data from input file and finally crashes ... now what to do ?

  14. #14
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    your fscanf could return 1 if one float value is successfully parsed
    0 if nothing is parsed (for example string that is not a number encountered)
    or EOF (which is -1 on most compilers) if the end-of-file riched.

    what will happen to your loop in this case?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  15. #15
    Registered User
    Join Date
    Mar 2009
    Posts
    11
    if i use return one after fscanf then its reading only the fist number from input file and after i press enter the program terminates...
    i m using borland as my compiler

Popular pages Recent additions subscribe to a feed

Tags for this Thread