I think that the problem is in lines 59-63

#include <stdio.h>
#include "genlib.h"
#include "ctype.h"
#include "simpio.h"
#include "string.h"


typedef struct{
    char name[30];
    long sell;
    double supply;

}domiT;


void ypologismoi(domiT stx[],int *p,FILE *infile);
void emfaniseis (domiT stx[],int *p,FILE *outfile);

main()
{

    int p;
    FILE *infile,*outfile;
    domiT stx[100];

ypologismoi(stx,&p,infile);
emfaniseis(stx,&p,outfile);    


system("pause");    
}
void ypologismoi(domiT stx[],int *p,FILE *infile)
{
    char file[15];
    int sarwseis;
    char comments[68];
    char name[30];
    char termch;
    long sell;

    while(TRUE)
    {
        printf("Enter file's name: ");

        gets(file);

    infile=fopen(file,"r");
    if(infile!=NULL) break;
    printf("Cannot open file %s !! Try againn",name);

   }


   *p=0;


    while(TRUE)
    {
        sarwseis=fscanf(infile," %30[^,], %ld,%68[^,],%c",name,&sell,comments,&termch);

        if(sarwseis==EOF) break;
        if(sarwseis!=4 || termch!='n')
        printf("ERROR try to find the error plz! " );


        strcpy(stx[*p].name,name);
        stx[*p].sell=sell;
        if(sell>=300000)
        stx[*p].supply=sell*5/100.0;
        else
        stx[*p].supply=sell*3/100.0;

        (*p)++;

    }

}        

void emfaniseis (domiT stx[],int *p,FILE *outfile)
{

    double sum;
    char newname[15];
    int i;



    sum=0;

    for(i=0;i<*p;i++)
    sum+=stx[i].supply;


    printf("Enter the NEW file's name: ");
    gets(newname);
    outfile=fopen(newname,"w"); 

    fprintf(outfile," %-30s %10s n","ONOMATEPONYMO","PROMHTHEIA");
    for(i=0;i<40;i++)
    fprintf(outfile,"%c","-");

    fprintf(outfile,"n");

    for(i=0;i<*p;i++)
    fprintf(outfile,"%-30s  %10g n",stx[i].name,stx[i].supply);

    for(i=0;i<40;i++)
       fprintf(outfile,"%c","-");

       fprintf(outfile,"n");

       fprintf(outfile,"%-30s %10g","SYNOLO PROMHTHEIWN:",sum);


}

Recommended Answers

We would have to get a copy of what file you're working with to even begin to understand what your problem is.

Jump to Post

If you think the problem is in lines 59~63, how about adding printf() to print out something at line 60, and between line 61 and 62? I mean if the bug is really there. (Simple debugging approach)

Jump to Post

indeed it's an infinity loop because of while(TRUE)

and it will only exit the loop if it statistfies

`if(sarwseis==EOF) 
        break;`

coz the other ifs are changing the value of the left variable

and on lines 62-67

`   if(sarwseis!=4 || termch!='n')
        printf("ERROR …
Jump to Post

All 10 Replies

Because while(TRUE) will give you an infinite loop. See here for more details.

yeah ...but i have a break point if you see...in line 61

We would have to get a copy of what file you're working with to even begin to understand what your problem is.

its a text file....and is not allowed to be upload in here

If you think the problem is in lines 59~63, how about adding printf() to print out something at line 60, and between line 61 and 62? I mean if the bug is really there. (Simple debugging approach)

indeed it's an infinity loop because of while(TRUE)

and it will only exit the loop if it statistfies

`if(sarwseis==EOF) 
        break;`

coz the other ifs are changing the value of the left variable

and on lines 62-67

`   if(sarwseis!=4 || termch!='n')
        printf("ERROR try to find the error plz! " );
        strcpy(stx[*p].name,name);
        stx[*p].sell=sell;`

is it intentional that you didn't add open and close braces

how about adding printf() to print out something at line 60, and between line 61 and 62? I mean if the bug is really there. (Simple debugging approach)

you should try to see the value of the variable sarwseis

Could the file still be open and without an EOF marker? I noticed that the sub that writes to the outfile doesn't close the stream, if the same file is then read would it have an EOF marker? just a thought.

Had another thought, see if this makes a difference:

 while(!infile.eof())
{
    sarwseis=fscanf(infile," %30[^,], %ld,%68[^,],%c",name,&sell,comments,&termch);
    if(sarwseis!=4 || termch!='n')
        printf("ERROR try to find the error plz! " );
    strcpy(stx[*p].name,name);
    stx[*p].sell=sell;
    if(sell>=300000)
        stx[*p].supply=sell*5/100.0;
    else
        stx[*p].supply=sell*3/100.0;
    (*p)++;
}

I've found the problem. Is in line 59-60 . I have to type in scanf ...%68[^\n] instead of %[^,]

that's good news please remember to mark this solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.