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

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.

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

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.

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

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

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

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

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

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

#include <iostream>

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

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

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.

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

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

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.

Joe,

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

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

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

Am sorry WaltP.. I was trying to check the difference between fprintf and fsprintf

my apologies about the code, I did not compile or test it. There is no such function as fsprintf() -- it should have been fscanf().

Thts ok..Mr. Dragon... Let me chk how it goes and keep you posted !!!

You might be using a file name greater than 20 characters long.

Increase the size of datafile to say 40 and try again as in...

char dataFile[40];

Bala,

Nope..my file name is just 6 char in length...

I could sugges reading the text file a single character at a time and checking each word but that would be cumbersome.

Tell me, u have a pre-exisiting file or the data might get changed in the file later on.

its pre-existing..means its a txt copy of a log file frm the database !!!

Can i have an idea of the platform u r coding in?

That might help me to answer u better ..

But what's wrong with the *revised* code from Ancient Dragon? Should work pretty good?

regards Niek

Am tryin 2 code in C language..windows OS.. Timberline database

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.