Program works on screen but not on disk...

Discussion in 'C Programming' started by Blankdraw, Aug 15, 2003.

  1. Blankdraw

    Blankdraw Guest

    I have a program below that runs fine. It accesses a datafile for
    random-access transfers. Supposedly, it does this by accessing the
    file in binary mode. I do not understand how it does this without
    any such parameter. The lower-level open() functions use a |O_BINARY
    or some such specification.
    Q: Can someone tell me how I can use/modify this program and be left
    with a datafile that has sensible (read: ASCII) data in it???



    /* RWARRAY.C - Loads and saves an array from a database file on-disk
    */

    #include <stdio.h>
    #include <string.h>
    #include <process.h>

    #define ITEMS 7

    int main()
    {
    char filename[85];
    int count;
    FILE *fileptr;
    int data[ITEMS] = {8, 57, 5, 309, 33, 87, 55};
    int data2[ITEMS];

    printf("\nEnter filename: ");
    gets(filename);

    /* write array to file */
    if ((fileptr = fopen(filename, "w")) == NULL)
    {
    printf("Error: Cannot open your pants\n");
    exit(0);
    }

    printf("Writing data items to file %s....\n", filename);

    fwrite(data, sizeof(data), 1, fileptr);
    fclose(fileptr);

    /* Read file into array */
    if ((fileptr = fopen(filename, "r")) == NULL)
    {
    printf("Error: Cannot open your pants\n");
    exit(0);
    }

    printf("Reading data items from file....\n");
    fread(&data2, sizeof(data), 1, fileptr);


    fclose(fileptr);
    printf("The elements of the array are: \n");
    for (count = 0; count < ITEMS; count++)
    {
    printf("Element %d is %d\n", count, data2[count]);
    }

    return 0;
    }
     
    Blankdraw, Aug 15, 2003
    #1
    1. Advertising

  2. Blankdraw

    -berlin.de Guest

    Blankdraw <> wrote:
    > I have a program below that runs fine. It accesses a datafile for
    > random-access transfers. Supposedly, it does this by accessing the
    > file in binary mode. I do not understand how it does this without
    > any such parameter. The lower-level open() functions use a |O_BINARY
    > or some such specification.


    Binary or non-binary mode doesn't make any difference when you use
    fwrite() and fread(), it is only relevant (on some systems) when
    using fprintf() and fscanf() (mostly about how the '\n' character
    is interpreted).

    > Q: Can someone tell me how I can use/modify this program and be left
    > with a datafile that has sensible (read: ASCII) data in it???


    > /* RWARRAY.C - Loads and saves an array from a database file on-disk
    > */


    > #include <stdio.h>
    > #include <string.h>


    Not requred for your program.

    > #include <process.h>


    What is process.h? That's not a standard header file and there's
    nothing in your program that would make it necessary to include
    the file.

    > #define ITEMS 7


    > int main()
    > {
    > char filename[85];
    > int count;
    > FILE *fileptr;
    > int data[ITEMS] = {8, 57, 5, 309, 33, 87, 55};
    > int data2[ITEMS];


    > printf("\nEnter filename: ");
    > gets(filename);


    Never, ever use gets(). There's no way you can make sure that the
    input will fit into the buffer you pass to gets(). Use fgets()
    instead.

    > /* write array to file */
    > if ((fileptr = fopen(filename, "w")) == NULL)
    > {
    > printf("Error: Cannot open your pants\n");
    > exit(0);
    > }


    > printf("Writing data items to file %s....\n", filename);


    > fwrite(data, sizeof(data), 1, fileptr);


    If you want to write out everything in ASCII you need the fprintf()
    function, i.e.

    for ( i = 0; i < ITEMS; i++ )
    fprintf( fileptr, "%d\n", data[ i ] );

    > fclose(fileptr);


    > /* Read file into array */
    > if ((fileptr = fopen(filename, "r")) == NULL)
    > {
    > printf("Error: Cannot open your pants\n");
    > exit(0);
    > }


    > printf("Reading data items from file....\n");
    > fread(&data2, sizeof(data), 1, fileptr);


    And, if you have written out the data in ASCII, you would use fscanf():

    for ( i = 0; i < ITEMS; i++ )
    if ( fscanf( fileptr, "%d", data2 + i ) != 1 )
    {
    fprintf( stderr, "Input file is broken.\n" );
    fclose( fileptr );
    exit( EXIT_FAILURE );
    }

    (Don't forget to include <stdlib.h> should you use EXIT_FAILURE.)

    > fclose(fileptr);
    > printf("The elements of the array are: \n");
    > for (count = 0; count < ITEMS; count++)
    > {
    > printf("Element %d is %d\n", count, data2[count]);
    > }


    > return 0;
    > }

    Regards, Jens
    --
    _ _____ _____
    | ||_ _||_ _| -berlin.de
    _ | | | | | |
    | |_| | | | | | http://www.physik.fu-berlin.de/~toerring
    \___/ens|_|homs|_|oerring
     
    -berlin.de, Aug 15, 2003
    #2
    1. Advertising

  3. >> I have a program below that runs fine. It accesses a datafile for
    >> random-access transfers. Supposedly, it does this by accessing the
    >> file in binary mode. I do not understand how it does this without
    >> any such parameter.


    The second argument of fopen() may be, for example, "r" vs. "rb" for
    text vs. binary mode.

    >>The lower-level open() functions use a |O_BINARY
    >> or some such specification.

    >
    >Binary or non-binary mode doesn't make any difference when you use
    >fwrite() and fread(), it is only relevant (on some systems) when
    >using fprintf() and fscanf() (mostly about how the '\n' character
    >is interpreted).


    The difference between text and binary mode affects how the '\n'
    character is interpreted, *REGARDLESS* of whether you use fwrite()
    or fprintf().

    Assuming that string points at a string (no embedded \0 characters)
    that contains newline(s), and foo is a stream opened for output,
    you should get the same result in the file from:

    char *string;
    FILE *foo;

    fprintf(foo, "%s", string);
    OR
    fwrite(string, 1, strlen(string), foo)


    What matters (if there is any difference at all: on some systems
    there isn't) is how the file was opened.

    Gordon L. Burditt
     
    Gordon Burditt, Aug 15, 2003
    #3
  4. (Gordon Burditt) wrote in
    <bhj69h$>:

    <SNIP>

    >The difference between text and binary mode affects how the '\n'
    >character is interpreted, *REGARDLESS* of whether you use fwrite()
    >or fprintf().
    >
    >Assuming that string points at a string (no embedded \0 characters)
    >that contains newline(s), and foo is a stream opened for output,
    >you should get the same result in the file from:
    >
    > char *string;
    > FILE *foo;
    >
    > fprintf(foo, "%s", string);
    >OR
    > fwrite(string, 1, strlen(string), foo)
    >
    >
    >What matters (if there is any difference at all: on some systems
    >there isn't) is how the file was opened.


    Question: is it good or bad practice if I fopen() text-files
    in binary mode to avoid possible automatic line delimiter
    translation (CR, LF, CRLF, ...) on different OSs and leave
    the control over line delimiting to my program?

    Irrwahn

    --
    If you can see it and it’s there - it’s real.
    If you can’t see it, but it’s there - it’s transparent.
    If you can see it but it isn’t there - it’s virtual.
    If you can’t see it and it isn’t there - it’s gone.
     
    Irrwahn Grausewitz, Aug 15, 2003
    #4
  5. ><SNIP>
    >
    >>The difference between text and binary mode affects how the '\n'
    >>character is interpreted, *REGARDLESS* of whether you use fwrite()
    >>or fprintf().
    >>
    >>Assuming that string points at a string (no embedded \0 characters)
    >>that contains newline(s), and foo is a stream opened for output,
    >>you should get the same result in the file from:
    >>
    >> char *string;
    >> FILE *foo;
    >>
    >> fprintf(foo, "%s", string);
    >>OR
    >> fwrite(string, 1, strlen(string), foo)
    >>
    >>
    >>What matters (if there is any difference at all: on some systems
    >>there isn't) is how the file was opened.

    >
    >Question: is it good or bad practice if I fopen() text-files
    >in binary mode to avoid possible automatic line delimiter
    >translation (CR, LF, CRLF, ...) on different OSs and leave
    >the control over line delimiting to my program?


    If they really *ARE* text files, treat them as such. Among other
    things, FTP in ASCII mode should automatically translate line
    delimiters from one format to another (using a fixed standard for
    traffic on the net, neither system needs to know what the OTHER
    system uses). Also, you're going to have problems with your binary
    so-called text files if you ever expect to manipulate them with
    standard tools like a text editor or print spooler.

    If you need to share the files among machines with different OSs
    and line-ending conventions via, say, NFS, then maybe you should
    use a portable binary format. (Portable in this sense means laying
    down format rules like: NO MULTIBYTE INTEGERS (or specify the byte
    ordering and size), NO FLOATING POINT (or specify the exact format
    used), line endings (if you even have lines) are THIS, etc.).

    Gordon L. Burditt
     
    Gordon Burditt, Aug 15, 2003
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jas Shultz
    Replies:
    0
    Views:
    996
    Jas Shultz
    Dec 3, 2003
  2. F. GEIGER
    Replies:
    3
    Views:
    815
    F. GEIGER
    Aug 6, 2004
  3. David Wang
    Replies:
    0
    Views:
    758
    David Wang
    Dec 1, 2006
  4. Replies:
    12
    Views:
    543
    santosh
    Nov 15, 2006
  5. owl
    Replies:
    2
    Views:
    910
Loading...

Share This Page