collecting data from text file

N

nick

hi all,
Is there anyone who can help in finishing this problem?
I’m checking a huge text file for some data, there are lots of lines
where some of
these lines lets say start with the word "comp". i need only to check
these
lines starting with comp and ignore the others.

Let's say we have: inputA, inputB.

ex:
...
...
...
comp "inputA[4]" ... = ...."p25";
comp "inputB[3]" ... = ...."f32";
comp "inputA[5]" ... = ...."f45";
comp "inputA[1]" ... = ...."g234";
comp "inputA[3]" ... = ...."p65";
comp "inputB[0]" ... = ...."p23";
comp "inputB[1]" ... = ...."g54";

...
...
...
...
Now it should print the numbers/letters between the second quotations
but in a
certain order as follows:
First it has to follow the above pattern i.e: inputA then inputB and
sort their value (desending) .

So basically the answer should look like:
The value of :inputA[4] inputA[3] inputA[2] inputA[1] inputA[0]
inputB[4]…....inputB[0]

Here in this case the final output should look like: f45 p25 p65 ..
g234 .. f32
...954 p23

Any help will be appreciated.
I couldn't have it to work properly, it prints them randomly ïŒ
 
V

Victor Bazarov

nick said:
hi all,
Is there anyone who can help in finishing this problem?

"Finishing"? You mean you have it started? Where? I mean, if you want
us to finish something, we need to see the start, don't you think?

See FAQ section 5, http://www.parashift.com/c++-faq-lite/
[.. description redacted ..]

Any help will be appreciated.
I couldn't have it to work properly, it prints them randomly ïŒ

Is that a question? I don't know the answer. Could you rephrase it?

V
 
N

nick

thanks for your reply :)
i apologize for not sending my code, i should have included my code but
i've been ading and deleting over and over till it became meaning less.
so i have to start from the begining.


int main(int argc, char *argv[])
{
string line;

ifstream myfile ("c:\\documents and
settings\\owner\\Desktop\\myfile.txt");
if (myfile.is_open())
{
while (! myfile.eof ( ) )
{
getline (myfile,line);
cout << line << endl;

}
myfile.close();
}

else cout << "Unable to open file"<<endl;



system("PAUSE");
return 0;
}
 
V

Victor Bazarov

nick said:
thanks for your reply :)
i apologize for not sending my code, i should have included my code
but i've been ading and deleting over and over till it became meaning
less. so i have to start from the begining.

Is http://www.cplusplus.com/doc/tutorial/files.html where you "started
from the begginning"? Make sure you copy all the stuff correctly.
int main(int argc, char *argv[])
{
string line;

ifstream myfile ("c:\\documents and
settings\\owner\\Desktop\\myfile.txt");
if (myfile.is_open())
{
while (! myfile.eof ( ) )
{
getline (myfile,line);
cout << line << endl;

}
myfile.close();
}

else cout << "Unable to open file"<<endl;



system("PAUSE");
return 0;
}

The code you provided doesn't compile. Get it to compile. Add comments
where you think you're doing whatever you think you're doing. If it does
not work as intended, explain how its behaviour is different from what
you expect.

What do you need to do? You need to do something with the lines that
start with "comp". How do you check if a line (string) starts with some
specific text? You can use 'substr' member and equality operator.

Once you find those particular lines, then what? You need to extract
some substrings, right? Again, use 'substr'. Which substrings? You need
to look for the quotation marks, right? Use 'find'...

And read the FAQ, section 5.

V
 
R

Roger Pate

thanks for your reply :)
i apologize for not sending my code, i should have included my code but
i've been ading and deleting over and over till it became meaning less.
so i have to start from the begining.


int main(int argc, char *argv[])
{
string line;

ifstream myfile ("c:\\documents and
settings\\owner\\Desktop\\myfile.txt");
if (myfile.is_open())
{
while (! myfile.eof ( ) )
{
getline (myfile,line);
cout << line << endl;

}
myfile.close();
}

else cout << "Unable to open file"<<endl;



system("PAUSE");
return 0;
}

Using !eof() like above is an error. See
http://www.gnomesane.net/code/doc/noteof/
Using getline as the loop condition means that if a line cannot be read,
the condition will fail (because getline returns the stream and the stream
would be in a not-good state). Checking eof() after the stream has failed
will tell you whether it failed due to eof; however, the only way getline
could fail is eof. (An out-of-memory condition with the default allocator
will throw an exception.)

#include <fstream>
#include <string>
#include <iostream>

int main( /*there's no reason to even have parameters if you don't use
them*/ ) {
using namespace std;

ifstream in ("c:/documents and settings/owner/Desktop/myfile.txt");

if ( !in ) {
cerr << "Unable to open file\n";
return 1;
}

for ( string line; getline( in, line ); ) {
cout << line << '\n';
// do something with line
}

// if you want the console to stay open after the program runs,
// run it from a console (e.g. run cmd and type the program's name)
return 0;

// no need for in.close(), ifstream will close itself when it destructs
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,869
Messages
2,569,911
Members
46,168
Latest member
wql4450989

Latest Threads

Top