Solved

reading in variable number of objects per line

Posted on 2000-03-08
5
390 Views
Last Modified: 2010-04-10

For each line in my textfile, I can have either two, three, or four floats.  So the file looks somehwhat like:

1.2 1.0 9.9
1.2 34.4
4.9 3.2 7.0 3.2
1.2 34.4 3.2
4.9 3.2 7.0 3.2
1.2 1.0 9.9


I have a struct that has 4 fields in it - and I want to read in the first number into the first field, the second number into the second field, and the third and fourth numbers in to their respective fields if they exist.

How do I read in this type of a file easiest using cin (not fscanf)?  I thought about using getline(), but that means I have to perform string to float conversions don't I?  So is there an easier way?

0
Comment
Question by:VEngineer
[X]
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
  • 2
  • 2
5 Comments
 
LVL 3

Expert Comment

by:arnond
ID: 2598128
Try this:

---------------------------------------------------------
struct WhatEverYouCallYourStruct ReadLine()
{
 char tmp1[100],tmp2[100],tmp3[100],tmp4[100;
 struct WhateverYouCallYourStruct s;

 cin>>s.a;
 cin>>s.b;
 cin>>tmp3;
 if (!strcmp(tmp3,"\n"))
{
  s.c=0;
  s.d=0;  
  return (s);
}
 else
{
  s.c=atof(tmp3);
}
cin>>tmp4;
 if (!strcmp(tmp4,"\n"))
{
  s.d=0;  
  return (s);
}
 else
{
  s.d=atof(tmp4);
}
 return (s);
}
--------------------------------------------------

Note: this isn't tested so there may be a few bugs in it.

Hope this helps,
Arnon David.

0
 
LVL 2

Author Comment

by:VEngineer
ID: 2598137
doesn't cin skip over spaces and \n characters?

assume we have only two numbers on the first line

cin>>s.a;   // reads in the first num
cin>>s.b;   // reads in the second num
cin>>tmp3;  // reads in the first num on the next line

So I can't see how to get around this..
 
0
 
LVL 3

Expert Comment

by:arnond
ID: 2598243
in that case, after reading the first 2 numbers, would parse char by char, into a temp array. If I get a number, fine, assgin it to s.c if I get a '\n', assgin 0 to s.c and s.d. (and so on for the fourth number).
Of course, you should do the converting of the string to double (but that's easy).
to read a single char from cin, use the cin.get() function.

Arnon David.
0
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 50 total points
ID: 2599636
In cases like this you should use getline.

char buffer[256];
// get the line
cin.getline(buffer, 256, '\n');
// next you scan the data
double d1, d2, d3, d4;
int no_doubles_found = sscanf(buffer, "%lf %lf %lf %lf", &d1, &d2, &d3, &d4);
// no_doubles_found will be the number of doubles found in the line

Luc
0
 
LVL 2

Author Comment

by:VEngineer
ID: 2637889
How do you justify getting 256 characters?  What happens if the line happens to exceed 256 characters?
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

733 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