ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have a question about how to read buffer after I read a file into buffer.
The file format is like this:
0 100.00
description of the case
comment line
1 2 3.00 "id1" 4.0
2 2 3.00 "id2" 5.0
.
.
.
30 2 4.45 "id30" 6.0
The second and the third line are some description and comments. I have a bunch of this kind of files and the description and comments are different. The format of the fourth line to the last line is fixed "%d%d%f%c%f". How to read this buffer?
The reason I do this is that I want to read file into a buffer, then MPI_Bcast the buffer to all processors, then read buffer in each processor. I am a rookie, any suggestion?
I think what you're looking for is the mmap() system call. That is, you want to take the whole file and read it into a chunk of memory? Try "man 2 mmap" to get more information.
I think what you're looking for is the mmap() system call. That is, you want to take the whole file and read it into a chunk of memory? Try "man 2 mmap" to get more information.
Thanks. but my question is how to read buffer, instead of put something into buffer.
Maybe I can read the ASCII input file and obtain the arrays I need. Then write these arrays to a buffer, broadcast the buffer and read it. Do you have any idea about how to write different arrays (like struct) into a buffer, and then read it?
// Now I want to read the buffer like read a file, how to do it?
// something like sscanf(buffer...)??
Second case, I want to read all the arrays I need from the file. And then write these arrays into a buffer, broadcast it, and read the arrays from the buffer in other processors.
By "buffer", it sounds like you mean an MPI ("Message Passing Interface") buffer. So the real questions are:
1) Can I read structured data into an MPI buffer, so that I can send it with a single MPI_Bcast command?
2) If so, how can I parse the data from my text file into this buffer?
I've frankly never used MPI, so I don't know the answer to the first question. The second question, parsing the data, should be trivial. Just pick a language you feel comfortable with (or feel comfortable learning), and go for it.
Languages that might be worthwhile to parse your data file with include:
a) Perl
b) Python
c) C/C++
d) Fortran
etc etc
'Hope that helps .. PSM
PS:
A buffer in the 'C' sense (like in your example code above) is just a "bag of bits" - it has no inherent structure. An MPI buffer, on the other hand, is typically an "array of integers" or
an "array of reals".
Perhaps the easiest solution is just to rephrase the problem like this:
a) Parse my text file into local arrays
b) Broadcast the arrays to other processors (via MPI buffers)
... and ...
c) Read broadcasts from other processors into my local arrays
d) Write the arrays to a new text file
You can read it just like you would any other string that you have. You could use sscanf or you can read each char one by one.
On a side note this gives me some new ideas on how to manage my own program that just uses text for data storage rather then a complicated linked list. Tho im not sure which program is more memory conservative.
Code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
char *buffer = NULL;
unsigned int filelen = 0;
FILE *testfile = NULL;
testfile = fopen("datab.txt","r");
fseek(testfile,0,SEEK_END);
filelen = ftell(testfile);
rewind(testfile);
printf("\nLength of file is %i charecters\n",filelen);
buffer = malloc(filelen +1);
fread(buffer,filelen,1,testfile);
fclose(testfile);
printf("\nThe first charecter in the buffer is %c\n",buffer[0]);
free(buffer);
return 0;
}
Of course this program has no error checking at all.
why do you want to do this?
there's no point as the C lib reads files into a buffer anyway.
I've experimented with this myself the only advantage is
added complexity
As I understand it, obladioblada is trying to use an MPI buffer, not a "buffer" in the ordinary, everyday sense.
He posted some sample code just about the same time as I sent him a response; I'm not sure exactly what he meant - but I'm pretty sure he wasn't looking for something like "mmap()" (which lets you treat a disk file as though it were a block of memory) or "sscanf ()" (which lets you do formatted reads from a string in memory, instead of a text string read from disk).
He definitely wants to parse the text file (we're all agreed upon that). He probably wants to read the data from disk into arrays (it looks like MPI - like FORTRAN - is oriented toward arrays of simple types). And, if he's not conversant with 'C', he might want to consider writing this tool in something simpler like Python, Perl .. or even FORTRAN.
If I understand you, (sorry if I don't or other people have answered)
Why don't you create a linked list of structures for your numbers, the list is linked to the date field Aug. 20 2006 in your example. Or you could even have all the date description part as a seperate structure or the struct first element in the list. Then read in the data values eg
and store these values in each element of the list. Then if you need to add a new set of values traverse the list and add where needed. When you have finished you can save the structures to a file. You do this sort of thing in games progreamming.
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]);
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.