-
Manipulating Files
Im currently writting a small database program for school, and im having a lot of trouble manipulating data within the file. My program has to write to a file in the following manner
[Record #] Moviename,year,length,reference#
and that has been no problem
but now i need it to scan the file for a specific record number (all record numbers are written as [Record #00] etc), and i have no idea how to do it. Also, you have to be able to edit the information in the file, and i have no idea how to do that
can someone give me a website or some personal information on inter-file manipulation?
-
Ouch, I've been working on a VERY similar problem for the past couple weeks. My resolution was ultimately to use perl, but that's probably not going to work in your situation. I spent quite a bit of time reading through the regex.h header file, and the files associated with it. In my situation, the idea was a file with lines that looked like:
A:1:somefile:75:3
and I needed all of those in a seperate variable. The idea was to grab any number of chars up until the colon, and than between colons five times over. It seems to me that you want a similar approach, I just can't tell you how exactly to do it in C.
As far as writing to the file goes, fputs should work well. The syntax is:
fputs(pointer_to_string, FILE *stream);
I hope that I've been of some assistance. Please, do keep me posted as to how this progresses.
-starX
www.axisoftime.com
-
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 ;)