Reading from file, passing into function.

Thread Solved

Join Date: Feb 2007
Posts: 53
Reputation: edouard89 is an unknown quantity at this point 
Solved Threads: 2
edouard89's Avatar
edouard89 edouard89 is offline Offline
Junior Poster in Training

Reading from file, passing into function.

 
0
  #1
Dec 2nd, 2007
Hello All, well this is my first post after a while of inactivity.

I have run into a problem. I have an assignment for school, but when I debug my program using Dev C++(BloodShed) it give me a segmentation fault.

I am reading from a file that looks like this:
  1. 0001:Satriani:Joe:6:38.0
  2. 0002:Vai:Steve:1:44.5
  3. 0003:Morse:Steve:10:50.0
  4. 0004:Van Halen:Eddie:3:25.75
  5. 0005:Petrucci:John:8:42.25
  6. 0006:Beck:Jeff:3:62.0
i read from the file using this:
  1. fscanf(in, "%[^:]:%[ ^:]:%[ ^:]:%d:%lf", empNum, lName, fName, &pGrp, &hrsWorked);
* Not Sure weather this is causing the problem or not.

I have an array that contains pay rates.

I want to do this:
  1. rate = payRates[pGrp-1];
While debugging it gives me a segmentation fault error and high lites this line. Is it possible to do what I want to do? If it is not is there a way that I can achieve the same result?

Thank You,

Edouard
Last edited by edouard89; Dec 2nd, 2007 at 5:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 53
Reputation: edouard89 is an unknown quantity at this point 
Solved Threads: 2
edouard89's Avatar
edouard89 edouard89 is offline Offline
Junior Poster in Training

Re: Reading from file, passing into function.

 
0
  #2
Dec 2nd, 2007
OK it seems to me I have a problem with reading the file.
I hard coded some values to be passed into the calc_gross_to_pay() function and when I output to file it looks like this:
  1. Emp# Last Name First Name Gross Pay LTB EI
  2. 0001 ÿ" X 455.00 9.10 6.37
  3. Satr ÿ" X 455.00 9.10 6.37
  4. iani ÿ" X 455.00 9.10 6.37
  5. Joe ÿ" X 455.00 9.10 6.37
  6. 6 ÿ" X 455.00 9.10 6.37
  7.  
* I have removed the last 4 columns due to formatting of the page*

As you can see the data in the Emp# column is from the first line of the data file that I was trying to read. Is there anything I can do to fix the problem in reading the data?

Here is my code for reading and out puting the data to a file.
  1. main()
  2. {
  3. FILE *in;
  4. FILE *out;
  5.  
  6. double payRates[NUMRATES];
  7. int i, iMax = 0, iTotEmp;
  8. char empNum[5];
  9. char lName[13];
  10. char fName[13];
  11. char lineC[50];
  12. int pGrp;
  13. double hrsWorked;
  14. double grossPay, LTB, EI, CPP, FT, ded, netPay, junk, tNetPay, tGrossPay;
  15.  
  16. get_PR(payRates);
  17. in = fopen("employees.dat", "r");
  18. if(in == NULL)
  19. {
  20. printf("Error! Cant find the file employees.dat!\n");
  21. exit(1);
  22. }
  23.  
  24. fgets(lineC, sizeof(lineC) - 1 ,in);
  25.  
  26. while (! feof(in))
  27. {
  28. iMax++;
  29. fgets(lineC, sizeof(lineC) - 1 ,in);
  30. }
  31. rewind(in);
  32. out = fopen("payroll.dat", "w");
  33. if(in == NULL)
  34. {
  35. printf("Error! Cant find the file payroll.dat!\n");
  36. exit(1);
  37. }
  38. else
  39. {
  40. fprintf(out, "Emp# Last Name First Name Gross Pay LTB EI CPP FT Deductions Net Pay\n");
  41. }
  42.  
  43. for(i = 0; i<iMax;i++)
  44. {
  45. fscanf(in, "%4[^:]:%12[ ^:]:%12[ ^:]:%d:%lf\n", empNum, lName, fName, &pGrp, &hrsWorked);
  46.  
  47. grossPay = calc_gross_ot_pay(PRATE, HRSWRKED);
  48. //PRATE and HRSWRKED are defined using #define
  49. LTB = calc_LTB(grossPay);
  50. EI = calc_EI(grossPay);
  51. CPP = calc_CPP(grossPay);
  52. FT = calc_FT(grossPay);
  53. ded = LTB + EI + CPP + FT;
  54. netPay = grossPay - ded;
  55. tNetPay += netPay;
  56. tGrossPay +=grossPay;
  57. fprintf(out, "%-5s %-13s %-13s %11.2lf %11.2lf %11.2lf %11.2lf %11.2lf %11.2lf %11.2lf\n", empNum, lName, fName, grossPay, LTB, EI, CPP, FT, ded, netPay);
  58. }
  59. printf("# of employees: %d Total Net Pay %lf Total Gross Pay %lf", iMax, tNetPay, tGrossPay);
  60.  
  61. fclose(out);
  62. fclose(in);
  63. return 1;
  64. }
