| | |
Reading from file, passing into function.
Thread Solved |
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:
i read from the file using this:
* Not Sure weather this is causing the problem or not.
I have an array that contains pay rates.
I want to do this:
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
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:
C Syntax (Toggle Plain Text)
0001:Satriani:Joe:6:38.0 0002:Vai:Steve:1:44.5 0003:Morse:Steve:10:50.0 0004:Van Halen:Eddie:3:25.75 0005:Petrucci:John:8:42.25 0006:Beck:Jeff:3:62.0
C Syntax (Toggle Plain Text)
fscanf(in, "%[^:]:%[ ^:]:%[ ^:]:%d:%lf", empNum, lName, fName, &pGrp, &hrsWorked);
I have an array that contains pay rates.
I want to do this:
C Syntax (Toggle Plain Text)
rate = payRates[pGrp-1];
Thank You,
Edouard
Last edited by edouard89; Dec 2nd, 2007 at 5:10 pm.
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:
* 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.
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:
C Syntax (Toggle Plain Text)
Emp# Last Name First Name Gross Pay LTB EI 0001 ÿ" X 455.00 9.10 6.37 Satr ÿ" X 455.00 9.10 6.37 iani ÿ" X 455.00 9.10 6.37 Joe ÿ" X 455.00 9.10 6.37 6 ÿ" X 455.00 9.10 6.37
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.
C Syntax (Toggle Plain Text)
main() { FILE *in; FILE *out; double payRates[NUMRATES]; int i, iMax = 0, iTotEmp; char empNum[5]; char lName[13]; char fName[13]; char lineC[50]; int pGrp; double hrsWorked; double grossPay, LTB, EI, CPP, FT, ded, netPay, junk, tNetPay, tGrossPay; get_PR(payRates); in = fopen("employees.dat", "r"); if(in == NULL) { printf("Error! Cant find the file employees.dat!\n"); exit(1); } fgets(lineC, sizeof(lineC) - 1 ,in); while (! feof(in)) { iMax++; fgets(lineC, sizeof(lineC) - 1 ,in); } rewind(in); out = fopen("payroll.dat", "w"); if(in == NULL) { printf("Error! Cant find the file payroll.dat!\n"); exit(1); } else { fprintf(out, "Emp# Last Name First Name Gross Pay LTB EI CPP FT Deductions Net Pay\n"); } for(i = 0; i<iMax;i++) { fscanf(in, "%4[^:]:%12[ ^:]:%12[ ^:]:%d:%lf\n", empNum, lName, fName, &pGrp, &hrsWorked); grossPay = calc_gross_ot_pay(PRATE, HRSWRKED); //PRATE and HRSWRKED are defined using #define LTB = calc_LTB(grossPay); EI = calc_EI(grossPay); CPP = calc_CPP(grossPay); FT = calc_FT(grossPay); ded = LTB + EI + CPP + FT; netPay = grossPay - ded; tNetPay += netPay; tGrossPay +=grossPay; 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); } printf("# of employees: %d Total Net Pay %lf Total Gross Pay %lf", iMax, tNetPay, tGrossPay); fclose(out); fclose(in); return 1; }
Last edited by edouard89; Dec 2nd, 2007 at 6:51 pm.
•
•
•
•
i read from the file using this:
C Syntax (Toggle Plain Text)
fscanf(in, "%[^:]:%[ ^:]:%[ ^:]:%d:%lf", empNum, lName, fName, &pGrp, &hrsWorked);
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.
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
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
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
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:
I also used the 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.
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:
C Syntax (Toggle Plain Text)
fscanf(in, "%[^:]:%[a-zA-Z a-zA-Z]:%[a-zA-Z]:%d:%lf\n", &empNum, &lName, &fName, &pGrp, &hrsWorked);
C Syntax (Toggle Plain Text)
while(!=feof(in))
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.
Wow now in Unix it gives me a Syntax Error when I try to compile it...
Is there anything wrong with this?
Its right at the begining of the C source file:
Is there anything wrong with this?
C Syntax (Toggle Plain Text)
void get_PR(double rates[]); //Used to get pay rates and populates array.
C Syntax (Toggle Plain Text)
#include <stdio.h> void get_PR(double rates[]); //Used to get pay rates and populates array. double calc_gross_ot_pay(double pRate, double hrsWrk); //Used to calculate gross pay of employee. double calc_LTB(double gross_pay); //Used to calculate Long Term Benefits. double calc_EI(double gross_pay); //Used to calculate Employee Insurance. double calc_CPP(double gross_pay); //Used to calculate Canadian Pension Plan. double calc_FT(double gross_pay); //Used to calculate Fed. & Prov. Taxes. int main() {
•
•
Join Date: Oct 2007
Posts: 305
Reputation:
Solved Threads: 43
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.
See if that helps.
Last edited by stilllearning; Dec 2nd, 2007 at 11:31 pm.
![]() |
Similar Threads
- seek function (Perl)
- C++ Reading from a text file (C++)
- Setting an array and reading in a txt file backwards (C++)
- My own read function (C)
- Passing a filestream, how? (C++)
- home work help funcations and arry issues (C)
- Reading CSV in VC++ (C++)
- Response to time saving tips sticky (C++)
- Help Reading Info in Text File Into an Array (C++)
Other Threads in the C Forum
- Previous Thread: Need Help!..
- Next Thread: sprintf, leading zeros
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile copypdffile creafecopyofanytypeoffileinc createprocess() database dynamic execv fflush fgets file floatingpointvalidation fork forloop function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram homework i/o ide inches include infiniteloop input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft mysql number oddnumber open opendocumentformat openwebfoundation pdf pointer posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming stack standard strchr string suggestions systemcall test threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






