How to select data frm text file based on a condition

Reply

Join Date: Nov 2006
Posts: 18
Reputation: nirmala.s is an unknown quantity at this point 
Solved Threads: 0
nirmala.s nirmala.s is offline Offline
Newbie Poster

How to select data frm text file based on a condition

 
0
  #1
Nov 26th, 2006
My txt file is of the format:

Date Station Operator Task Action

and a list of values under each heading.. Now how do i write a C code to pull up records tht match a specific value under "TASK"...... say for example, if Task = Vendor Setup, then the result shld show me all the records whr task was equal to vendor setup...

Thanx in advance !!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to select data frm text file based on a condition

 
0
  #2
Nov 26th, 2006
show a couple sample file entries. what you posted tells us very little. But basically you have to read each line of the file and check the task column. there is no one way to do it, so you have to know what the file really looks like.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 18
Reputation: nirmala.s is an unknown quantity at this point 
Solved Threads: 0
nirmala.s nirmala.s is offline Offline
Newbie Poster

Re: How to select data frm text file based on a condition

 
0
  #3
Nov 26th, 2006
Sample entries wud be like :

Date Station Operator Task Action
12-08-2006 TOSHIBA-2F3B7FF TS TS Start TS-Main

12-08-2006 TOSHIBA-2F3B7FF TS TS End TS-Main
16-08-2006 TOSHIBA-2F3B7FF TS Vendor Setup Open Data Folder


Here i need to pull up records whr Task = Vendor Setup.... Cud anyone help me out plz.......
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to select data frm text file based on a condition

 
0
  #4
Nov 26th, 2006
you can use ifstream's extraction >> operator to get each of the first two strings, then call getline() to get the remainder of the line into a single string. Then just check the last string for what you want
  1. std::string date;
  2. std::string operator;
  3. std::string ts;
  4.  
  5. in >> date >> operator;
  6. getline(in,ts);
  7.  
  8. if( ts.find("Vendor Setup") != string::npos)
  9. {
  10. // text was found
  11. }

Of course you will have to put all the above in a loop so as to read the whole file.
Last edited by Ancient Dragon; Nov 26th, 2006 at 11:10 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 18
Reputation: nirmala.s is an unknown quantity at this point 
Solved Threads: 0
nirmala.s nirmala.s is offline Offline
Newbie Poster

Re: How to select data frm text file based on a condition

 
0
  #5
Nov 26th, 2006
Thnx.. but then when it finds few records tht match Task = Vendor Setup, then it shld print out the date and operator of those corresponding records..... How do i do diz... am sorry I'm a beginner in C, so bugging u too much.....
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 18
Reputation: nirmala.s is an unknown quantity at this point 
Solved Threads: 0
nirmala.s nirmala.s is offline Offline
Newbie Poster

Re: How to select data frm text file based on a condition

 
0
  #6
Nov 26th, 2006
Here's the code which i wrote but it gave me a whole bunch of errors.. Plz cud u help me out with this....

  1.  
  2. # include <stdio.h>
  3. # include <stdlib.h>
  4. # include <string.h>
  5.  
  6. int main()
  7. {
  8. FILE *in_file;
  9. char dataFile[20];
  10.  
  11. printf("Ënter file name");
  12. scanf ("%s", dataFile);
  13.  
  14. in_file = fopen(dataFile, "r");
  15.  
  16. std :: string date;
  17. std :: string operator;
  18. std :: string task;
  19.  
  20. in >> date >> operator;
  21. getline(in,task);
  22.  
  23. if (task.find("Vendor Setup") != string::npos)
  24.  
  25. {
  26. printf("Data found");
  27. }
  28.  
  29. return 0;
  30. }

The following are the errors which were listed when I compiled the code:

