473,406 Members | 2,954 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,406 software developers and data experts.

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 4875

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Google Mike | last post by:
I have PHP version 4.2.2 that ships with RH9. I want to have it write to a file like this: function WriteLog($sLogFile, $sMsg) { if (substr($sLogFile,1,1) != '/') { $sLogFile =...
6
by: Tony C | last post by:
Does Python have a function that is analogous to C's write() or fwrite()- that is , I want to write a file (of arbitrary data) that is 100K, or 1MB (or more) bytes long.. Both write() and...
2
by: tnhoe | last post by:
hi, how do i write variable value into text file from jaavscript ? regards hoe
8
by: Patrik Malmström | last post by:
How do I read, write a file binary? I want to open, say, file.exe read it in to the program, then write it out to file2.exe. Like file copy, anyone have a code sample?
0
by: chipotle_pickle | last post by:
You're letting a lot of bad links creep onto that page. Do a Google search for "genesis world energy". The second item found is a broken link to the newsroom. (The third item found explains how the...
2
by: ykgoh | last post by:
Hi. I've a problem of being able to create and remove a directory but unable to write a file inside the created directory for some strange reason. I suspect that this problem could be vaguely...
3
by: David Thielen | last post by:
Hi; I created a virtual directory in IIS 6.0 and my asp.net app runs fine. But when it tries to write a file I get: Access to the path is denied. - C:\Inetpub\wwwroot\RunReportASP\images ...
2
by: Bina | last post by:
hi, I want to read & write a html file which contain the japanese character. Now how i will do it?I can read & write english character . but when i read & write Japanese character from the html...
24
by: Bill | last post by:
Hello, I'm trying to output buffer content to a file. I either get an access violation error, or crazy looking output in the file depending on which method I use to write the file. Can anyone...
6
by: Thanh-Nhan Le | last post by:
Hello, I have an ASP application on IIS server: http://localhost/myApp I use ASP and a my own VB activex DLL to create a pdf file and write this file to a subfolder of the Application folder:...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...

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.