473,320 Members | 1,707 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,320 software developers and data experts.

passing a non string type to "fputs"

Hi
I have a struct
Expand|Select|Wrap|Line Numbers
  1. struct b_entry_t {
  2.    uint32_t source;  
  3.    uint32_t target;
  4.    int counter;  
  5. };
  6.  
and I have created an instance of that
Expand|Select|Wrap|Line Numbers
  1. struct b_entry_t *entry;
Somewhere in my program I want to write it to file with fputs. The problem is how can I do that?
Expand|Select|Wrap|Line Numbers
  1. fputs (entry, pFile);
I get this warning
Expand|Select|Wrap|Line Numbers
  1. warning: passing argument 1 of ‘fputs’ from incompatible pointer type
  2. /usr/include/stdio.h:682: note: expected ‘const char * __restrict__’ but argument is of type ‘struct b_entry_t *’
  3.  
and after executing the program the content of the file doesn't contain uint32_t numbers. Seems that it is a binary file!!

How can I fix that? thanks
May 25 '11 #1
5 3622
donbock
2,426 Expert 2GB
Your program data (in this case, a struct b_entry_t) can't be directly represented in a file. The structure has ... structure. There are a certain number of fields. Each field as a particular size and a particular type.

You need to pick some encoding scheme for how your program data will be represented in the file. There are any number of possible encoding schemes, but they generally fall into two categories: a binary representation or a textual representation. Your choice of fputs suggests that you want a textual representation.

Consider the following snippet:
Expand|Select|Wrap|Line Numbers
  1. struct b_entry_t { 
  2.    uint32_t source;   
  3.    uint32_t target; 
  4.    int counter;   
  5. }; 
  6. struct b_entry_t *entry;
  7. ...
  8. entry->source = 1066;
  9. entry->target = 1492;
  10. entry->counter = 1941;
  11. put_b_entry_t(entry);
What would you like the output to look like?
May 25 '11 #2
I want to write those fields in a plain text with fputs (is there another way? I don't know...)

As in your example, the output.txt should looks like
Expand|Select|Wrap|Line Numbers
  1. 1066     1492     1941
May 26 '11 #3
Banfa
9,065 Expert Mod 8TB
If you want to write binary data to a file as binary use fwrite. If you want to write binary data to a file as text use fprintf.
May 26 '11 #4
No as I said, the data are some uint_32t numbers and I want to write to a text file.
May 26 '11 #5
donbock
2,426 Expert 2GB
Read Banfa's message again. .
May 27 '11 #6

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

Similar topics

4
by: Suddn | last post by:
Help me get my mind around passing string types to a function. I need to have the function modify the string types and get them back. Normaly I would just return the modified string but I need to...
6
by: Lenn | last post by:
Hi, Could someone clarify my confusion regarding passing reference types to a method with ref keyword and explain when it's practical to use it. It's my understanding that in .NET reference...
9
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! Is it possible to alter the string type? Basicly what i want to do is to create my own string type. lets call it sqlString, when i use this it will always use replace on the string...
1
by: muntyanu | last post by:
Hi all, I am passing string from C++ to C# but not sure which way is more correct. Here it is: First approach: CDAnetCSharpHooks::PassString( ) { char str = "String to pass"; DotNetObject...
3
by: Alex K. | last post by:
Hi all I need a string type with only three possible values: let's say "A", "B", "C". In other words, I need something like enum but of string type: enum MyStrings { sA = "A", sB = "B", sC...
1
by: Néstor Sánchez A. | last post by:
Hi, is there a way, maybe using reflection, to use a generic class passing the type parameter dynamicly (not kwnowing the exact type at compile time)? I tried the next example, but doesn't work: ...
1
by: Mike | last post by:
Hi, I'm passing a JSON-encoded string to json_decode() and am expecting its output to be an object type, but am getting a string type instead. How can I return an object? In the docs, the...
1
by: kenneth6 | last post by:
ctype.h: toupper(c), tolower(c): change case of char isalphanum(c), isalpha(c), isdigit(c), ispunct(c), isspace(c), iscntrl(c), islower(c), isupper(c): boolean checks on type of char i know...
1
by: kenneth6 | last post by:
I am writing sth about data in string type in console mode. now, I transform it to Windows Form Application, i know managed type should be used here instead of the unmanaged type. should I change...
6
by: SchoolOfLife | last post by:
Does an object in C++ of std::string type terminates with null character? The charater string literals in C++ (to maintain backward compatibility with C) end with a null character. Is this same with...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.