Prob reading from file with C/C++

Discussion in 'Programmer's Corner' started by ignite, Mar 11, 2008.

  1. ignite

    Thread Starter Member

    Jan 31, 2008
    Hi im trying to read 6 bytes of data from a previously created file but wen i type in the file name it stops responding and crashes, its a binary file. i have included the code used to create the binary file(its commented out) and the code im using to read the binary file.Any tips???

    Thank you

    #include <cstdio>
    #include <cstdlib>
    #include <iostream>

    using namespace std;

    /* random record description - could be anything */

    struct rec


    int x,y,z;

    //writes and then reades 6 arbitrary records from the file "sequence".

    int main(void)

    int i,TxData[6];
    char word[100];
    FILE *f;
    struct rec r;
    char fname[100];

    //create the file of 6 bytes
    // f=fopen("sequence","wb");
    // for(i=0;i<6;i++){
    // scanf("%s", &word);
    // TxData[("%d",i)] = strtol(word,NULL,2);
    // getchar();
    // fwrite(&r,sizeof(struct rec),1,f);
    // }
    // fclose(f);
    // printf("\n");
    //read the 6 records back

    printf("Enter file name\n");
    fread(&r,sizeof(struct rec),1,f);

    return 0;
  2. Mark44

    Well-Known Member

    Nov 26, 2007
    It's not really germane to your problem, but I think you're trying read 6 records from the file, not 6 bytes.
    You're using "fname" as a string constant in the line above. When you created the file earlier, its name was "sequence". Now you're trying to open a file named "fname" rather then using your variable of the same name. The call to fopen should look like this:
    Code ( (Unknown Language)):
    2.     f = fopen(fname, "r");
    Other tips...
    Use the fact that fopen and the other file functions return values when a failure occurs. For example, fopen returns NULL if it fails to open a file. fclose returns EOF if an error occurs and zero otherwise. The other I/O functions also return values that can be useful in diagnosing errors.

    Hope that's helpful!
  3. ignite

    Thread Starter Member

    Jan 31, 2008
    thanks for that mark, i kind of new to programming so get confused with the commands, i ave learned from internet examples and few books, is there a command for reading bytes of code from a file at the time??? Is it possible to generate a file with an arbitrary number of bytes for example 100 bytes of random data????

    Thank you
  4. Mark44

    Well-Known Member

    Nov 26, 2007
    Reading a block of bytes:
    fread(void* ptr, size_t size, size_t nobj, FILE* stream)

    ptr is the array into which the block of bytes will go.
    size is the size of each object to read (if each is a byte, size will be 1).
    nobj is the number of objects to read.
    stream is the stream of bytes from which to read.

    fread returns the number of objects actually read (as type size_t).

    To write to a file, you can use fwrite(), which has almost the same parameters as fread(). The only difference is the first parameter, which is of type const void *. It's the source of the bytes that will be written, and will typically be an array of char or a pointer to char.
    I'm not sure I understand your last question completely, about writing 100 bytes of random data. Do you really want it to be random, or do you mean to write, for example, 100 bytes of arbitrary data? In any case, fwrite() will write what you have in the first argument in the call to this function. If you have uninitialized data in the array, that's what will be written.

    Do you have some documentation about these standard library functions? The information I provided here came from The C Programming Language, 2nd Ed., by Kernighan and Ritchie (aka K & R).