LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-03-2007, 03:45 AM   #16
mat2ag
LQ Newbie
 
Registered: Aug 2002
Distribution: Slackware,Redhat
Posts: 17

Rep: Reputation: 0

Quote:
Originally Posted by obladioblada View Post
paulsm4 is right.

I want to read structured data into an MPI buffer, so that I can send it with a single MPI_Bcast command? And then, at the receiver processor, I want to read the buffer.

I was trying to use following codes to write buffer:
int j;
j = 0;
for (i=0;i<n;i++)
{
j+= sprintf(fbuffer+j, "%d %f ",int_a[i], float_b[i]);
}

but I can NOT read it by using following code:

j = 0;
for (i=0;i<n;i++)
{
j+=sscanf(fbuffer+j, "%d %f ",&a,, &f1);
printf("%d %f ",a,f1);
}

And it will worse if there is char.
scanf family does not accept format as like printf, so you should read your int and float separately by tow following scanf.
 
Old 02-05-2009, 04:54 AM   #17
raffica
LQ Newbie
 
Registered: Feb 2009
Posts: 1

Rep: Reputation: 0
Read a binary file and store double data into a buffer

Hi all,

I need to read a binary file where I previously stored double data.

The step are:
1. Open a file
2. Read binary data and convert them into double
3. Store them into a buffer

I need also know how many double data are.

Can someone help me please?
 
Old 02-05-2009, 10:45 AM   #18
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,895

Rep: Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015
Quote:
Originally Posted by obladioblada View Post

I was trying to use following codes to write buffer:
int j;
j = 0;
for (i=0;i<n;i++)
{
j+= sprintf(fbuffer+j, "%d %f ",int_a[i], float_b[i]);
}



printf("%d %f ",a,f1);
}
I'm still learning C, so forgive me if I've got this completely wrong, but as sprintf returns the length excluding the terminating /0, wouldn't that code result in the string terminator being overwritten in the next iteration of the loop resulting in one big string?


edit: Oh FFS! Sorry guys, didn't notice the thread necromancy.

Last edited by GazL; 02-05-2009 at 10:48 AM.
 
Old 02-05-2009, 12:15 PM   #19
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Well, like the tag line to that Stephen King movie goes, "Sometimes dead is better" ;-)

Anyway, hopefully this snippet answers your question:
Code:
/*
 * Sample output:
 *
 * j: 0, len: 3, fbuffer: ABC...
 *   0x41:A 0x42:B 0x43:C 0x0:
 * j: 3, len: 3, fbuffer: ABCDEF...
 *   0x41:A 0x42:B 0x43:C 0x44:D 0x45:E 0x46:F 0x0:
 */
#include <stdio.h>
#include <string.h>

#define MAX_STRING 80

char *s[] = {
  "ABC",
  "DEF"
};

void
dumpstr (const char *s)
{
  int i;
  printf ("  ");
  for (i=0; i <= strlen(s); i++)
    printf ("0x%x:%c ", s[i], s[i]);
  printf ("\n");
}

int
main (int argc, char *argv[])
{
  char buff[80];
  char *fbuffer = buff;
  int i, j = 0;
  for (i=0; i < 2; i++)
  {
    int len = sprintf(fbuffer+j, "%s", s[i]);
    printf ("j: %d, len: %d, fbuffer: %s...\n", j, len, fbuffer);
    dumpstr (buff);
    j += len;
  }
  return 0;
}
 
Old 02-05-2009, 05:24 PM   #20
GazL
LQ Veteran
 
Registered: May 2008
Posts: 6,895

Rep: Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015Reputation: 5015
Thanks Paul. I can see that dumpstr function coming in very handy for debugging stuff.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C, howto read binary file into buffer Scrag Programming 24 11-27-2014 05:32 PM
buffer i/o read error from hdd fakie_flip Linux - Software 4 08-20-2006 11:26 PM
What is the difference between the free buffer and buffer in the buffer hash queue? Swagata Linux - Enterprise 0 05-25-2006 11:57 PM
read from tty buffer vineeth789 Programming 1 01-12-2006 10:40 AM
tcp/ip read and write buffer da_kidd_er Linux - Software 0 11-21-2004 04:13 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:17 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration