Thread: reading files

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    98

    reading files

    i have this code for reading files:
    Code:
    HANDLE h;
    h = CreateFile("D:\\Dev-Cpp\\Works\\Updater\\Client\\abc\\Client.exe", GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    if(h == INVALID_HANDLE_VALUE) Error("invalid handle");
    char buf[1024];
    DWORD w;
    ReadFile(h, buf, 1023, &w, NULL);
    Msg(buf);
    CloseHandle(h);
    and it works just fine with .txt files but not with .exe
    it prints me just 2 characters instead of 1023.
    is there another way to read it?

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    exe files don't contain text, they contain binary. The first 2 charactors of an exe file are zm (or mz I can't remember which order they are in) followed by zeros which means the zeros NULL terminate the string and so only the first 2 charactors are displayed.
    What are you trying to do with the exe file?

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    transfer it with winsock

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    TransmitFile()
    or look at the lpNumberOfBytesRead value (w) to see how much of the buffer was filled and send that much with send() (You would need to loop since the file is likely larger then 1023 bytes, also since this isn't text and you don't need to add a terminating zero you can use the full 1024 bytes for this.
    Last edited by Quantum1024; 05-21-2005 at 06:54 AM.

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    that's exactly what i do, but the new file only contains MZ
    maybe it's because i add the data to another string using sprintf and it adds only untill a null character?
    Last edited by hiya; 05-21-2005 at 07:08 AM.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Quote Originally Posted by hiya
    maybe it's because i add the data to another string using sprintf and it adds only untill a null character?
    That's what is happening. sprintf is a string function.

  7. #7
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    this should be in the windows programming board

  8. #8
    Registered User
    Join Date
    Apr 2005
    Posts
    98
    oh sorry...
    when i send the data without sprintf, it sends all the data, but the client receiving only the first 3 characters and then nothing.
    how else can i send\receive the file except using transmitfile?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with reading files
    By glopv2 in forum C Programming
    Replies: 3
    Last Post: 07-31-2007, 11:05 PM
  2. Reading User Defined Files
    By Necrofear in forum C++ Programming
    Replies: 17
    Last Post: 06-30-2006, 12:55 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. A little help reading from files...
    By Invincible in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2002, 10:43 AM
  5. Need Advice in reading files
    By jon in forum C Programming
    Replies: 4
    Last Post: 10-07-2001, 07:27 AM