filterdaa.c
filterdaa.c(15) : error C2143: syntax error : missing ';' before ':'
filterdaa.c(16) : error C2143: syntax error : missing ';' before ':'
filterdaa.c(17) : error C2143: syntax error : missing ';' before ':'
filterdaa.c(19) : error C2065: 'in' : undeclared identifier
filterdaa.c(19) : error C2065: 'date' : undeclared identifier
filterdaa.c(19) : error C2065: 'operator' : undeclared identifier
filterdaa.c(19) : warning C4552: '>>' : operator has no effect; expected operato
r with side-effect
filterdaa.c(20) : error C2065: 'task' : undeclared identifier
filterdaa.c(22) : error C2224: left of '.find' must have struct/union type
filterdaa.c(22) : error C2065: 'string' : undeclared identifier
filterdaa.c(22) : error C2143: syntax error : missing ')' before ':'
filterdaa.c(22) : error C2059: syntax error : ')'
filterdaa.c(28) : error C2059: syntax error : 'return'
filterdaa.c(29) : error C2059: syntax error : '}'

I've no clue now as to wht has 2 be done..plz help me out...
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: How to select data frm text file based on a condition

 
0
  #7
Nov 26th, 2006
  1. std :: string date;
  2. std :: string operator;
  3. std :: string task;
I think you forgot to include iostream before using these -- they're part of the Standard Template Library.

Hmm... here you never declared in, which should be of type ifstream:
  1. in >> date >> operator;

Also, I think you should scrap fopen(), as using Mr. Dragon's suggestions, you have no need for the C functions to open files. Simply declare something like
  1. ifstream in("file.txt");
That takes care of opening the file, and then you won't get errors.

Hope this helps
"Technological progress is like an axe in the hands of a pathological criminal."

All my posts may be freely redistributed under the terms of the MIT license.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to select data frm text file based on a condition

 
0
  #8
Nov 26th, 2006
Oh, I see you are writing a C program, not a c++ program. What I posted will not work. You will have to use character arrays instead of those c++ string classes.

Read you text book about printf() function to find out how to print something on the screen. I would like to give you the complete solution to your assignment, but if I did that you would not learn a thing. Programming is very time consuming, not like just reading a novel.

I did not compile this, and hopefully it does not contain too many errors.
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3. # include <string.h>
  4.  
  5. int main()
  6. {
  7. FILE *in_file;
  8. char dataFile[20];
  9. char date[20];
  10. char operator[30];
  11. char ts[80];
  12.  
  13. printf("Ënter file name");
  14. scanf ("%s", dataFile);
  15.  
  16. in_file = fopen(dataFile, "r");
  17.  
  18. fsprintf(in_file,"%s %s", date, operator);
  19. fgets(ts,sizeof(ts), in_file);
  20.  
  21.  
  22. if ( strstr(ts,"Vendor Setup" != NULL)
  23.  
  24. {
  25. printf("Data found");
  26. }
  27.  
  28. return 0;
  29. }
Last edited by Ancient Dragon; Nov 26th, 2006 at 11:57 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 18
Reputation: nirmala.s is an unknown quantity at this point 
Solved Threads: 0
nirmala.s nirmala.s is offline Offline
Newbie Poster

Re: How to select data frm text file based on a condition

 
0
  #9
Nov 27th, 2006
Thnx Joe... I followed ur suggestions, the errors ve all disappeared but the thing is "how do i include iostream" ???? is it a header file... am very sorry, diz mite sound silly but then i seriously dunno much abt C..am just learnin it now by practice..

Now i got the error:

Fatal Error: C1083 : Cannot open include file - iostream.h
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,407
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1468
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: How to select data frm text file based on a condition

 
0
  #10
Nov 27th, 2006
Originally Posted by nirmala.s View Post
Thnx Joe... I followed ur suggestions, the errors ve all disappeared but the thing is "how do i include iostream" ???? is it a header file... am very sorry, diz mite sound silly but then i seriously dunno much abt C..am just learnin it now by practice..

Now i got the error:

Fatal Error: C1083 : Cannot open include file - iostream.h
Confusion abounds -- are you writing a C or a C++ program??????? The code Joe posted and the code I originally posted is C++, not C. They are two different languages, and you must know which you want before you can write your program.

iostream.h is an obsolete file. use the header file without the .h extension. For c++ programs only
  1. #include <iostream>
Last edited by Ancient Dragon; Nov 27th, 2006 at 12:10 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC