Solved

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

Posted on 2004-10-20
9
223 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.
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.

810 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