Thread: learning to work on files

  1. #1
    Registered User
    Join Date
    Sep 2008
    Posts
    53

    learning to work on files

    hi.
    as this is my first post here i would like to introduce myself and say hi first before i ask my question. i have really bad eyesight at times so i mispell a lot and my english is maybe not so good all times ,i hope it is not to bad to read my posts .

    i have been learning c by myself for sometime and i have no experience from before as such. i use the books and online material i can find but i have not been able to find out how to manipulate the content of open files .

    i know how to open a file and parse it for html tags and such operations where a filename or more is given on the commandline and then parsed for as an example a simple wordcount program or a program that deletes c commentaries '/* ' and '*/' from a a file.

    i want to learn more on file handling and one thing i cant find anything on is how to as example if i have a file and i need to set the cursor on a given position or delete an entry of one line or more in a txt file ,could be an simple commandline address book application i been thinking about trying to write.

    thanks for the time .

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Welcome, cmay!

    Some file functions to check out - fgetpos(), fsetpos(), ftell(), fseek(). There is an example of multiple file positions being done in binary file mode, just 2 weeks ago.

    Should be one page 5 or 6 by now. The title is "Performance Issues", and look at the last few posts in the thread.

    You can also search for those terms, using the "Search" part of the forum header. There was an example program that used fseek() just 3 or 4 days ago, where the poster wanted to read in a file on the drive, backwards, and write it out to another file. (A school assignment, I believe).

    So look around, and glad to have you with us. Your English is good.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Making changes to an open text file is complicated by the fact that you probably want to work line by line, but file positioning (eg using fseek) works on a character (or more truly, a byte) position. The easiest way to work is probably to read in every line and write it back out again to a temp file (after making any required changes), then swap the files.

    But if you don't want to do that, ie, you want to work on the file "in place", here's something to get you started, with some functioning examples of some of the commands involved:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main() {
    	int got, total=0, offset, i, dif;
    	char *ptr, find[]="change this", replace[]="to that", buffer[64];  /* presume short lines */
    	FILE *file=fopen("tmp.txt", "r+");
    	while ((got=fread(buffer,1,64,file))) {   /* 64 objects of 1 byte each */
    		if ((ptr=strstr(buffer,"change this"))) {
    			offset=ptr-buffer; /* some pointer arithmetic */
    			fseek(file,total+offset,SEEK_SET);
    			fwrite(replace,1,strlen(replace),file);
    			dif=strlen(find)-strlen(replace);
    			for (i=0;i<dif;i++) fwrite(" ",1,1,file);
    			fseek(file,total+got,SEEK_SET);  /* back to end of line */
    		}	 	
    		total+=got;
    	}
    	return 0;
    }
    This does not change the length of the file, it "blanks out" the extra characters, so if the file looked like that:
    Code:
    blah blah blah
    blah change this blah
    blah blah
    it will now look like this:
    Code:
    blah blah blah
    blah to that     blah
    blah blah
    Which is part of why it's easier to write in and out line by line, or else read the entire file into memory, work on it and write it back out (no need for a temp file).
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  4. #4
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    thanks for the replies.
    i will check up on this.

    also i was wondering if anyone knows of tutorials on how to write a small editor like nano in c.

    the only thing google return me is a how to write an editor using freepascal which i cant use that much of since i dont know pascal . i can still see alot of the problems in the creation of an small editor but it would be nice to have something wiht examples using c code.

    thanks for the time.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by cmay View Post
    thanks for the replies.
    i will check up on this.

    also i was wondering if anyone knows of tutorials on how to write a small editor like nano in c.
    nano uses the ncurses library (many or most other console based apps do). Which ncurses is indeed C.

    There is at least one tutorial on the ncurses API (Application Programming Interface), which I learned from last year when I was a stark raving newbie, and I would totally recommend it for the challenges you will encounter and the things you will learn.

    Ncurses can be used for all sorts of simple experimentation when you don't want to do a fullblown GUI but want/need more than the "scrolling lines" interface.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Sep 2008
    Posts
    53
    Quote Originally Posted by MK27 View Post
    nano uses the ncurses library (many or most other console based apps do). Which ncurses is indeed C.

    There is at least one tutorial on the ncurses API (Application Programming Interface), which I learned from last year when I was a stark raving newbie, and I would totally recommend it for the challenges you will encounter and the things you will learn.

    Ncurses can be used for all sorts of simple experimentation when you don't want to do a fullblown GUI but want/need more than the "scrolling lines" interface.
    perfect. thanks a lot.
    i have as a matter of fact a book called begging linux programming 4 editon which has a chapter on ncurses so i have it installed already.
    i am not all the way trough the elementary c so i have to study really hard to learn how to use this but i think following this book and tutorial as i learn more would be great.

    thanks again. it means a lot to me .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Packed Files (Puting multiple files in one file)
    By MrKnights in forum C++ Programming
    Replies: 17
    Last Post: 07-22-2007, 04:21 PM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. 3 Files of OO Language
    By rayrayj52 in forum C++ Programming
    Replies: 4
    Last Post: 11-03-2004, 02:08 PM
  4. How do you work with resource files???
    By Finchie_88 in forum Windows Programming
    Replies: 4
    Last Post: 10-23-2004, 02:39 PM
  5. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 05:18 PM