Results 1 to 10 of 10

Thread: file operations

  1. #1

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919

    file operations

    hello

    is there a way i can find out a files location in memory?

    if i use
    Code:
    FILE *fp; 	
    
    	if((fp=fopen(fname,"w")) == NULL){
    		return 0; //return failure
    	}
    if i did "&fp", will that give me the location of the first byte of the file in memory?

    and suppose the file is 16 bytes long..can i cycle through each byte one-by-one and read/modify it?


    -thanks
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    no and no
    You can get the file content byte by byte by using fgetc().
    Last edited by CornedBee; Dec 11th, 2001 at 10:31 AM.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3
    jim mcnamara
    Guest
    FILE is a struct (user-defined type) FILE *fp is a pointer to that struct.

    What is in the FILE struct is somewhat different for each compiler, but generally has some basic stuff - here is one:

    Code:
    /* Definition of the control structure for streams
    */
    typedef struct  {
            short           level;          /* fill/empty level of buffer */
            unsigned        flags;      /* File status flags    */
            char            fd;              /* File descriptor      */
            unsigned char   hold;     /* Ungetc char if no buffer */
            short           bsize;          /* Buffer size          */
            unsigned char   *buffer;  /* Data transfer buffer */
            unsigned char   *curp;    /* Current active pointer */
            unsigned        istemp;     /* Temporary file indicator */
            short           token;          /* Used for validity checking */
    }       FILE;                            /* This is the FILE object */
    So, you can access the buffer like this:

    Code:
    char *tmp;
    tmp = calloc(1,BUFSIZ+1);
    strncpy(tmp,r->buffer,fp->bsize);
    printf("my file buffer has this is in it:%s\n", tmp);
    free(tmp);
    The file is NOT in memory somewhere, unles you have read it into a string array or whatever.

  4. #4

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    does "buffer size" give me the size of the file?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  5. #5
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    No it gives you the size of the current read buffer.

    You should NOT use the internal structures, always use the accessor/mutator functions. If you want the size of a file, use stat (#include <sys/stat.h>).
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  6. #6

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    err...what are those?

    i did:

    Code:
    	while(!feof(fp)){
    		tmp = getc(fp);
    		::fsize+=1;	
    	}

    to get the file size, it came out accurate...i and i timed it with GetTickCount()...0.0000ms...i guess thats good enough.
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  7. #7

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    another question too..
    i also did:

    Code:
    	char tmp = (char) malloc(1 * sizeof(char));
                    ...
    	free((void *)tmp);
    is that good/clean?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  8. #8

    Thread Starter
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    VC++ doesn't like it.

    how do de-allocate memory?
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  9. #9
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    Code:
    while(!feof(fp))
    {
    	tmp = getc(fp);
    	::fsize+=1;
    }
    HAHA try that with a 1 MB file

    Better way:

    Code:
    fseek(fp, 0, SEEK_SET);
    size = ftell(fp);
    Or use parksies way...
    Code:
    char tmp = (char) malloc(1 * sizeof(char));
    ...
    free((void *)tmp);
    A) The main problem is that you need to cast the return of malloc to a pointer:
    Code:
    char* tmp = (char*) malloc(1 * sizeof(char));
    ...
    free((void *)tmp);
    B) This is no good use for dynamic memory. You use dynamic memory when a) you don't know the amount you'll need or b) when you want a really big block of memory - at least several hundred bytes. Everything else can be allocated on the stack.

    C) You seem to use C++ (not pure C), so you should use new and delete instead:
    Code:
    char* tmp = new char;
    ...
    delete tmp;
    Isn't that cleaner?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  10. #10
    jim mcnamara
    Guest
    File size -

    FWIW - ftell does not "ftell" the truth all the time.
    For: FILE fp=fopen("myfile","r") ftell() doesn't count EVERY character.

    Why?

    It converts CrLf (two characters) into \n a single character.
    Depending on the OS there may other similar problems with file length.

    In ANSI C the standard ways to get file length are:

    with stat()

    Code:
    #include <sys/stat.h>
    long FileLen(char *filename){
    stat *buf;
    
     if(!stat(filename,buf){
         return (long) buf->st_size;
     else
         return 0L;
    }

    or with:

    [code]
    long FileLen(char* filespec){
    FILE *fp;
    long f;

    fp=fopen(filespec,"rb");
    fseek(fp,0,SEEK_END);
    f=ftell(fp);
    fclose(fp);
    return f;
    }

    stat() is the preferred way because it doesn't involve file i/o.
    Both work reliably. MS doesn't document either one very well - ie., you can't put this all together without some legwork. IMO.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width