By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,120 Members | 1,275 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,120 IT Pros & Developers. It's quick & easy.

how to write a variable to a file ?

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a

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

P: n/a
"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

P: n/a
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.