Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Read from buffer not file

Posted on 2014-02-18
5
Medium Priority
?
480 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 1600 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

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

704 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