Solved

Read from buffer not file

Posted on 2014-02-18
5
470 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
ID: 39868347
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
ID: 39868426
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
ID: 39868481
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
ID: 39868500
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
ID: 39868719
Exact what I was looking for. Many thanks!
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

820 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