?
Solved

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

Posted on 2004-10-20
9
Medium Priority
?
231 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 500 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

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!

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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 video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
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.
Suggested Courses
Course of the Month13 days, 14 hours left to enroll

800 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