File output

This is a discussion on File output within the C Programming forums, part of the General Programming Boards category; Hello everyone, I don't know any c, but I have the code of the mersenne twister (a random number generator) ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    2

    File output

    Hello everyone,

    I don't know any c, but I have the code of the mersenne twister (a random number generator) and I'd like to output its results to a text file.

    I'd really appreciate it if somebody could add the piece of code that is needed to do so as I couldn't manage to do so.
    The output is at the very bottom of the code and there are two different outputs.

    Here's the link to the code:
    http://www.math.sci.hiroshima-u.ac.j...ES/mt19937ar.c

    Regards,
    Patrick

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Just do some research on the following functions:
    fopen()
    fwrite()
    fclose()

    You also could use one of the print formatting functions to use your file stream as its output, but using the three functions listed above should be sufficient. There is also a basic example on those pages.

    Edit: Just thought of this, but if the current output to the command line satisfies you, you could just pipe it to a text file. ex. filename.exe >> out.txt
    I believe its the same for linux too.
    Last edited by carrotcake1029; 05-29-2009 at 10:55 AM.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2
    Thanks for your reply.
    So far I couldn't achieve anything with fwrite, but I've tried the following:

    Code:
    FILE *fp;
    fp=fopen("numbers.txt", "w+");
    fprintf(fp,"%0.0f ", u);
    fclose(fp);
    (u is the variable i'm trying to write)

    But if I run, the output file looks like this:
    Code:
    ′‴‶‵′‵‵‵″‶″‴″″‵′′′‴‱″″″‶′‶‵″‵′″′′‶‵′‴‱′‵‵‵′‴′‶‴‴‴′‱′‴‵′′″‱‶′‶‶‱‱‵′‵″″‶″″′‴″‴‴‴′′″′‵‴″′‴″′′‵‴″‵″‴‴‴′″‱‴‵‶‱‴″‶‱‴″‱‵′‵‱‵′‵″‴′
    Can you tell me where my mistake is?

    EDIT: If I wirte %1.1f insted of %0.0f it works, even though the numbers all have a .0 (e.g. 5.0 or 4.0), but I can live with that.

    Thanks for your help
    Last edited by unkios; 05-29-2009 at 12:09 PM.

  4. #4
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    You should refer to a reference to make sure you're using the functions correctly. Here's a good one: The C Standard Library. fwrite is used for writing objects out of an array of a certain size. I would probably only use it when writing structures to binary files. fprintf and fputs are probably pretty for your purposes right now.

    Also check your format string. What kind of variable is u? What format were you expecting? Me thinks you mean %f.

  5. #5
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    I wouldn't use it to write whole structures either. Just their components, because most structures need padding.

    But why do you want to fill it with zeros if you don't specify a size? e.g. "%06.0"? that's also why "%1.1" works. You must specify the size directly after the zero.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Brafil View Post
    I wouldn't use it to write whole structures either. Just their components, because most structures need padding.
    fwrite will respect that:
    Code:
    struct thing *eg=malloc(sizeof(struct thing));
    fwrite(eg,sizeof(struct thing),1,stdout);
    That's what it's for...
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    I know, but it's not portable at all. Although int etc. is not very portable either. But there one can achieve _a little_ more by using fixed-width data types.

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Brafil View Post
    I know, but it's not portable at all.
    fwrite is probably *more* portable than the format of a binary file, so it will never be the source of the problem. It and the underlying write() are also part of POSIX.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    Making mistakes
    Join Date
    Dec 2008
    Posts
    476
    Yes. Damn endianness. And bad, bad 36-bit systems. I understand 16-bit vs. 32-bit. Not that they are guilty, either. It's just difficult to fulfill such jobs because of them.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 11:12 PM
  5. Simple File Creation Algorithm
    By muffin in forum C Programming
    Replies: 13
    Last Post: 08-24-2001, 04:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21