Solved

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

Posted on 2004-10-20
9
230 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
[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
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
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 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.

707 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