Solved

reading in variable number of objects per line

Posted on 2000-03-08
5
397 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: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

687 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