how to write a variable to a file ?

P

priyanka

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
 
B

Bill Pursell

priyanka said:
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.
 
K

Keith Thompson

priyanka said:
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.
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.
 
S

Simon Biber

priyanka said:
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::eek:fstream file("list.txt");
file << mp->core[employee_to_manager(22)];
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,184
Messages
2,570,978
Members
47,561
Latest member
gjsign

Latest Threads

Top