472,096 Members | 1,223 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,096 software developers and data experts.

fwrite in C

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. }
The .doc file is created, however there are no numbers in it. Instead there are some weird symbols. Can anyone help?

Oct 29 '09 #1
12 20807
2,425 Expert 2GB
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
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?

Oct 29 '09 #3
7,435 Expert 4TB
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
1,275 Expert 1GB
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.

Oct 30 '09 #5
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(integers 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,
Oct 30 '09 #6
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
9,065 Expert Mod 8TB
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
2,425 Expert 2GB
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
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,
Oct 30 '09 #10
687 512MB
...10 integers are written in a text file ...
...will be written at stdout by using write....

These requirement contradict each other ( unless you use redirect when running your program, like a.out > output.txt). What if 'write' should be 'fwrite' reallyl?
Oct 30 '09 #11
That's what the teacher probably meant: To redirect the stdout to the txt file. However, I have no clue on how to do this.. I did the whole program(what I'm asking is a part of the whole project) and I stuck on this little thing. Could someone post an example of the code, so that I get the basic idea and then implement it by myself?

Oct 30 '09 #12
2,425 Expert 2GB
Take a look at the formatted-output functions. fprintf goes directly to a file; if you must use write then take a look at sprintf.
Oct 31 '09 #13

Post your reply

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

Similar topics

3 posts views Thread by Antoine Bloncourt | last post: by
23 posts views Thread by FrancisC | last post: by
17 posts views Thread by SW1 | last post: by
15 posts views Thread by Suraj Kurapati | last post: by
4 posts views Thread by ibrahimover | last post: by
3 posts views Thread by sumit1680 | last post: by
2 posts views Thread by Richard Hsu | last post: by
10 posts views Thread by Sheldon | last post: by
12 posts views Thread by hemant.gaur | last post: by
25 posts views Thread by Abubakar | last post: by
reply views Thread by leo001 | last post: by

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.