Get file section

I am creating a program that sends a file quickly by sending sections of it in datagrams....
everything is working fine so far, except when i use file.read(buffer,size)
size is always 40000, but the size of buffer is a seemingly random number everytime, about 100-200 in length. I have allocated enough memory for buffer, and the file it is reading from is very large (around 200mb) so i know its not at the end of the file. What could be wrong? If you need any of the code, just ask for it
Did you open the file as binary? If you didn't, it's likely that a newline conversion is happening in the back.
yes, i did open it as binary, but it still doesn't work. It does correctly get newline characters
here is the code im using to get a section of the file
1
2
3
4
5
6
7
8
9
10
11
void getSection(int section,char* data){
    inputFile.seekg(getSectionStart(section),ios::beg);
    clearChar(data);
    strncpy(data,getBlankHeader(header),header);
    inputFile.read(data+header,getSectionLength(section));
    cout<<strlen(data+header)<<endl;
    _memccpy(data+(header-getDecPlaces(section)),intToChar(section),NULL,getDecPlaces(section));
}
void clearChar(char* c){
strncpy(c,"",strlen(c));     
}


this function returns a char* with a header 8 characters long, then a portion of the file, 40000 characters long. It works on a text file, with any possible character you can type on a keyboard, but then i tried to send a .avi file, and it doens't return something 40000 long anymore, but some random length.
Last edited on
Why are you using strlen() to get the length of the read buffer if you're getting binary data, which you don't know where it could contain a '\0'?
As far as I know, the only way to know how many bytes were read is by checking the file pointer position after the call to read and subtracting with the position before the call.
Last edited on
hmm.. thats a good point, ill look into this
how could i send binary data through a datagram then? doesn't it take a null terminated string as input?
Last edited on
Base64 is a method to encode binary data as ASCII.
http://en.wikipedia.org/wiki/Base64
Last edited on
ahhhh... nevermind. I looked further into my code, and i found that for the string length parameter on the sendto function, i used strlen. I just changed this to my own function to get the length, and its fine
EDIT: nevermind, i still need a little help...
so i sent 40000 characters throught a datagram (i hope it was 40000, but there isnt anyway i can make sure, except the length was set to 40000)
then, the client recieves the packet... and saves it to a file opened as binary with... file<<buffer;
but wouldn't that only save until it reaches \0? so how could i get it to save the whole thing of length 40000?
EDIT: wow.. i just remembered after i posted this. file.write(buffer,length); anyway this works, so thanks helios for your help. :D
Last edited on
If only every poster was like you...
Topic archived. No new replies allowed.