LinuxQuestions.org
Support LQ: Use code LQCO20 and save 20% on CrossOver Office
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 09-09-2005, 10:23 PM   #1
kapil_dev
LQ Newbie
 
Registered: Sep 2005
Posts: 11

Rep: Reputation: 0
reading file written by shell using C


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..

Any sugg
 
Old 09-09-2005, 11:06 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 9,920

Rep: Reputation: 338Reputation: 338Reputation: 338Reputation: 338
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.

ASCII was developed many many years ago during the teletype days for representing alphanumeric characters as numbers. http://www.webopedia.com/TERM/A/ASCII.html
 
Old 09-09-2005, 11:07 PM   #3
axial
LQ Newbie
 
Registered: Sep 2005
Location: Monroe, WI
Distribution: Slackware, Bluewhite64
Posts: 11

Rep: Reputation: 0
Try fscanf

something like:

while(!feof(fp)){
fscanf(fp,"%f",&f);
printf("%f\n",f);
}
 
Old 09-09-2005, 11:13 PM   #4
kapil_dev
LQ Newbie
 
Registered: Sep 2005
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
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.

ASCII was developed many many years ago during the teletype days for representing alphanumeric characters as numbers. http://www.webopedia.com/TERM/A/ASCII.html
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?
 
Old 09-09-2005, 11:21 PM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 9,920

Rep: Reputation: 338Reputation: 338Reputation: 338Reputation: 338
Look at the atof function. It is part of the stdlib library.
 
Old 09-09-2005, 11:31 PM   #6
kapil_dev
LQ Newbie
 
Registered: Sep 2005
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
Originally posted by axial
Try fscanf

something like:

while(!feof(fp)){
fscanf(fp,"%f",&f);
printf("%f\n",f);
}
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?
 
Old 09-09-2005, 11:44 PM   #7
michaelk
Moderator
 
Registered: Aug 2002
Posts: 9,920

Rep: Reputation: 338Reputation: 338Reputation: 338Reputation: 338
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.
 
Old 09-09-2005, 11:44 PM   #8
kapil_dev
LQ Newbie
 
Registered: Sep 2005
Posts: 11

Original Poster
Rep: Reputation: 0
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
 
Old 09-09-2005, 11:46 PM   #9
kapil_dev
LQ Newbie
 
Registered: Sep 2005
Posts: 11

Original Poster
Rep: Reputation: 0
Quote:
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
 
Old 09-10-2005, 12:11 AM   #10
michaelk
Moderator
 
Registered: Aug 2002
Posts: 9,920

Rep: Reputation: 338Reputation: 338Reputation: 338Reputation: 338
I'm tired. You are correct fscanf does parse the data according to the format "%f" and %f is a floating point number and so atof is not needed.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
reading/writing a shell password in gambas den_benne Programming 2 11-09-2005 03:58 PM
Gambas: reading a shell password Mattentaart Linux - Software 2 10-30-2005 05:39 AM
Reading text from a file using shell scripting. mrobertson Programming 11 06-29-2005 12:12 PM
Shell Script: Reading Pdf files funkymunky Programming 8 06-18-2004 03:34 PM
How to get last file written to directory kd5giv Linux - Networking 4 03-29-2004 02:26 PM


All times are GMT -5. The time now is 05:09 AM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration