Solved

Read from buffer not file

Posted on 2014-02-18
5
471 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
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 this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

751 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