| | |
How to select data frm text file based on a condition
![]() |
•
•
Join Date: Nov 2006
Posts: 18
Reputation:
Solved Threads: 0
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 !!!!
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 !!!!
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.
•
•
Join Date: Nov 2006
Posts: 18
Reputation:
Solved Threads: 0
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.......
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.......
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
Of course you will have to put all the above in a loop so as to read the whole file.
C Syntax (Toggle Plain Text)
std::string date; std::string operator; std::string ts; in >> date >> operator; getline(in,ts); if( ts.find("Vendor Setup") != string::npos) { // text was found }
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.
•
•
Join Date: Nov 2006
Posts: 18
Reputation:
Solved Threads: 0
Here's the code which i wrote but it gave me a whole bunch of errors.. Plz cud u help me out with this....
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...
c Syntax (Toggle Plain Text)
# include <stdio.h> # include <stdlib.h> # include <string.h> int main() { FILE *in_file; char dataFile[20]; printf("Ënter file name"); scanf ("%s", dataFile); in_file = fopen(dataFile, "r"); std :: string date; std :: string operator; std :: string task; in >> date >> operator; getline(in,task); if (task.find("Vendor Setup") != string::npos) { printf("Data found"); } return 0; }
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...
C Syntax (Toggle Plain Text)
std :: string date; std :: string operator; std :: string task;
iostream before using these -- they're part of the Standard Template Library.Hmm... here you never declared
in, which should be of type ifstream: C Syntax (Toggle Plain Text)
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
C Syntax (Toggle Plain Text)
ifstream in("file.txt");
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.
All my posts may be freely redistributed under the terms of the MIT license.
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.
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.
C Syntax (Toggle Plain Text)
# include <stdio.h> # include <stdlib.h> # include <string.h> int main() { FILE *in_file; char dataFile[20]; char date[20]; char operator[30]; char ts[80]; printf("Ënter file name"); scanf ("%s", dataFile); in_file = fopen(dataFile, "r"); fsprintf(in_file,"%s %s", date, operator); fgets(ts,sizeof(ts), in_file); if ( strstr(ts,"Vendor Setup" != NULL) { printf("Data found"); } return 0; }
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.
•
•
Join Date: Nov 2006
Posts: 18
Reputation:
Solved Threads: 0
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
Now i got the error:
Fatal Error: C1083 : Cannot open include file - iostream.h
•
•
•
•
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
iostream.h is an obsolete file. use the header file without the .h extension. For c++ programs only
C Syntax (Toggle Plain Text)
#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.
![]() |
Other Threads in the C Forum
- Previous Thread: Using a laser printer with C
- Next Thread: c home work
| Thread Tools | Search this Thread |
#include * ansi array arrays asterisks bash binarysearch calculate centimeter changingto char character convert copyanyfile copyimagefile creafecopyofanytypeoffileinc createprocess() database dynamic execv fgets file floatingpointvalidation fork framework function getlogicaldrivestrin givemetehcodez grade gtkwinlinux histogram ide inches include infiniteloop initialization input interest intmain() iso keyboard km license linked linkedlist linux list looping lowest matrix meter microsoft number oddnumber open opendocumentformat openwebfoundation owf pdf pointer pointers posix power probleminc process program programming pyramidusingturboccodes radix read recursion recv recvblocked research reversing scheduling segmentationfault send sequential single socket socketprogramming standard strchr string suggestions systemcall test testautomation testing threads turboc unix urboc user variable whythiscodecausesegmentationfault win32api windowsapi






