Thread: C reading/writing files

  1. #16
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Code:
      for(i = 0;i < numfiles;++i)
        extract_file(fdin, files[i]);
    
      close(fdin);
    The structure is filled including name, file size, and offset into archive before that block of code is reached. That tiny for() loop simply extracts each file in turn. If you want to rewrite the extract_file() function to just do a simple lseek() and then read(), that's totally fine. Just make sure your buffer is big enough. Maybe something like:
    Code:
    char *buffer;
    for(i = 0;i < numfiles;++i)
    {
      buffer = malloc(files[i].size);
      read(fdin, buffer, files[i].size);
      fdout = open(...);
      write(fdout, buffer, files[i].size);
      close(fdout);
      free(buffer);
    }
    Last edited by itsme86; 12-01-2010 at 03:14 PM.
    If you understand what you're doing, you're not learning anything.

  2. #17
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    itme86,

    hello again, just encountered a error I am stuck on: -

    --) When adding files to archive ( the first program), when we go back to write() the header data, we reserved 2 bytes(short) at beginning of file, two store value of how many files in archive.

    like: write(fdout,(short*)&numfiles, sizeof(short)); - - at the time obviously variable 'numfiles' would have value ie say 4, representing no of files user added and thus that line above should have written say 4 into the first 2 bytes(short) at start of archive file, mistake me if I misunderstand?

    --) So, in the second program (extracting) we have the line: -

    Code:
    int numfiles;
    read(fdin, (short*)&numfiles, sizeof(short));
    Obviously, the var 'numfiles' in this program would be zero intially and using the read() we want to read the value we stored in first 2 bytes of archive file, to get this value back out and put it back into var 'numfiles' correct me if I misunderstood?

    -- ) The problem I have, I believe is either the numfiles value was not written into file in first place, or there is error with read() statement I using below, in my extract program : -

    Code:
    int numfiles; //would be empty obviously in this program until read value from file in
    lseek(fdin, 0, SEEK_SET); 
    read(fdin, (short*)&numfiles, sizeof(short)); 
    //want to check numfiles has number of files stored inside it
    printf("%d is the number of files inside archive\n", numfiles);
    printf("%x is the number of files inside archive- memory address\n", numfiles);
    However, the memory address returns fine, but say I added one file to archive 'file1' then 'numfiles=1' should should it not, however it returns a number in its thousands like 23456 or something?

    Is there a way I an see the header data to see if its written correctly, or is it always hidden?

    What does (short*)&numfiles do, &numfiles is the memeory address of int numfiles variable, but what is (short*) about ?

    Thanks
    daza166

  3. #18
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can open the file in a hex editor to see what's going on in there. I use XVI32

    But yes, you're right. If you add one file then the first 2 bytes of the file should be 0x01 0x00, assuming you're using a little endian CPU such as x86.
    If you understand what you're doing, you're not learning anything.

  4. #19
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    itme86,

    the problems is when I say add one file
    read(fdin, (short*)&numfiles, sizeof(short));

    and then
    printf("%d\n", numfiles); -- returns 28265 , should that not be '1' as only 1 file added?

    Thus the loop below would be invalid as says numfiles is - 28265
    for(i = 0; i < numfiles;++i)

    There is an error somewhere when writting and reading out first 2 bytes(short) from archive?

  5. #20
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Yes, numfiles should be 1 if only 1 file was added.

    Did you try a hex editor on the archive file? What do the first 2 bytes look like? I tested with the code I've pasted here and the first 2 bytes always come out correct for me.

    My guess is that there's something wrong with your code that writes the header.
    Last edited by itsme86; 12-02-2010 at 12:37 PM.
    If you understand what you're doing, you're not learning anything.

  6. #21
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    looked in ghex but could not work out, what it means, here is bit of first line is like

    00000000 69 6E
    00000011 61 72
    00000022 73 74

    nothing on the clear text on side, just content can not see header ie first char is not a number
    using write() read() as shown in previous posts

    lseek(fdout, 0, SEEK_SET);
    write(fdout,(short*)&numfiles, sizeof(short));
    Last edited by daza166; 12-02-2010 at 12:50 PM.

  7. #22
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Where do you assign numfiles? Can you post the whole code?
    If you understand what you're doing, you're not learning anything.

  8. #23
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    itme86,

    ok managed to sort program myself, just stuck on little issue

    ) How do I write a variable into a file using write() function, it returns an error

    say I want to write() an integer value at start of file, so want to write 4 bytes

    Code:
    int numberfiles=5; 
    open().......
    write(file, numberfiles, sizeof(numberfiles));
    Do I have to use '&' in order to write that value from that variable using its memory address?

    Thanks,

  9. #24
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Do I have to use '&' in order to write that value from that variable using its memory address?
    Yup. Just use &numberfiles and it should work correctly.
    If you understand what you're doing, you're not learning anything.

  10. #25
    Registered User
    Join Date
    Nov 2010
    Posts
    14
    itme86,

    Say I want to write() 4 bytes(int) to a file and read() that 4 bytes back out and put the value into inside those 4 bytes into a variable

    Code:
    //open () .........opens approp files
    
    int numfile=5; 
    lseek(file, 0, SEEK_SET);
    write(file, &numfile, sizeof(numfile)); // write 4 bytes (int value) to file
    
    //read 4 bytes from file and write into variable readout, also int
    int readout; //put value read from file in this variable 
    
    lseek(file, 0, SEEK_SET);
    read(file, &readout, sizeof(readout));
    printf("number that was put in file originally was %d",readout);
    I know that you can not print or see int values read() or write()en to file, as formats change

    So how can I read() the 4 bytes I know is at start of file and put the value into that int variable (readout) which is the right type to hold that value and print it out to screen?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Linux IPC - reading/writing files, getting garbage
    By kbfirebreather in forum C Programming
    Replies: 9
    Last Post: 02-01-2009, 02:55 PM
  2. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  3. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  4. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  5. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM