473,388 Members | 868 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

How to parse data from a buffer into an array of structs?

42
Hello
I'm still kinda new at C and I have this buffer filled with data that I want to to 'parse' into a an array of structs. How do I do this?
The only thing I can think of is for-loops, kinda 'bytewise', but there must be a better way?

It's something like this:

Expand|Select|Wrap|Line Numbers
  1. char buf[BUFSIZE];
  2.  
  3. struct table_entry{
  4. int length;
  5. int offset;
  6. char *name;
  7. }
  8.  
  9. struct table_entry *tablearray = NULL;
  10.  
I allocate memory for the tablearray, data is read into the buf and then, how do I 'fill' the structs? The data in the buffer is of course 'structdata', I mean of the kind 'length,offset,name,length,offset,name...."

Hope someone knows what I mean and can help me, that would be so very kind of you!
Cheers!
Mar 31 '08 #1
9 10321
You can use memcpy function in your code.

Expand|Select|Wrap|Line Numbers
  1. Code: ( text )
  2.       char buf[BUFSIZE];
  3.       struct table_entry{
  4.        int length;
  5.        int offset;
  6.        char *name;
  7.        }
  8.     struct table_entry *tablearray = NULL;
  9.  
  10.  
I am assuming that the buffer has valid data that is aligned properly with the struct table_entry. As you mentioned you will need a for loop to copy the number the entries which you should maintain a count of. I am supposing the number of entries to be variable n.

Expand|Select|Wrap|Line Numbers
  1. int i = 0;
  2. /* Allocate the memory for tablearray */
  3. tablearray = malloc(...);
  4. for(i=0;i < n;i++) {
  5. memcpy(tablearray, buf, sizeof(table_entry));
  6. }
  7.  
I hope this helps.
Ambrish Kinariwala
Mar 31 '08 #2
mac11
256 100+
Hello
I'm still kinda new at C and I have this buffer filled with data that I want to to 'parse' into a an array of structs. How do I do this?
The only thing I can think of is for-loops, kinda 'bytewise', but there must be a better way?

It's something like this:

Expand|Select|Wrap|Line Numbers
  1. char buf[BUFSIZE];
  2.  
  3. struct table_entry{
  4. int length;
  5. int offset;
  6. char *name;
  7. }
  8.  
  9. struct table_entry *tablearray = NULL;
  10.  
I allocate memory for the tablearray, data is read into the buf and then, how do I 'fill' the structs? The data in the buffer is of course 'structdata', I mean of the kind 'length,offset,name,length,offset,name...."

Hope someone knows what I mean and can help me, that would be so very kind of you!
Cheers!
I want to help but I don't want to write a bunch of stuff that you might already know. Help me understand how far along in the process you are.

Do you have code to open the input file and read it into your parse buffer?
Have you figured out the high level algorithm for parsing a struct worth of data?
Do you have any parse code?

--
Ambrish's code is likely not to work because sizeof( struct table_entry) only gives you enough memory to hold 'name' address, not space to actually hold the name (which may be 1024 bytes for all anybody knows - you have to read 'length' to know that).
Mar 31 '08 #3
MimiMi
42
Do you have code to open the input file and read it into your parse buffer?
Have you figured out the high level algorithm for parsing a struct worth of data?
Do you have any parse code?
Thank you both for taking the time trying to help me!

I have the code to open the input file and read it into my parse buffer yes, so I guess my problem is figuring out the high level parsing a struct worth of data algorithm!

No, I don't have any parse code yet (if I understand that last question correctly), that's kinda the problem..

Thanks again!
Mar 31 '08 #4
weaknessforcats
9,208 Expert Mod 8TB
Forget the array if structs. You have not mentioned your file format. It's not so much that you parse into an array of structs but more imprtant that you know the data you are reading.

If you can read the data an populate one struct variable, then an array is simple.

So, what does your data look like?
Mar 31 '08 #5
MimiMi
42
Forget the array if structs. You have not mentioned your file format. It's not so much that you parse into an array of structs but more imprtant that you know the data you are reading.

If you can read the data an populate one struct variable, then an array is simple.

So, what does your data look like?
That's the thing, that's my problem: populating that one struct variable!

