943,862 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 3386
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Dec 2nd, 2007
0

Reading from file, passing into function.

Expand Post »
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.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
edouard89 is offline Offline
53 posts
since Feb 2007
Dec 2nd, 2007
0

Re: Reading from file, passing into function.

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
edouard89 is offline Offline
53 posts
since Feb 2007
Dec 2nd, 2007
0

Re: Reading from file, passing into function.

Click to Expand / Collapse  Quote originally posted by edouard89 ...
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)
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Dec 2nd, 2007
1

Re: Reading from file, passing into function.

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.
Moderator
Reputation Points: 3278
Solved Threads: 892
Posting Sage
WaltP is offline Offline
7,718 posts
since May 2006
Dec 2nd, 2007
0

Re: Reading from file, passing into function.

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
edouard89 is offline Offline
53 posts
since Feb 2007
Dec 2nd, 2007
0

Re: Reading from file, passing into function.

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. {
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
edouard89 is offline Offline
53 posts
since Feb 2007
Dec 2nd, 2007
0

Re: Reading from file, passing into function.

Looks ok to me. What compiler and what is the error message?
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,951 posts
since Aug 2005
Dec 2nd, 2007
0

Re: Reading from file, passing into function.

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.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
edouard89 is offline Offline
53 posts
since Feb 2007
Dec 2nd, 2007
1

Re: Reading from file, passing into function.

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.
Reputation Points: 161
Solved Threads: 43
Posting Whiz
stilllearning is offline Offline
309 posts
since Oct 2007
Dec 2nd, 2007
0

Re: Reading from file, passing into function.

Wow that was it... the comments...god...

Thanks A lot for that response!
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
edouard89 is offline Offline
53 posts
since Feb 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Need Help!..
Next Thread in C Forum Timeline: sprintf, leading zeros





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC