Here's a program and its pretty simple .It requires file handling and some calculations but on running it I am not getting the required result.It seems that the code outside the file read's outer while loop is not executing e.g the print statement is not being printed.Plz Help!
#include<stdio.h>
#include<stdlib.h>
#include<ctype.h>
int main(int argc,char *args[])
{
FILE *fp;
char ch;
int load=0,value=0,weight=0;
int i=0,count=0,p,k=1,j,*cost,line=1,u;
if(argc!=2){
printf("\nInsufficient no. of arguments");
exit(0);
}
fp=fopen(args[1],"r");
if(fp==NULL){
printf("\nFile could not be opened");
exit(0);
}
while((ch=fgetc(fp))!='\n'){
if(isdigit(ch)){
p=ch-'0';
load=load*10+p;
}
}
printf("\nload= %d \n",load);
i=0;
while((ch=fgetc(fp)!=EOF)){
value=0;
weight=0;
k=0;
u=0;
while(ch!='\n'){
// printf("\nline=%d,weight=%d,value=%d",line,weight,value);
if((isalpha(ch)||isdigit(ch))&&k==0){
while(ch!=' '){
ch=fgetc(fp);
}
k++;
}
if(weight==0){
while(ch!=' '){
p=ch-'0';
weight=weight*10+p;
ch=fgetc(fp);
}
}
// printf("\nweight=%d",weight);
if(value==0){
while(ch!='\n'){
p=ch-'0';
value=value*10+p;
ch=fgetc(fp);
}
}
// printf("\nValue=%d",value);
if(ch==' '){
while(ch==' '){
ch=fgetc(fp);
}
}
}
u=value/weight;
printf("line=%d weight=%d,value=%d,cost=%d\n",line,weight,value,u);
line++;
}
// count=i-1;
fclose(fp);
return 0;
}
The text file that I am using is strictly of the following format:
1250
LJS93K 1300 10500
J38ZZ9 700 4750
HJ394L 200 3250
01IE82 75 10250
there is a line-break after each line and the first line(1250) is the "load" value,the first column of every is to be avoided the 2nd column is weight and the 3rd column is value.
One more thing I am using ubuntu distro which uses bash shell!