Thread: Using variables in fwrite

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    27

    Using variables in fwrite

    Say I have a code like this:

    Code:
    int main() {
    
         int a=1;
         int b=2;
         int x=3;
         int y=4;
    
        FILE *writefile;
        char *filename = "test.out";
        writefile = fopen(filename, "w");
    
        char data[] = "";
    
        fwrite(data, 1, sizeof(data), writefile);
        fclose(writefile);
    
    }
    If I put a, b, and c in data[] like "char data[] = {'a', 'b', 'c'};" it will write this into "test.out":

    abc
    I'm trying to write my code so that it will write something like:
    Int a is: 1
    Int b is: 2
    Int x is: 3
    Int y is: 4
    How would I go about doing this? Should I even use a char array?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If you actually want your output to be "int a is 1", then you should use something like fprintf instead.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    27
    Ah, thanks! I didn't think fprintf would actually write into a file! Interesting.
    Last edited by never_lose; 04-04-2011 at 07:38 PM. Reason: fprint should be frpintf.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    fprintf = file printf
    sprintf = string printf

    Pretty much anything that starts with an f is for file, and anything that starts with an s is for string, in the standard library. I'm sure there are exceptions, but generally that's what they indicate.


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    fprintf = file printf
    sprintf = string printf
    printf = screen printf

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Accessing Variables
    By vileoxidation in forum C++ Programming
    Replies: 10
    Last Post: 10-05-2009, 07:58 AM
  2. Protected / Private Variables accessable.
    By +Azazel+ in forum C++ Programming
    Replies: 19
    Last Post: 09-08-2009, 07:39 PM
  3. Question about Static variables and functions.
    By RealityFusion in forum C++ Programming
    Replies: 2
    Last Post: 10-14-2005, 02:31 PM
  4. Remotely Creating Variables
    By Rajin in forum C++ Programming
    Replies: 1
    Last Post: 04-26-2005, 11:20 PM
  5. Declaring an variable number of variables
    By Decrypt in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2005, 04:46 PM