Click to See Complete Forum and Search --> : have problem using C programming in writing data into a text file!


gechin
October 2nd, 2002, 08:56 PM
hello everybody! i have a problem using C in writing data into a text file.
actually i can writen the data into the text file already by using the code:
___________________________________________
fprintf(filename,"bil \tname \tsalary \tO.T \t total");
while(!EOF)
fprintf(filename,"%d \t%c%c%c \t0.2%f \t0.2%f \t0.2%f",bil,name[0],name[1],name[2],salary,ot,total);
_________________________________________________

the problem i facing is the data was not properly arrange. for example:

bil name salary O.T total
1 mch 10.00 10.00 20.00
2 efg 20.00 20.00 40.00
3 jkl 100.00 100.00 200.00
4 opq 400.00 100.00 300.00

the problem occur when the value of salary, O.T and total is more than hundred and even thousand. what code must i write in order to get this result :

bil name salary O.T total
1 mch 10.00 10.00 20.00
2 efg 20.00 20.00 40.00
3 jkl 100.00 100.00 200.00
4 opq 400.00 100.00 300.00
5 mnb 1000.00 500.00 1500.00

which is nice and easy to read.

thankz,
gechin

gechin
October 2nd, 2002, 09:09 PM
after looking at my posting, i found that the data was not display like what i type in the first place.so i have to explain a litter bit more so that you all will know what exactly i'm trying to say.
i want my data to arrange in 5 colums. example for 3th colum:
_______________
salary
100
10
300
900
1200
_________________
and the 4th colum
_________________
O.T
100
20
400
200
_________________


but some of the data did not start at it own colums. this happend when the "float" value is more that hundred or even more.
i hope you all understand what am i trying to say here.

thankz again,
gechin

Andreas Masur
October 3rd, 2002, 05:07 AM
Originally posted by gechin
fprintf(filename,"%d \t%c%c%c \t0.2%f \t0.2%f \t0.2%f",bil,name[0],name[1],name[2],salary,ot,total);

Well...I do not know it this will fix all of your problems since I took only a quick look but the above line should be...

fprintf(filename,"%d\t%c%c%c\t%0.2f\t%0.2f\t%0.2f",bil,name[0],name[1],name[2],salary,ot,total);

brettskey
October 3rd, 2002, 03:52 PM
A common mistake when looking at tab-delimited files is tab settings. If you are looking at your file in a text editor that has the tabs set to every 8 characters, for example, then putting a value that's 8 characters long will cause the remaining values in the row to be right-shifted by one column.

If you're not sure, try opening it up in Excel or change your tab settings to be really big.

gechin
October 8th, 2002, 11:46 PM
well thank for the solution that you all give. i have found a more simple way to solve it. That is:

fprintf(filename,"%d \t%c%c%c \t8.2%f \t8.2%f \t8.2%f",bil,name[0],name[1],name[2],salary,ot,total);


that is replace the 0 with an integer such as 8. this will allocate 8 space for us to fill in our value.

thanks,
gechin