[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 179
  • Last Modified:

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

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???
0
dan22dan
Asked:
dan22dan
2 Solutions
 
purrbertCommented:
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!
Jill
0
 
purrbertCommented:
Dan,

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.
Jill
0
 
dan22danAuthor Commented:
This sure is a c++. So if you Jill could help me then please do!!!
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
purrbertCommented:
Gotcha!

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!
Jill
0
 
Mayank SAssociate Director - Product EngineeringCommented:
If its needed: you can use also atof () to convert a string representation of a floating-point value to its numeric equivalent.

Mayank.
0
 
CleanupPingCommented:
dan22dan:
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 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now