Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > collecting data from text file

Reply
Thread Tools

collecting data from text file

 
 
nick
Guest
Posts: n/a
 
      01-24-2006
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 

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-24-2006
nick wrote:
> 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
 
Reply With Quote
 
 
 
 
nick
Guest
Posts: n/a
 
      01-24-2006
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;
}

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-25-2006
nick wrote:
> 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


 
Reply With Quote
 
Roger Pate
Guest
Posts: n/a
 
      01-25-2006
On 24 Jan 2006 15:30:28 -0800, nick <(E-Mail Removed)> wrote:

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


--
Roger Pate <(E-Mail Removed)>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Collecting snmp data - threads, processes, twisted? Antal Rutz Python 0 02-02-2006 08:51 PM
Collecting form data when there is not a submit button antonyliu2002@yahoo.com ASP .Net 5 09-23-2005 01:57 PM
Collecting Data on one form and outputing it on another form Michael Mitchell via DotNetMonster.com ASP .Net 0 01-23-2005 03:14 PM
URgent!!! Collecting data from server side =?Utf-8?B?U2lsZWVzaA==?= ASP .Net 1 11-03-2004 07:15 PM
Collecting data from website ? Andy ASP .Net 1 08-05-2004 01:28 PM



Advertisments