fwrite in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • AlarV
    New Member
    • Dec 2008
    • 37

    fwrite in C

    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:

    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
    Last edited by Banfa; Oct 30 '09, 09:05 AM. Reason: Changed char a[] to int a[]
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    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.

    Comment

    • AlarV
      New Member
      • Dec 2008
      • 37

      #3
      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

      Comment

      • drhowarddrfine
        Recognized Expert Expert
        • Sep 2006
        • 7434

        #4
        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.

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          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

          Comment

          • AlarV
            New Member
            • Dec 2008
            • 37

            #6
            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

            Comment

            • AlarV
              New Member
              • Dec 2008
              • 37

              #7
              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!

              Comment

              • Banfa
                Recognized Expert Expert
                • Feb 2006
                • 9067

                #8
                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.

                Comment

                • donbock
                  Recognized Expert Top Contributor
                  • Mar 2008
                  • 2427

                  #9
                  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.

                  Comment

                  • AlarV
                    New Member
                    • Dec 2008
                    • 37

                    #10
                    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

                    Comment

                    • newb16
                      Contributor
                      • Jul 2008
                      • 687

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

                      Comment

                      • AlarV
                        New Member
                        • Dec 2008
                        • 37

                        #12
                        Originally posted by newb16
                        ...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

                        Comment

                        • donbock
                          Recognized Expert Top Contributor
                          • Mar 2008
                          • 2427

                          #13
                          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.

                          Comment

                          Working...