Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Few Questions Regarding Fscanf

align scanf

  • Please log in to reply
14 replies to this topic

#13 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 02 July 2012 - 10:15 AM

Yeah, the logic/pseudo-code I said does that. Read the logic carefully.
  • 0

#14 Agent001

Agent001

    CC Regular

  • Member
  • PipPipPip
  • 41 posts

Posted 02 July 2012 - 10:35 AM

Ok, i came up with this bit of code.
I'm using strings, because i'm working with product names this time.
It doesn't work with the last elements in the file, for some reason...

while(!feof(dat1)){
/*read the first line and **ign start values to help variables */
if(i<1){
fscanf (dat1, "%s %d \n",&name,&amount);
strcpy(help,name);totalamount=amount;

/*if we're reading line greater than 1*/
if(i>=1){
fscanf (dat1, "%s %d \n",&name,&amount);

/*we compare the two string, that contain the products name*/
if(!strcmp(help,name)){
totalamount+=amount; /*if names are the same we increase the amount */
}
else{ /*if we printf what we have done so far */
fprintf(dat2,"%s %d %d\n",help,totalamount);
strcpy(help,name);totalamount=amount;
/*then i copy the current string into help one for further comparison*/
}}
i++;
}

Can you help with this?

EDIT: The logic seems good, but this way, i dont print the last element from the file.
Because if i check the last two lines, and they don't match, i'll print the upper one...

Same way if i have a series of mathching lines that are in the end of file, they won't print because the program prints everything except the last product (or set of same products).

And at the moment i can seem to figure a workaround of this problem...
Help would be appreciated, as so many times so far...
  • 0

#15 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 04 July 2012 - 06:15 AM

The last line is print after the while loop in my pseudo-code.
If (product_id is not -1)  // in your case product_id should be product_name
begin
    print both product_id and product value in output file
end

In your case, after the while loop, you should check whether your 'help' variable has a value in it: if yes print the help/totalAmount pair in the output file.
  • 0





Also tagged with one or more of these keywords: align, scanf

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download