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
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
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
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
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
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
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 !!!
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
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
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
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 ???
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
10,505 posts since May 2006
Reputation Points: 3,348
Solved Threads: 944