Solved

Read from buffer not file

Posted on 2014-02-18
5
464 Views
Last Modified: 2014-02-18
Hello,
in my old code I worked with fread to read from an little text file some settings. This I want now replace with reading from a buffer (char).  The original ocde is:
	int val;
	fread(&val, sizeof(int), 1, f); DBFFTSize  = val;
	fread(&val, sizeof(int), 1, f); DBNumFFTs  = val;
	fread(&val, sizeof(int), 1, f); DBNumBins  = val;
	fread(&val, sizeof(int), 1, f); DBFirstBin = val;
	fread(DBOrgBuf, sizeof(float), 16, f);
	fclose(f);

Open in new window

How to replace this while read from a char buffer.  I have a buffer that was filled before and that was initialized with:
char buff[100];
memset(buff,0,sizeof(buff));

Open in new window


Thanks,

Ingo
0
Comment
Question by:Ingo Foerster
  • 3
  • 2
5 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 400 total points
Comment Utility
In this case, 'read' might not be the right term. You'd just interpret the buffer's content differently, i.e.

struct FFTVals {

  int DBFFTSize;
  int DBNumFFTs;
  int DBNumBins;
  int DBFirstBin;
  float DBOrgBuf[16];
};

// ...

char buff[100];
memset(buff,0,sizeof(buff));

// ...

FFTVals* pVals = (FFTVals*) buff; // consider 'buff' as 'read'

// copy values - or just use them directly from 'pVals'
  int DBFFTSize = pVals->DBFFTSize;
  int DBNumFFTs = pVals->DBNumFFTs;
  int DBNumBins = pVals->DBNumBins;
  int DBFirstBin = pVals->DBFirstBin;
  // etc.

Open in new window

0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
BTW, the above would as well work to read your file in one single call, e.g.

fread(buff, 4 * sizeof(int) + 16* sizeof(float), 1, f);

FFTVals* pVals = (FFTVals*) buff;

Open in new window

0
 

Author Comment

by:Ingo Foerster
Comment Utility
ok, how to get the float to a existing float value:

existing:
float cinDBOrgBuf[16];
cinDBOrgBuf = pVals->DBOrgBuf; 

Open in new window


do not work.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
That can be done like

float cinDBOrgBuf[16];
for (int i = 0; i < 16; ++i)
  cinDBOrgBuf[i] = pVals->DBOrgBuf[i]; 

Open in new window


or simply like

float cinDBOrgBuf[16];
memcpy(cinDBOrgBuf,pVals->DBOrgBuf,16 * sizeof(float)); 

Open in new window


But, as I wrote, there is no need to duplicate these values, you can use them directly from 'pVal'.
0
 

Author Closing Comment

by:Ingo Foerster
Comment Utility
Exact what I was looking for. Many thanks!
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now