Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Printf all memers of structure.

Status
Not open for further replies.

ismailov-e

Member level 1
Joined
Jan 26, 2015
Messages
34
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
295
Hi everybody!!
How can I use only one printf to print all memers of structure. For example:

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <string.h>
 
typedef struct Books
{
   char  title[50];
   char  author[50];
   char  subject[100];
   int   book_id;
} Book;
 
int main( )
{
   Book book;
 
   strcpy( book.title, "C Programming");
   strcpy( book.author, "Nuha Ali"); 
   strcpy( book.subject, "C Programming Tutorial");
   book.book_id = 6495407;
 
   printf( "Book title : %s\n", book.title);
   printf( "Book author : %s\n", book.author);
   printf( "Book subject : %s\n", book.subject);
   printf( "Book book_id : %d\n", book.book_id);
 
   return 0;
}

 

Hey. I found how to do this. Using Binary I\O Functio.
I have one question:
Can I write members in file separeted with new line?
 

is this what you require?
Code:
int main(void)
{
  Book book={"The Weald of Youth","Siegfried Sassoon","autobiography"};
  printf("%s\n%s\n%s\n", book.title, book.author, book.subject);
  }
gives
Code:
The Weald of Youth
Siegfried Sassoon
autobiography
 

is this what you require?
Code:
int main(void)
{
  Book book={"The Weald of Youth","Siegfried Sassoon","autobiography"};
  printf("%s\n%s\n%s\n", book.title, book.author, book.subject);
  }
gives
Code:
The Weald of Youth
Siegfried Sassoon
autobiography

Thank you for your reply.
But I tried avoid to write all members as you did. And after I aimed to write to file.
I succeed with "fwrite" function.

Code C - [expand]
1
fwrite(&book,sizeof(book),1,f);

.
But it is write to file the value of members in disorder.
 

fwrite() writes a block of memory to a file, i.e. in binary, to be read later by a program using fread() or similar
if you require formatted output in human readable form use printf()
binary IO is used because it is more efficent (faster and the files are shorter) than formatted IO when one does not need the output to be human readable
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top