Niek,
I compiled and executed the revised code of Mr.Dragon's..it prompts me 2 enter the filename..and when i enter it..it jus closes off with a msg ' An error has encountered and the application needs to close"----dont knw why...
Niek,
I compiled and executed the revised code of Mr.Dragon's..it prompts me 2 enter the filename..and when i enter it..it jus closes off with a msg ' An error has encountered and the application needs to close"----dont knw why...
Did you do exactly as you were told:
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
int main()
{
FILE *in_file;
char dataFile[20] = { '\0' };
char date[20] = { '\0' };
char operator[30] = { '\0' } ; // you're using a C keyword!
char ts[80] = { '\0' } ;
printf("Enter file name");
scanf ("%s", dataFile);
in_file = fopen(dataFile, "r");
fprintf(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;
}
More importantly, do you have a file with the name which you are entering ? And does it contain data in the specified format ?
Make all these things sure first and then repost if necessary.
There is no such function as fsprintf()
That's what confused me. It appears that fsprintf() does exist:
http://www.google.com/search?q=cache:dvJeI37CwEgJ:ou800doc.caldera.com/en/man/html.3S/fprintf.3S.html+fsprintf&hl=en&ct=clnk&cd=1&client=firefox-a
Did you do exactly as you were told:
# include <stdio.h> # include <stdlib.h> # include <string.h> int main() { FILE *in_file; char dataFile[20] = { '\0' }; char date[20] = { '\0' }; char operator[30] = { '\0' } ; // you're using a C keyword! char ts[80] = { '\0' } ; printf("Enter file name"); scanf ("%s", dataFile); in_file = fopen(dataFile, "r"); fprintf(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; }
More importantly, do you have a file with the name which you are entering ? And does it contain data in the specified format ?
Make all these things sure first and then repost if necessary.
~s.o.s~, your code won't compile. operator
is a C keyword, so it will need to be named something different. And the line
fprintf(in_file,"%s %s", date, operator);
should be using fscanf(), not fprintf().
[edit]After testing the code a little bit, I think you should add a statement to check that the file's handle is valid.
if (in_file == NULL) {
// invalid file; do what you have to but don't let the program try to read from this handle!
}
If it's invalid, then the filename is probably incorrect, and the program could crash if you try to use fscanf().
...or was that supposed to be showing what Nirmala is using right now? :cheesy:
Ah...I thought maye you had changed the code and the OP had totally missed it...maybe i was wrong ;)
Looks like you can't trust anyone these days... :)
Ah...I thought maye you had changed the code and the OP had totally missed it...maybe i was wrong ;)
Looks like you can't trust anyone these days... :)
Yeah... I like to comment the errors in, as simply dumping the fixed code to the OP doesn't really teach them anything. :)
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.