Maybe it's just that my problem is too simple, that's why you all have a hard time understanding what I mean? (And of course, I'm not too great at describing my problem clearly either I guess..)

Let's put it this way: If I have a buffer filled with bytes, and I want to populate a struct out of it, how do I do that?

It's something like this
Expand|Select|Wrap|Line Numbers
  1. struct table_entry{
  2. int length;
  3. int offset;
  4. char *name;
  5. }
  6. char buf[BUFSIZE]; 
  7.  
  8. struct table_entry entry;
  9.  
  10. entry.length = //The first 4 bytes of buf
  11. entry.offset = //The next 4 bytes of buf
  12. entry.name = //The next 16 bytes of buf
  13.  
My problem is the comments, what should the code look like there?
Hope I'm being a little bit clearer, and sorry for being such a newbie..
Apr 1 '08 #6
MimiMi
42
Expand|Select|Wrap|Line Numbers
  1. struct table_entry{
  2. int length;
  3. int offset;
  4. char *name;
  5. }
  6. char buf[BUFSIZE]; 
  7.  
  8. struct table_entry entry;
  9.  
  10. entry.length = //The first 4 bytes of buf
  11. entry.offset = //The next 4 bytes of buf
  12. entry.name = //The next 16 bytes of buf
  13.  
My problem is the comments, what should the code look like there?
Maybe you can do it like this?
Expand|Select|Wrap|Line Numbers
  1. memcpy(entry.length,buf,4);
  2. memcpy(entry.offset,buf+4,4);
  3. memcpy(entry.name,buf+8,16);
Cheers!
Apr 1 '08 #7
mmk
19
Maybe you can do it like this?
Expand|Select|Wrap|Line Numbers
  1. memcpy(entry.length,buf,4);
  2. memcpy(entry.offset,buf+4,4);
  3. memcpy(entry.name,buf+8,16);
Cheers!
Hi Mimimi while trying out your query, i coded in this way but its not working out. Can you please share your code snippet. Anybody can help out me regarding this.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2.  
  3. #define BUFSIZE 100
  4.  
  5. char buf[BUFSIZE][BUFSIZE]={ 1,3,"Trying",2,4,"out",4,5,"something"};
  6.  
  7. struct table_entry{
  8. int length;
  9. int offset;
  10. char *name;
  11. }
  12.  
  13. struct table_entry *tablearray = NULL;
  14. memset(tablearray,0,BUFSIZE);
  15. tablearray = (table_entry*)malloc(sizeof(BUFSIZE));
  16.  
  17. int main()
  18. {
  19.     int i;
  20.     for(i=0;i<BUFSIZE;i++)
  21.     {
  22.         memcpy(tablearray.length,buf,4);
  23.         memcpy(tablearray.offset,buf+4,4);
  24.         memcpy(tablearray.offset,buf+16,16);
  25.     }
  26.     return 0;
  27. }
  28.  
Thanks in advance,
mmk
Apr 1 '08 #8
MimiMi
42
Thank you all for taking the time to try and help me with this!
The problem is now solved!

memcpy was no good idea!
And also, the struct member 'name' worked much better looking like this
Expand|Select|Wrap|Line Numbers
  1. struct table_entry {
  2.   int length;
  3.   int offset;
  4.   char name[NAMESIZE];
  5. };
instead of 'char *name'. And I didn't need an 'intermediate' buffer but could get the struct-data 'straight away'. Kinda like this (pseudocode):
Expand|Select|Wrap|Line Numbers
  1. struct table_entry *tablearray;
  2. int TABLESIZE = 64;
  3. int i,offset = 0;
  4. struct table_entry entry;
  5. tablearray = mem_alloc(sizeof (struct table_entry) * TABLESIZE);
  6. read_from_file(file,(unsigned char*)tablearray,offset,TABLESIZE);
Then I could access the data like this
Expand|Select|Wrap|Line Numbers
  1. for(i=0; i<TABLESIZE;i++)
  2. {
  3. entry.len = tablearray[i].len;
  4. entry.offset = tablearray[i].offset;
  5. strncpy(entry.name,tablearray[i].name,NAMESIZE);
  6. }
Again, thank you all, and take care!
Cheers!
Apr 1 '08 #9
weaknessforcats
9,208 Expert Mod 8TB
And also, the struct member 'name' worked much better looking like this

Code: ( text )
struct table_entry {
int length;
int offset;
char name[NAMESIZE];
};

instead of 'char *name'. And I didn't need an 'intermediate' buffer but could get the struct-data 'straight away'.
Yes, but you also built a limit into your code: NAMESIZE. That means if NAMESIZE is 80, a name like MimiMi will take 80 bytes instead of 6. Now your program is larger and slower.

Stick with the pointer.

If you have a buffer with a C-string in it, all you need to do si allocate memory and then copy the string to your struct variable:
Expand|Select|Wrap|Line Numbers
  1. struct data
  2. {
  3.    char* name;
  4. };
  5.  
  6. char buffer[SOMESIZE];
  7. struct data var;
  8. var.data = (char*)malloc((strlen(buffer) + 1) * sizeof(char));
  9. strcpy(var.data, buffer);
  10.  
var now has a copy of the data.

I aksed about your file format since some of your struct members were ints and you cannot have ints in a text file. Text file have only chars. However, I never saw yout file layout or your read code so I really can't see what you are doing.

char buf[BUFSIZE][BUFSIZE]={ 1,3,"Trying",2,4,"out",4,5,"something"};
An array has to have elements that are the same type. This one has int and char*. I expect the compiler will gag on it.

Again, this comes down to data format. You cannot read data unles you know the format.
Apr 1 '08 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

19
by: Johnny Google | last post by:
Here is an example of the type of data from a file I will have: Apple,4322,3435,4653,6543,4652 Banana,6934,5423,6753,6531 Carrot,3454,4534,3434,1111,9120,5453 Cheese,4411,5522,6622,6641 The...
14
by: SD | last post by:
I am thinking about writing a text editor in C for unix sometime soon. I am just doing this to learn more about C. I want to write something like ed.c, a simple line editor. What types of data...
2
by: Eric | last post by:
Hi, I need to send data from a file into a structure. In Vb6 this was done like so. public Type Struct1 v1 as string v2 as string end type
3
by: Zeke Zinzul | last post by:
Hi Guys & Geeks, What's the most elegant way of dealing with binary data and structures? Say I have this (which I actually do, a woo-hoo): struct Struct_IconHeader { byte width; byte...
31
by: aarklon | last post by:
Hi all, this is a question which i saw in a book typedef struct mall_li_header_ { int refcnt; uchar pool; uchar flag; ushort magic_no; char data;
5
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called...
1
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this...
11
by: JRough | last post by:
I'm trying to use output buffering to cheat so i can print to excel which is called later than this header(). header("Content-type: application/xmsdownload"); header("Content-Disposition:...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.