Manipulating Files

This is a discussion on Manipulating Files within the C Programming forums, part of the General Programming Boards category; Im currently writting a small database program for school, and im having a lot of trouble manipulating data within the ...

  1. #1
    JoshuaNSW
    Guest

    Unhappy 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?

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    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

  3. #3
    0x01
    Join Date
    Sep 2001
    Posts
    88
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 10:52 PM
  2. Working with muliple source files
    By Swarvy in forum C++ Programming
    Replies: 1
    Last Post: 10-02-2008, 09:36 AM
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21