468,467 Members | 2,686 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,467 developers. It's quick & easy.

how to write a variable to a file ?

Hi there,

I have a C programming problem for which I need help.

I have a structure pointer which has 3 different values in it.

I can print the value in the valriable in the stdout as:
cout << mp->core[employee_to_manager(22)];

This is the only way of retreiving the value of the employee number 22.

and the ouput is like this:
22 name = aka, val = 00

However, I need to print the same value in an external file called
list.txt.

I used:
fprintf(fp,"%d\n",mp->core[employee_to_manager(22)]);

But this does not work. It gives error like "segmentation fault".

Can anyone help me how to get "22 name = aka, val = 00" printed in the
file list.txt.

Thank you in advance,

priya

Jul 6 '06 #1
3 4532

priyanka wrote:
Hi there,

I have a C programming problem for which I need help.

I have a structure pointer which has 3 different values in it.

I can print the value in the valriable in the stdout as:
cout << mp->core[employee_to_manager(22)];

This is the only way of retreiving the value of the employee number 22.

and the ouput is like this:
22 name = aka, val = 00

However, I need to print the same value in an external file called
list.txt.

I used:
fprintf(fp,"%d\n",mp->core[employee_to_manager(22)]);

But this does not work. It gives error like "segmentation fault".

Can anyone help me how to get "22 name = aka, val = 00" printed in the
file list.txt.
1) "cout <<" implies that your code is C++, which is not C.
2) read the man page for fopen, which can be used to give you a FILE *
which can then be passed to fprintf.

Jul 6 '06 #2
"priyanka" <pr**********@gmail.comwrites:
I have a C programming problem for which I need help.

I have a structure pointer which has 3 different values in it.

I can print the value in the valriable in the stdout as:
cout << mp->core[employee_to_manager(22)];

This is the only way of retreiving the value of the employee number 22.
That's C++, not C.

<OT>
In C++, the "<<" operator is overloaded. We can't tell from the
fact that your statement above is legal what type the expression is.
</OT>
and the ouput is like this:
22 name = aka, val = 00
It looks like mp->core[employee_to_manager(22)] is probably a string.
However, I need to print the same value in an external file called
list.txt.

I used:
fprintf(fp,"%d\n",mp->core[employee_to_manager(22)]);

But this does not work. It gives error like "segmentation fault".
"%d" expects an argument of type int. If mp->core[employee_to_manager(22)]
is a char* that points to a C string, you need to use "%s". If it's
something else, you'll need to do something else; we can't guess what,
since you haven't given us enough information.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 6 '06 #3
priyanka wrote:
Hi there,

I have a C programming problem for which I need help.
Actually it's C++. I have posted this followup to comp.lang.c++ as well
as the original comp.lang.c.
I have a structure pointer which has 3 different values in it.

I can print the value in the valriable in the stdout as:
cout << mp->core[employee_to_manager(22)];

This is the only way of retreiving the value of the employee number 22.

and the ouput is like this:
22 name = aka, val = 00
Without seeing the declaration of core, it's hard to tell what type the
expression mp->core[employee_to_manager(22)] has. It may be a string, or
it may be a user-defined type for which an overloaded version of the <<
operator has been provided.
However, I need to print the same value in an external file called
list.txt.
Assuming it is a string, or that the overloaded operator << in question
is written to work on any output stream, you should be able to use code
like this:

#include <fstream>

std::ofstream file("list.txt");
file << mp->core[employee_to_manager(22)];

--
Simon.
Jul 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Google Mike | last post: by
6 posts views Thread by Tony C | last post: by
2 posts views Thread by tnhoe | last post: by
8 posts views Thread by Patrik Malmström | last post: by
reply views Thread by chipotle_pickle | last post: by
3 posts views Thread by David Thielen | last post: by
24 posts views Thread by Bill | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.