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: - int main(int argc, char *argv[])
-
{
-
FILE *fs;
-
int irc;
-
fs=fopen("Project.doc","w");
-
int a[10]={1,2,3,4,5,6,7,8,9,10};
-
irc=fwrite(a,sizeof(int),10,fs);
-
printf(" fwrite return code = %d\n", irc);
-
fclose(fs);
-
system("PAUSE");
-
return 0;
-
}
-
The .doc file is created, however there are no numbers in it. Instead there are some weird symbols. Can anyone help?
Thanx,
Alex
12 20807
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.
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
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.
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
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
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 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.
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.
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
...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?
@newb16
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
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.
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
| | | | | | | | | | |