954,496 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to select data frm text file based on a condition

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

nirmala.s
Newbie Poster
18 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

nirmala.s
Newbie Poster
18 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

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

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.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

nirmala.s
Newbie Poster
18 posts since Nov 2006
Reputation Points: 10
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....

# 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...

nirmala.s
Newbie Poster
18 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
std :: string date;
std :: string operator;
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 :

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

ifstream in("file.txt");

That takes care of opening the file, and then you won't get errors.

Hope this helps

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

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.

# 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;
}
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

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

nirmala.s
Newbie Poster
18 posts since Nov 2006
Reputation Points: 10
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

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. Forc++ programs only

#include <iostream>
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Hmm... you should decide if you want to switch your program entirely to C++ or stay with C... if you choose the latter, then follow Mr. Dragon's previous post, which uses fsprintf() instead of ifstream.

If you want to switch to C++, then all your printf() statements should be changed to std::cout statements, and then you'll need to put the line #include <iostream> at the top of your program. But you should definitely not go in-between, and get a horrid mixture of C and C++.

It's probably best to go stay with C, as that is what you're learning right now...

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Yaa.. i'll stick to C.. do as per urs and Mr.Dragon's advice, chk out how it goes...Thnx a lot again both of u all !!!

nirmala.s
Newbie Poster
18 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
Yaa.. i'll stick to C.. do as per urs and Mr.Dragon's advice, chk out how it goes...Thnx a lot again both of u all !!!


It all depends on the class you are taking -- is it C or C++ ? You have to write your program in the language required by your instructor. And as Joe mentioned, do not mix the two languages.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Am not attendin any classes, I've started learnin C on my own... and one of my frndz gave me diz scenario and am brkin my head for it !!! I prefer 2 do diz in C first, get 2 learn it properly and then move on...

nirmala.s
Newbie Poster
18 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Hi,

I tried 2 compile the code but then..it gave me diz error:

filterdaa.c
filterdaa.c(22) : warning C4047: 'function' : 'const char *' differs in levels o
f indirection from 'int'
Microsoft (R) Incremental Linker Version 7.10.3077
Copyright (C) Microsoft Corporation. All rights reserved.

/out:filterdaa.exe
filterdaa.obj
filterdaa.obj : error LNK2019: unresolved external symbol _fsprintf referenced in function _main
filterdaa.exe : fatal error LNK1120: 1 unresolved externals

Then i tried to remove the "fsprintf" statement completely... and then on compiling, there wasnt any error..it just gave me a warning msg but it give in the .obj and .exe files.
When i tried to run the program.... it asked me to Ënter file name" but when i entered the name of the file - it said the program has caused an error and that it needs to close....

nirmala.s
Newbie Poster
18 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Just tried running Ancient Dragon's code through gcc... seems like he's got a number of "errors" in there. :D

# include <stdio.h>
# include <stdlib.h>
# include <string.h>
 
int main()
{
  FILE *in_file;
  char dataFile[20];
  char date[20];
  char operator[30];   // you're using a C keyword!
  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) // missing bracket
  
   {
      printf("Data found");
   }
  
   return 0;
}


The only error that I couldn't get to go away was the compiler complaining about fsprintf() being undefined. I don't get it, as I never thought it was outdated or anything, and stdio.h is being included. It's odd, as snprintf() works and a lot of the other functions relating to the fprint series.

I'll let the expert (Mr. Dragon) answer this one.

John A
Vampirical Lurker
Team Colleague
7,630 posts since Apr 2006
Reputation Points: 2,240
Solved Threads: 339
 

Joe,

Cud u lemme knw whtz the difference between FPRINTF and FSPRINTF ???

nirmala.s
Newbie Poster
18 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 

Also Joe,

I tried to compile and run Mr.Dragon's code by adding the bracket and using fprintf instead of fsprintf... it compiled without any errors.. but when i run the program - it asks me 2 enter the filename and when i entered it, it jus gave me an error msg and closed off completely !!!!

nirmala.s
Newbie Poster
18 posts since Nov 2006
Reputation Points: 10
Solved Threads: 0
 
chunkmartinez
Junior Poster
115 posts since Jun 2006
Reputation Points: 14
Solved Threads: 0
 

Joe,

Cud u lemme knw whtz the difference between FPRINTF and FSPRINTF ???

I'm finding it difficult to read your posts ithe all the made-up words you seem to like. Please read the FAQ section called Keep It Clean

WaltP
Posting Sage w/ dash of thyme
Moderator
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You