Last edited by edouard89; Dec 2nd, 2007 at 6:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading from file, passing into function.

 
0
  #3
Dec 2nd, 2007
Originally Posted by edouard89 View Post
i read from the file using this:
  1. fscanf(in, "%[^:]:%[ ^:]:%[ ^:]:%d:%lf", empNum, lName, fName, &pGrp, &hrsWorked);
I tested this and it works:
fscanf(in, "%[^:]:%[a-zA-Z0-9]:%[a-zA-Z0-9]:%d:%lf", empNum, lName, fName, &pGrp, &hrsWorked)
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,114
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 281
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Reading from file, passing into function.

 
1
  #4
Dec 2nd, 2007
The SPACEs in the scanset seem to be the problem. Remove them and see if that works for you.

Also, check out this information on
main()
while (! feof(in))

And what is the purpose of reading the file first? Just read it once.
I would also suggest rather than using fscanf() you switch to fgets()/sscanf() combo. If an error occurs with fscanf() it's harder to recover.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 53
Reputation: edouard89 is an unknown quantity at this point 
Solved Threads: 2
edouard89's Avatar
edouard89 edouard89 is offline Offline
Junior Poster in Training

Re: Reading from file, passing into function.

 
0
  #5
Dec 2nd, 2007
Thanks a lot for the help you have both given me!

It was a space problem and the way I was getting the Last and First Name.
I changed my scanf code to this and it works flawlessly:
  1. fscanf(in, "%[^:]:%[a-zA-Z a-zA-Z]:%[a-zA-Z]:%d:%lf\n", &empNum, &lName, &fName, &pGrp, &hrsWorked);
I also used the
  1. while(!=feof(in))
you had suggested, the reason I was reading it twice was to find the length but using your suggestion I just added a counter to get over that and just read it once.

Once Again thank you both for your input.

Now its just a matter of tweaking some functions to get the correct output values.
Last edited by edouard89; Dec 2nd, 2007 at 9:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 53
Reputation: edouard89 is an unknown quantity at this point 
Solved Threads: 2
edouard89's Avatar
edouard89 edouard89 is offline Offline
Junior Poster in Training

Re: Reading from file, passing into function.

 
0
  #6
Dec 2nd, 2007
Wow now in Unix it gives me a Syntax Error when I try to compile it...

Is there anything wrong with this?
  1. void get_PR(double rates[]); //Used to get pay rates and populates array.
Its right at the begining of the C source file:

  1. #include <stdio.h>
  2.  
  3. void get_PR(double rates[]); //Used to get pay rates and populates array.
  4. double calc_gross_ot_pay(double pRate, double hrsWrk);
  5. //Used to calculate gross pay of employee.
  6. double calc_LTB(double gross_pay); //Used to calculate Long Term Benefits.
  7. double calc_EI(double gross_pay); //Used to calculate Employee Insurance.
  8. double calc_CPP(double gross_pay); //Used to calculate Canadian Pension Plan.
  9. double calc_FT(double gross_pay); //Used to calculate Fed. & Prov. Taxes.
  10.  
  11. int main()
  12. {
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Reading from file, passing into function.

 
0
  #7
Dec 2nd, 2007
Looks ok to me. What compiler and what is the error message?
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 53
Reputation: edouard89 is an unknown quantity at this point 
Solved Threads: 2
edouard89's Avatar
edouard89 edouard89 is offline Offline
Junior Poster in Training

Re: Reading from file, passing into function.

 
0
  #8
Dec 2nd, 2007
I'm not sure which compiler, because I am remotely connecting to my school and using Unix from there. But it gives me a syntax error when I attempt to compile it.

*EDIT* I think its this:

VisualAge C++ Professional / C for AIX Compiler, Version 5
Last edited by edouard89; Dec 2nd, 2007 at 10:44 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Reading from file, passing into function.

 
1
  #9
Dec 2nd, 2007
The AIX native C compiler does not like the // comment style for .c files, since its considers it a C++ comment and not a C style comment. I think that is probably the reason you're getting the errors and you will either need to change it to the /* */ comment style or using the compile flag -qcpluscmt.

See if that helps.
Last edited by stilllearning; Dec 2nd, 2007 at 11:31 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 53
Reputation: edouard89 is an unknown quantity at this point 
Solved Threads: 2
edouard89's Avatar
edouard89 edouard89 is offline Offline
Junior Poster in Training

Re: Reading from file, passing into function.

 
0
  #10
Dec 2nd, 2007
Wow that was it... the comments...god...

Thanks A lot for that response!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC