![]() |
| | #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;
}
}
|
| ali_1234 is offline | |
| | #2 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| 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;
}
}
4. in your while loop - check that you do not enter more than 10 numbers
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #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 |
| ali_1234 is offline | |
| | #5 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #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 ... |
| ali_1234 is offline | |
| | #7 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| 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);
}
Don't bother indenting the debug printfs, since that makes it easier to go back and erase them all later.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #8 |
| Registered User Join Date: Sep 2006
Posts: 2,512
| 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
|
| Adak is offline | |
| | #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 ??.. |
| ali_1234 is offline | |
| | #10 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
Code: #include <stdio.h>
int main() {
printf("HELLO WORLD!\n");
return 0;
}
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #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 |
| ali_1234 is offline | |
| | #12 | |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,946
| Quote:
Code: 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.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS | |
| MK27 is offline | |
| | #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;
}
|
| ali_1234 is offline | |
| | #14 |
| CSharpener Join Date: Oct 2006
Posts: 5,242
| 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?
__________________ If I have eight hours for cutting wood, I spend six sharpening my axe. |
| vart is offline | |
| | #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 |
| ali_1234 is offline | |
![]() |
| Tags |
| c programming, home work, input output, min and max, minimum and maximum |
| Thread Tools | |
| Display Modes | |
|