0

I have this file called input.txt opened using file pointer. Now I want to read this file and write this data into device node file using write() system call(Any better way). Right now I am scanning integer values one by one and writing to device node. Any way so that I can do this in block say some fixed number of bytes. input.txt contain 32 bit integers seperated by newline. Below is the code I have right now.

FILE *fp = fopen("input.txt", "r");
int fd = open("/dev/devnode", O_WRONLY);
int current = 0; 
while(!feof(fp))
   {
       fscanf(fp,"%x\n", &current)
       write(fd,&current,sizeof(int));
       trigger_dma_to_device();
   }

Is there any better way to do this? Please help.

EDIT: To put my requirement clearly I write to device node and then have to trigger DMA but DMA capacity is 1024 bytes so rather than writing one integer (32 bytes) I want to read 32 integers (1024 bytes) from input.txt and write it to device node before triggering dma. Any way this can be done without allocating extra memory. I'm OK with loop to start with.

bdubey
  • 307
  • 4
  • 13
  • http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – Mat Nov 02 '14 at 12:34
  • @Mat My doubt is not about usage of feof. I want to know how I can copy data from one file to another say a chunk of 128 bytes or some x number of integers. – bdubey Nov 02 '14 at 12:57
  • What you have there does not copy a file. It reads a text files, and outputs binary data. If you want to copy, just use read/write, not fscanf. And you should really have doubts about feof, you're not using it properly. – Mat Nov 02 '14 at 12:58
  • @Mat Thanks for the reply. I have not run into any issues with my usage of feof but I will check as you pointed. I have edited the question to give more info, let me know if you can help. Please ask more question if you need to. TIA – bdubey Nov 02 '14 at 13:05
  • Basically I have to read the input file and not just copy, because the data needs to be parsed. Can I do something where I can read and parse my input while appending to device node till U reach dma limit before triggering DMA. Not sure if I clear so please ask. – bdubey Nov 02 '14 at 13:08

0 Answers0