Line in text file to 1 to 10 different float variables.

Posted on 2003-03-31
Medium Priority
Last Modified: 2010-04-17
I have got a power-analyzer which is connected to a computer and it makes a text file with all of the measured variables which can be from 1-10+. Sample time is stored in the beginning of a line in the file and then comes each variable in a row with tab (\t) in between. Here is a example;

10:0:0     2,307763e+02     0,0e+00     5,000508e+01     0,0e+00
19:58:16     2,304e+02     1,102e+00     5,0019e+01     1,1350e+03

My problem is, it is easy to make the hole line to a string, but I have had difficulties to part the string up. I have tried scanf function and it doesn’t work (I think). Could you point out a solution to my problem???
Question by:dan22dan
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 8241112
Hi Dan,
If I understand your question correctly, this looks like a job for StringTokenizer.
Here are some snippets for you:

import java.util.StringTokenizer;

      StringTokenizer st;

      // line is a variable defined as each line of file.
      // sounds like you know how to do this
      while (line != null) {

          st = new StringTokenizer (line);

         // create a value for each element of the String
          value1 =  (st.nextToken(" ") ).trim();
          value2 =  (st.nextToken(" ") ).trim();
      }  // end while

Hope this helps!

Expert Comment

ID: 8241220

Oops!  I thought I was in the Java programming area!
(I was wondering why you were using scanf).
I assume you are using C?  If so, I think I can assist you.

Author Comment

ID: 8241415
This sure is a c++. So if you Jill could help me then please do!!!
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI


Accepted Solution

purrbert earned 200 total points
ID: 8241501

Since you are reading from a file you'll have a file pointer:

static FILE *fp_datain;
fp_datain = fopen('filename', "r" );

As you read each line from (you can use while...fgets).
You can load up the line into variables using the following:

fscanf (fp_datain, "%s\t", "%s\t","%s\t"..., variable1, variable2, variable3...);

Assuming that eacho of your variables is a string (%s) which looks like your case.  This should work, if not,
remove the \t in your fscanf.
Remember to rewind the file if you want to do any additional reads after performing this!

Hope this helps!
LVL 30

Assisted Solution

by:Mayank S
Mayank S earned 100 total points
ID: 8245163
If its needed: you can use also atof () to convert a string representation of a floating-point value to its numeric equivalent.


Expert Comment

ID: 9446624
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
Post your closing recommendations!  No comment means you don't care.

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question