Useful functions

This is a discussion on Useful functions within the C Programming forums, part of the General Programming Boards category; Post your own. Two file size functions and a fgetline function. Two file size checking functions, you'll need to include ...

  1. #1
    Spaced Cadet
    Join Date
    Aug 2003
    Posts
    110

    Useful functions

    Post your own.

    Two file size functions and a fgetline function.

    Two file size checking functions, you'll need to include stdio.h if you want to use either one. The first takes a pointer to a file, it presumes that the pointer isn't at the beggining and you'll want it reset to what it was before you called fsize(). The second takes a string as an argument, it will create the file pointer for you and close it. Both will return -1 on an error. Please note that the first one will not give an error if it failled to reset the file pointer.

    Code:
    long fsize(FILE *fp) {
    	long origin, ret;
    	
    	if (fp == NULL) {
    		return -1;
    	}
    	
    	origin = ftell(fp);
    	
    	if (origin == -1) {
    		return -1;
    	}
    	
    	if (fseek(fp, 0, SEEK_END) == 0) {
    		ret = ftell(fp);
    		if (ret == -1) {
    			return -1;
    		}
    		fseek(fp, origin, SEEK_SET);
    	} else {
    		return -1;
    	}
    	
    	return ret;
    }
    
    long fsize(const char * name) {
    	FILE *fp = fopen(name, "r");
    	long ret;
    	
    	if (fp == NULL) {
    		return -1;
    	}
    	
    	if (fseek(fp, 0, SEEK_END) == 0) {
    		ret = ftell(fp);
    		if (ret == -1) {
    			fclose(fp);
    			return -1;
    		}
    	} else {
    		fclose(fp);
    		return -1;
    	}
    
    	fclose(fp);
    	
    	return ret;
    }
    Code:
    //This one was made for a C++ program, so just define bool
    bool fgetline(char * str, FILE *fp, bool keep_new_line = 1) {
       short c;
       short i = 0;
       
       do {
          c = getc(fp);
          str[i] = c;
          i++;
       } while (c != EOF && c != '\n');
       if (keep_new_line) {
          str[i] = '\0';
       } else {
          str[i-1] = '\0';
       }
       
       if (c == EOF) {
          return false;
       } else {
          return true;
       }
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    long int fsize( const char *fn )
    {
        long int size = -1;
    
        if ( fn )
        {
            FILE *fp;
    
            if( (fp = fopen( fn, "rb" )) )
            {
                int c;
    
                size = 0;
                while( (c = fgetc( fp )) != EOF )
                    size++;
                fclose( fp );
            }
        }
    
        return size;
    }
    Everyone post a new variation of the theme.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Code:
    bool fgetline(char * str, FILE *fp, bool keep_new_line = 1) {
    Yay! All the safety of gets!
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 02:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM

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