Solved

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

Posted on 2004-10-20
9
218 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

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…
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 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.
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.

758 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

17 Experts available now in Live!

Get 1:1 Help Now