ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
I used shell script (sed, awk etc) to copy some floating point data from a .txt file to a file named output_file. I am not able to access the data using a C program.
What i did ? i opened the file and read it first as float & then as double and printed it to screen.
float *data;
data = (double*) malloc(sizeof(double)); # i also used float
read(fp,data,sizeof(double));
printf("%g/f/e",*data); # g/f/e means i have used one of those
# code for opening file etc not included above as the file did open
Now the data it prints is not same as output_file which i can see by opening in Nedit or vim.
Later i declared data as double but changed the line
read(fp,data,7);
as i saw that the file was 12703 bytes &it has 1760 floating point no.'s in it. I made sure that the data were not on new line or if there was any space between each ft. point data..
The floating point data from the txt file as well as your copied file output_file are ASCII text files. Nedit and vim are ASCII text file editors. Your program is trying to read the data from the file as binary and therefore does not reproduce the same number. You need to read the data as text and then convert it to a floating point number.
Originally posted by michaelk The floating point data from the txt file as well as your copied file output_file are ASCII text files. Nedit and vim are ASCII text file editors. Your program is trying to read the data from the file as binary and therefore does not reproduce the same number. You need to read the data as text and then convert it to a floating point number.
Yes u r right. sometime back i started printingevery character and it displayed the data..........i read and printed 7 bytes. it gave me one fl. pt. data.
How do i convert these 7 characters to the same floating point no?
this seems to work as far as reading & printing is concerned. Now if i need to store these floating data in to variables, do i need to use atof () ?
can u give me a small hint abt the code?
Read the data from the file into a character array. Then use the atof function to convert the string to a number. How you read the data from the file depends on its format.
Originally posted by kapil_dev that seems to have worked without atof or atoi. i just wrote the variable f to another file and this time it worked. thanx guys
I mean micheal, i didnt need to use atof at all just read fscanf and then use variable f. will also try reading data into a char & using atof.
thanx
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.