Code:
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define EXIT_FAILURE 1;
#define EXIT_SUCCESS 0;
int main(int argc, char *argv[])
{
FILE *fp; // File Handle
char fpname[] = "Records.txt", r_string[] = "record #";
const int MAX = 150;
char line[MAX] = {0};
if(argc < 2){
printf(" Example: Ref.exe [record#]\n");
return EXIT_FAILURE;
}
fp = fopen(fpname,"rb");
if(!fp){
printf(" Cannot find or open %s", fpname);
return EXIT_FAILURE;
}
while(fgets(line, MAX, fp)!=NULL)
{
if(strstr(line,r_string)
!= NULL && strstr(line,argv[1]) != NULL)
printf("%s",line);
}
return EXIT_SUCCESS;
}
Ok, create a file called "Records.txt", then place in stuff like the following.
[record #01] Moviename : BlahBlah Year : 2002 Length : 2hrs
[record #02] Moviename : Aname Year : 2001 Length : 1hr 30 min
.................., ................., ., .........., ........, ., ......., .........., ., ....etc
Then run the program like so,
Example : ref.exe 01
Output : [record #01] Moviename : BlahBlah Year : 2002 Length : 2hrs
Hmm... I just wrote this code, and I am not confident in anyway that this is the proper way to do what your asking(your first question).. its kinda cheap but it works