944,081 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9471
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 26th, 2006
0

How to select data frm text file based on a condition

Expand Post »
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 !!!!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nirmala.s is offline Offline
18 posts
since Nov 2006
Nov 26th, 2006
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Nov 26th, 2006
0

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

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.......
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nirmala.s is offline Offline
18 posts
since Nov 2006
Nov 26th, 2006
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Nov 26th, 2006
0

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

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.....
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nirmala.s is offline Offline
18 posts
since Nov 2006
Nov 26th, 2006
0

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

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...
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nirmala.s is offline Offline
18 posts
since Nov 2006
Nov 26th, 2006
0

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

  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
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Nov 26th, 2006
0

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

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005
Nov 27th, 2006
0

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

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
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nirmala.s is offline Offline
18 posts
since Nov 2006
Nov 27th, 2006
0

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

Click to Expand / Collapse  Quote originally posted by nirmala.s ...
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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,957 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Using a laser printer with C
Next Thread in C Forum Timeline: c home work





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC