Solved

reading in variable number of objects per line

Posted on 2000-03-08
5
377 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

863 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

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now