LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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 02-14-2004, 08:25 PM   #1
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Rep: Reputation: 30
Structures


Hey all

I've been trying to get into structures for a while but can't figure out how to use them to their full potential.

What i'm trying to do is have a file with 3 fields: name, password and comment. I wanna be able to use a structure to view/add these fields.

How easy is this to do? I understand how to get info (i.e. i've been working with the passwd structure alot) but not how to lay everything out....

Oh BTW i'm programming in C

Thanks in advance

Last edited by AMMullan; 02-14-2004 at 08:29 PM.
 
Old 02-14-2004, 09:14 PM   #2
Mohsen
Member
 
Registered: Feb 2003
Location: Iran
Distribution: Solaris 10
Posts: 201

Rep: Reputation: 30
It's quite easy!
usign fread/read or fwrite/write you can read a record or store a record in a file.
Code:
structure user{
  char[256] pass;
  char[256] name;
  char[256] comment;
};

int main () {
  // read from the file using fread (or read syscall)
  // write to the file using fwrite (or write syscall)
  FILE* fp = fopen ("myFile.dat", "w")
  struct user u = {
     "12345", "Mohsen", "Salaam!"
  }
  fwrite (u, 3, 255);

  // reading from the file is identical to some extent
  return 0;
}
 
Old 02-14-2004, 09:49 PM   #3
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
K wow I've never run into fwrite so didn't that either (guess it takes longer to learn all the functions you can you than I thought

Ummm had to change a few things (some syntax errors) but the fwrite requires 4 arguments, not 3... I had a read of the man page for it and it's meant to be something like this:

Code:
fwrite (u, 255, 3, fp);
But because u is part of a structure it's incompatible....

Also as we gave this structure an alias of u do we call the information like this:

Code:
struct user *foo;
  printf("User: %s\n", foo.u->name);
?

Thanks heaps - u've already shown me quite a bit

 
Old 02-14-2004, 11:51 PM   #4
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
Also is it possible to dynamically make structures from variables - i.e.

Code:
char name[256];

printf("Enter your name: ");
fgets(name, sizeof(name), stdin);

...

struct user %s {"%s", "%s", "%s"}, name, name, pass, comment;
I know that doesn't work but I wanna see if I can do something like that

 
Old 02-18-2004, 07:24 AM   #5
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
Nopes you can not dynamicly create structures.

But you could create "name-value" pairs.

Code:
struct pair {
  char name[16];
  char value[16];
}

struct myData {
   struct pair field1;
   struct pair field2;
}
or why not:

Code:
  struct pair files[2];
 
Old 02-18-2004, 07:25 AM   #6
worldmagic
Member
 
Registered: Oct 2003
Location: Europe/Sweden
Distribution: RedHat
Posts: 78

Rep: Reputation: 15
erh.. files[2] should be fields[2] =)
 
Old 02-18-2004, 11:39 AM   #7
AMMullan
Member
 
Registered: Sep 2003
Location: United Kingdom
Distribution: Ubuntu, Arch
Posts: 438

Original Poster
Rep: Reputation: 30
Kewl i'll read up a bit more on the pair type and see how I go... Thanks

Also just bought "C by Example" and looks like a really good book, might learn a few things from it

Thanks all
 
  


Reply



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
regarding structures eshwar_ind Programming 2 04-25-2005 09:18 AM
passing structures to function b123coder Programming 3 04-20-2005 10:15 AM
initializing arrays of structures in c dreamgoat Programming 5 09-27-2004 02:02 AM
Nested structures :S? alitrix Programming 11 11-15-2003 07:13 PM
C and arrayed structures.. miguetoo Programming 9 05-22-2003 06:30 PM

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

All times are GMT -5. The time now is 11:08 AM.

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