Click here to Skip to main content
11,920,156 members (58,025 online)
Rate this:
Please Sign up or sign in to vote.
See more: C Windows Win32 Text
I have some data in a text file. Now I want to copy this data to a character buffer so that I can send it iver a udp socket. How can I copy the data in a text file to a buffer? I have tried fread for this purpose but it also copies some of the redundant data, though I have specified only the number of bytes equal to the file size to be read, still it is reading some redundant data. Below is code snippet that I am trying:

char file_buffer[1000];
fpSend = fopen("sendclInformation.txt", "w+");
WriteFile(sendFile,"Data in File",strlen("Data in File"),&dwWritten,0);
fseek(fpSend, 0, SEEK_END);
size_t file_size = ftell(fpSend); // The size calculated here is 12 so fread must display only 12 bytes but it is displaying large redundant data appended to actual data.
fseek(fpSend, 0, SEEK_SET);
if(file_size>0)   //if file size>0
 int bytes_read=0;
 if((bytes_read=fread(file_buffer, file_size, 1, fpSend))<=0)
  //      "Unable to copy file into buffer",
    MessageBox( NULL,file_buffer,"File copied in Buffer",MB_ICONEXCLAMATION|MB_OK);
Posted 10-Apr-13 2:55am
Edited 10-Apr-13 3:03am
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

still it is reading some redundant data
It is not reading 'redundant data'. Simply your buffer contains uninitialised data (or previously initialised data) after the bytes_read limit: after each read operation only data from buffer[0] to buffer[bytes_read-1] is valid.
ayesha hassan 10-Apr-13 8:29am
so I should copy only buffer[0] to buffer[byres_read-1] to some other new buffer and then send that new buffer over the socket?
CPallini 10-Apr-13 13:47pm
No need to copy: send just the first bytes_read bytes of buffer over the socket.
Rate this: bad
Please Sign up or sign in to vote.

Solution 3

first of all initialize your buffer to 0 i.e char file_buffer[1000] = {0}
next, dont use static buffer size as above to store the file data, instead you can calculate the size of the file allocate that much memory to buffer and then copy the contents.
H.Brydon 13-Apr-13 13:16pm
These are good points of advice for a n00b (+5) but initializing the entire array is overkill that is not generally done in well formed programs (except for instances perhaps related to debugging, high security, ...).

Finding file size with fseek()/ftell() calls is not typically the best way to do it.

Also I would add that an if() statement that contains an assignment is a style that seems to be somewhat popular in C, but I flag as questionable for maintenance purposes. Your if() statement contains an assignment, API call and a comparison. That syntax/style is not required here and is packing way too much into that one line.

[edit]hopefully clarified that I meant that Solution#3 author gave good advice to OP, who is n00b in question[/edit]
shailesh91082 14-Apr-13 5:03am
Then could you please tell me what is the best way to initialize the array ??
and what would be the best way to get filesize ??
Rate this: bad
Please Sign up or sign in to vote.

Solution 4

Depending on the size of your buffer you may want to look into CreateFileMapping and MapViewOfFile, which will give you a nice array you can play around with directly and then flush.

I find your mix of stdio and Winapi a little confusing and ugly, I'd recommend sticking to one or using #defines depending on your compile environment.

However, on Windows ReadFile and WriteFile will both end up operating on a mapped view of the file due to how they work, the real question is convenience vs performance.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 455
1 phil.o 170
2 CPallini 150
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,153
3 George Jonsson 2,810
4 Suvendu Shekhar Giri 2,181

Advertise | Privacy | Mobile
Web03 | 2.8.151120.1 | Last Updated 15 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100