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
Solved

how can I use getline() to get multiple fields ?

Posted on 2004-10-20
9
227 Views
Last Modified: 2010-04-01
Hi experts,

I want to get all the fileds with the followings conditions:

1. If column 10 has AA then ignore
2. If column 30 has BB or CC then ignore
3. otherwise, write the file to a new file ( it means write the fileds to a new file without AA, BB, and CCfields )

So my code is like the followings:

String GetField ( )
{
...
}

main()
{
ifstream fin;
ofstream fout;
fin.open("testin.csv");
fout.open("testout.csv");

string line;

while ( getline(fin, line1) && getline(fin, line2) )
{
    string fn1 = GetField ( line, 10, ',' );
    string fn2 = GetField ( line, 30, ',' );
    if ( ( fn1 != "AA" )|| ( fn2 !="BB" ) || ( fn2 !="CC" ) )
     fout << line << endl;
}
}

I know something wrong with my code, but just dont know how to fix it. Anyone can help ? Thanks
0
Comment
Question by:justinY
9 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12363823
You are using getline to read from 'fin' to 'line1' and 'line2' variables (which I don't see declaration from)
Then you are applying GetField to 'line' which is not assigned to any value in your sample code

Maybe you need something similar to:
while ( getline(fin, line) )
{
    string fn1 = GetField ( line, 10, ',' );
    string fn2 = GetField ( line, 30, ',' );
    if ( ( fn1 != "AA" )|| ( fn2 !="BB" ) || ( fn2 !="CC" ) )
     fout << line << endl;
}


0
 

Author Comment

by:justinY
ID: 12364108
Hi,
its not working. I dont know why.
But if I do this  ---   if ( fn1 != "AA"  ) ---- then AA field got deleted  --- I dont understand why cannot delete AA,BB, CC
0
 
LVL 1

Expert Comment

by:gugario
ID: 12365184
Justin,

try to put some partial outputs in the middle of your code...
#include <iostream> on the top,
and then you can do cout << fn1 << endl; cout << fn2 << endl; cout << line << endl;... try to make sure you're reading in the right parameters each step of the way, that might help you debug.

Gustavo.
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 15

Expert Comment

by:efn
ID: 12365896
Your condition doesn't do what you said it is supposed to.

Say there is "XX" at column 10.  Then fn1 != "AA", so the whole if condition is true, so the line gets copied out regardless of what is in fn2.

According to your specification, the program should write a line out only if all three conditions are true, not just any one of them.  So you want ands rather than ors.

--efn
0
 
LVL 12

Accepted Solution

by:
OnegaZhang earned 125 total points
ID: 12366776
while ( getline(fin, line) )
{
    string fn1 = GetField ( line, 10, ',' );
    string fn2 = GetField ( line, 30, ',' );
    if ( ( fn1 != "AA" )&& ( fn2 !="BB" ) && ( fn2 !="CC" ) )
     fout << line << endl;
}
0
 

Author Comment

by:justinY
ID: 12370161
Thanks guys,
One last question, I want to load my file into buffer and then getline, how can I do that ?
 
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 12370524
you can use a stringstream object.

Dump all your file content to a stringstreem object and then use getline to retrieve line by line. You will need <sstream> header.

Have not tried but I guess is something like this:

// Will need this header
#include <sstream>  

stringstream str;
fin >> str;   // dump all your file into str

while ( getline(str, line) )   // Now extract lines from str
{
    string fn1 = GetField ( line, 10, ',' );
    string fn2 = GetField ( line, 30, ',' );
    if ( ( fn1 != "AA" )&& ( fn2 !="BB" ) && ( fn2 !="CC" ) )
     fout << line << endl;
}
0
 

Author Comment

by:justinY
ID: 12372416
Hey,
I have a compiling error here --- fin >> str
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

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…
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 additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

839 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