473,856 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

fwrite in C

37 New Member
Hello,
I've been trying to build a program with fwrite( ), that just writes an array of 10 integers at a file called Project.doc. Here is the code:

Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[])
  2. {
  3.   FILE *fs;
  4.   int irc;
  5.   fs=fopen("Project.doc","w");
  6.   int a[10]={1,2,3,4,5,6,7,8,9,10};
  7.   irc=fwrite(a,sizeof(int),10,fs);
  8.   printf(" fwrite return code = %d\n", irc);
  9.   fclose(fs);
  10.   system("PAUSE");    
  11.   return 0;
  12. }
  13.  
The .doc file is created, however there are no numbers in it. Instead there are some weird symbols. Can anyone help?

Thanx,
Alex
Oct 29 '09 #1
12 21100
donbock
2,426 Recognized Expert Top Contributor
Consider the difference between the number 1 and the character constant '1'. These are quite different things, with different values.

Consider the number 10. This number resides in a single variable. However, to print it out you need to print '1' followed by '0' -- two characters to represent a single number. You are confusing the value of a number and the characters you print to represent the number.

char a[10]={1,2,3,4,5,6,7 ,8,9,10};
Although the type is 'char', these are numbers not characters.
Oct 29 '09 #2
AlarV
37 New Member
So this means that fwrite( ) is only for characters? Could you be a little more specific? What do I have to do so that I write in a .txt file those 10 integers? Am I using the wrong functions?

Thanx,
Alex
Oct 29 '09 #3
drhowarddrfine
7,435 Recognized Expert Expert
No, he's saying what you have in your array is not chars. You need to change that so you have '1','2','3' with the single quotes so the compiler knows these are the chars you want.
Oct 30 '09 #4
gpraghuram
1,275 Recognized Expert Top Contributor
Usually fwrite is used to write a structure or a block of data into a file.
When you try to open the file written by fwrite it will be like junk but when u read it using fread you will get actual contents.
You shuld open a file in wb mode to use fwrite.


Raghu
Oct 30 '09 #5
AlarV
37 New Member
Thanks for the answers. Now I see!

@gpraghuram: I used "wb" mode too, but some weird symbols appear in the .doc file :/

I used this easy example(integer s from 1 to 10) so that I understand the logic. The real program that I want to make creates an array of 10 integers with rand( ) and then I want to fwrite( ) them in a text file. From what you are saying, this means that I have to save them as strings, and fwrite( ) them in the .doc file. Am I right?

Kind regards,
Alex
Oct 30 '09 #6
AlarV
37 New Member
Omg I just saw that I wrote char a[10]! I'm sorry, I meant int a[10]! I'll edit it in the first post!
Oct 30 '09 #7
Banfa
9,065 Recognized Expert Moderator Expert
Actually no you don't need to save them as strings. In fact IMO in general strings should be avoided except for data you wish to be human readable, they are no good as a type for doing calculations with.

However I feel you might benefit from looking up the function fprintf which has a similar function to printf but writes to a stream rather than the console.


P.S. you probably can't change your first post now but I have done it for you.
Oct 30 '09 #8
donbock
2,426 Recognized Expert Top Contributor
You have to decide what you want to put in your output file.

You could put the numbers 1-10 into the file. These are numbers, not characters, so this would correspond to a binary file. No point opening a binary file with an editor, it does not contain printable characters so you will only see funny characters.

You could put the text characters corresponding to the numbers 1-10 in the file. This would correspond to a text file. In this case you can open the file with an editor and see the text representation of those numbers.

You need to clarify what you're trying to accomplish before we can guide you further.
Oct 30 '09 #9
AlarV
37 New Member
Thanx for the answers and thanx for editing my text!

Actually what I have to accomplish is a project ,at a Linux(ubuntu) operating system, for the university. I was asked to create an array of 10 integers by using rand( ). Then these 10 integers are written in a text file and they can be read by an editor(e.g. notepad). What the project exactly says is:

"The array will be written at stdout by using write"

I haven't been taught to use streams or write, yet. So I have been trying things on my own, by reading the book. I have already used some functions (e.g. fprintf, fwrite), but the project says to use write. How will I pull it off?

Thanks in advance,
Alex
Oct 30 '09 #10

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

Similar topics

3
2453
by: Antoine Bloncourt | last post by:
Hello everybody Sorry to bother you but I have a problem writing datas into a file ... I want to make a backup of my MySQL database and put the result into a ..sql file. To do this, I use the "get_table_strucure" and "get_table_content" functions.
23
17868
by: FrancisC | last post by:
how to use fwrite( ) instead of fprintf( ) in this case? I want to generate binary file. FILE *fnew; int i, intName; double array; fprintf(fnew, "%d\n", intName); fprintf(fnew, " %f", array);
17
6129
by: SW1 | last post by:
I wrote a small program which does something like tftp - transfering files and some chat, anyway i got a problem with fwrite, here is a snippet of my code: while(length > 0) { putchar('.'); //These were for error checking if(length <= bsize) { //Buffer is bigger than remaining File realloc(buffer,length * sizeof(char)); //resize buffer to remain rec = recv(sock,buffer,length,0); //recieve Data
15
3916
by: Suraj Kurapati | last post by:
Hello, I'm having a rather strange bug with this code: for certain values of 'buf', a segmentation fault occurs when 'free(buf)' is followed by an 'fwrite()'. In the program output, there is no error reported by 'perror()' and the file is written correctly. /* returns NULL upon failure, or a calloc()ed data upon success */ char* recv_data(int *bytes_read);
4
2487
by: ibrahimover | last post by:
typedef struct{ char name; int no; }TAM; typedef struct{ char name; char ch; }HARF;
3
4541
by: sumit1680 | last post by:
Hi everyone, I am using the below listed code The code is #include<stdio.h> #include<stdlib.h> #include<string.h>
2
6325
by: Richard Hsu | last post by:
// code #include "stdio.h" int status(FILE * f) { printf("ftell:%d, feof:%s\n", ftell(f), feof(f) != 0 ? "true" : "false"); } int case1() { FILE * f = fopen("c:\\blah", "wb+"); int i = 5;
10
2701
by: Sheldon | last post by:
Hi, I am trying to learn C from scratch and, though I do know how to program in Python, many things in C are hard to understand - even after reading the examples. I guess because so many variations exists. Can someone explain why this variation of fwrite fails: #include <stdio.h> #include <stdlib.h> #define ROW 15 /* In order to have more memory and no segmentation
12
5108
by: hemant.gaur | last post by:
I have an application which writes huge number of bytes into the binary files which is just some marshalled data. int len = Data.size(); //arrary size for (int i = 0; i < len; ++i) fwrite(&Data, 1, 1, f); now after running this for long time and pushing millions of bytes, It once misses writing the last byte of fData. Then the further push of bytes is again correct. As i am not using the return value for the
25
15591
by: Abubakar | last post by:
Hi, recently some C programmer told me that using fwrite/fopen functions are not efficient because the output that they do to the file is actually buffered and gets late in writing. Is that true? regards, ...ab
0
9906
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9758
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10774
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10379
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9528
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7088
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5757
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5956
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4171
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.