5

Here is my code as of now:

#include <stdio.h>
#include "readwrite.h"

int main ()
{   FILE* pFile;
    char buffer[] = {'x' ,'y','z',0};
    pFile = fopen("C:\\Test\\myfile.bin","wb");

    if (pFile ){
        fwrite(buffer,1,sizeof(buffer),pFile); 

    printf("The buffer looks like: %s",buffer);
  }
    else
    printf("Can't open file");


   fclose(pFile);
   getchar();
   return 0;
}

I'm trying to write something verify i wrote to the file and then read from the file and verify i read from the file. How best is there to do this? I also need to figure out a way to write the same thing to 2 different files. Is this even possible?

2 Answers 2

10

I think you are looking for something like this:

FILE* pFile;
char* yourFilePath  = "C:\\Test.bin";
char* yourBuffer    = "HelloWorld!";
int   yorBufferSize = strlen(yourBuffer) + 1;

/* Reserve memory for your readed buffer */
char* readedBuffer = malloc(yorBufferSize);

if (readedBuffer==0){
    puts("Can't reserve memory for Test!");
}

/* Write your buffer to disk. */
pFile = fopen(yourFilePath,"wb");

if (pFile){
    fwrite(yourBuffer, yorBufferSize, 1, pFile);
    puts("Wrote to file!");
}
else{
    puts("Something wrong writing to File.");
}

fclose(pFile);

/* Now, we read the file and get its information. */
pFile = fopen(yourFilePath,"rb");

if (pFile){
    fread(readedBuffer, yorBufferSize, 1, pFile);
    puts("Readed from file!");
}
else{
    puts("Something wrong reading from File.\n");
}

/* Compare buffers. */
if (!memcmp(readedBuffer, yourBuffer, yorBufferSize)){
    puts("readedBuffer = yourBuffer");
}
else{
    puts("Buffers are different!");
}

/* Free the reserved memory. */
free(readedBuffer);

fclose(pFile);
return 0;

Regards

Sign up to request clarification or add additional context in comments.

3 Comments

excellent!!! How would i do this if instead i were going to use numbers instead of strings and letterS?
Numbers, structs, strings... all is a memory block with a memory address. So you could make a cast of this memory blocks to a byte buffer and then write/read to a file without problems. Try something like (char*)&myIntegerVariable to get the myIntegerVariable's address and sizeof(int) to get the variable length. Good luck.
fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream); so it should be corrected to: fwrite(yourBuffer, 1, yorBufferSize, pFile);
1

The program to read a buffer is almost the same except "rb" for read binary and fread() instead of fwrite()

Remember you will have to know how big the buffer you are going to read is and have some memory of the right size ready to receive it

2 Comments

Can i wrap the Buffer size in a #define and change it that way? I need the 0 after whats supposed to be in the buffer so I don't get "junk" when i compile and run. For instance, when i printf with the code above, i see x y and z but then i see junk because it searches for a null.
@Questioneer, for simple tests that's easiest - later you can detect the file size and learn about malloc. You need the trailing zero becuse that marks the end of string in c and you a printing a string - you could also loop over the buffer and print individual characters

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.