Connecting Tech Pros Worldwide Forums | Help | Site Map

fwrite in C

Newbie
 
Join Date: Dec 2008
Posts: 20
#1: 4 Weeks Ago
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

Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#2: 4 Weeks Ago

re: fwrite in C


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.
Newbie
 
Join Date: Dec 2008
Posts: 20
#3: 4 Weeks Ago

re: fwrite in C


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
drhowarddrfine's Avatar
Expert
 
Join Date: Sep 2006
Posts: 5,580
#4: 4 Weeks Ago

re: fwrite in C


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.
gpraghuram's Avatar
Expert
 
Join Date: Mar 2007
Location: Chennai
Posts: 1,258
#5: 4 Weeks Ago

re: fwrite in C


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
Newbie
 
Join Date: Dec 2008
Posts: 20
#6: 4 Weeks Ago

re: fwrite in C


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,
Alex
Newbie
 
Join Date: Dec 2008
Posts: 20
#7: 4 Weeks Ago

re: fwrite in C


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!
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,202
#8: 4 Weeks Ago

re: fwrite in C


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.
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#9: 4 Weeks Ago

re: fwrite in C


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.
Newbie
 
Join Date: Dec 2008
Posts: 20
#10: 4 Weeks Ago

re: fwrite in C


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
Needs Regular Fix
 
Join Date: Jul 2008
Posts: 386
#11: 4 Weeks Ago

re: fwrite in C


...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?
Newbie
 
Join Date: Dec 2008
Posts: 20
#12: 4 Weeks Ago

re: fwrite in C


Quote:

Originally Posted by newb16 View Post

...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?

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?

Alex
Expert
 
Join Date: Mar 2008
Location: Naperville, Illinois U.S.
Posts: 831
#13: 4 Weeks Ago

re: fwrite in C


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.
Reply


Similar C / C++ bytes