Solved

reading in variable number of objects per line

Posted on 2000-03-08
5
368 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
How do you justify getting 256 characters?  What happens if the line happens to exceed 256 characters?
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

10 Experts available now in Live!

Get 1:1 Help Now