c - print file in hexa after fread - c

I have this code that read binary file content and then write it but fwrite print it as chars and I need to print the hexa value of each byte. I only find a way to do it using fprint (using format '%x')
void PrintHex(int *buffer, int length){
fwrite(buffer,length, 1 ,stdout);
}
int main(int argc, char **argv) {
FILE *inptr = fopen ( argv[1], "rb");
if (inptr == NULL)
{
fprintf (stderr, "Could notopen %s", argv[1]);
return 1;
}
int buffer[1000];
while (fread (&buffer, 1, 1, inptr) == 1)
{
PrintHex(buffer, 1);
}
fclose(inptr);
return 0;
}

Related

Unable to read from file from argv

I am trying to read from a file, and here is my code, but as I run my code nothing shows up. Have I used the getline() function incorrectly? I can not understand my problem.
const char *READ = "r";
/**
* main - Entry point of my program
*
* Return: On success, it returns 0. On
* error it returns -1
*/
int main(int ac, char **av)
{
FILE *fpointer;
char *lineptr = NULL;
size_t *n = 0;
int line_number = 1;
if (ac != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
fpointer = fopen(av[1], READ);
if (fpointer == NULL)
{
fprintf(stderr, "Error: Can't open file %s\n", av[1]);
exit(EXIT_FAILURE);
}
while (getline(&lineptr, n, fpointer) != -1)
{
printf("Line %d: %s\n", line_number, lineptr);
line_number++;
}
return (0);
}
getline(&lineptr, n, fpointer) returns -1. You did not explicitly check this and print an error message.
Checking errno it's because of EINVAL: invalid argument. Also good to check errno.
Reason is that n is NULL, while a pointer to an existing size_t is required.
BTW, indenting with 8 spaces is rather uncommon; I'd stay with 4 space. (Also, never use TAB characters.)
It's advisable to stick with extremely common argc and argv.
Nice you put {s on a further empty line; I like that style.
You'd get this:
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
const char *READ = "r";
int main(int argc, char **argv)
{
FILE *fpointer;
char *lineptr = NULL;
size_t n;
int line_number = 1;
if (argc != 2)
{
fprintf(stderr, "USAGE: monty file\n");
exit(EXIT_FAILURE);
}
fpointer = fopen(argv[1], READ);
if (fpointer == NULL)
{
fprintf(stderr, "Error: Can't open file '%s'.\n", argv[1]);
exit(EXIT_FAILURE);
}
if (getline(&lineptr, &n, fpointer) == -1)
{
printf("Failed to read file '%s': %s.\n", argv[1], strerror(errno));
exit(EXIT_FAILURE);
}
do
{
printf("Line %4d: %s\n", line_number, lineptr);
line_number++;
}
while (getline(&lineptr, &n, fpointer) != -1);
return (0);
}
Declaration of getline:
ssize_t getline(char **lineptr, size_t *n, FILE *stream);
As an output parameter, the type of n is size_t *. It points to a space for writing by getline.
But in your code, n points to 0, which is NOT a vaild addr to write in.

C programming, copying from one file to another using command line arguments

This is my code
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc < 4) {
printf("Missing arguments\n");
return -1;
}
// Check if buffer is valid before reading anything
int bufferSize = atoi(argv[3]);
if (!bufferSize || bufferSize < 1) {
printf("Invalid buffer size\n");
return -1;
}
printf("*** Copying from '%s' to '%s' (Buffer size: %dB) ***\n",
argv[1], argv[2], bufferSize);
// READ SOURCE FILE
FILE *inputFile = fopen(argv[1], "r");
if (!inputFile) {
printf("Error opening source file\n");
return -1;
}
// READ DESTINATION FILE
FILE *outputFile = fopen(argv[2], "w");
if (!outputFile) {
printf("Error opening destination file\n");
return -1;
}
int buffer[bufferSize];
int bytes;
do {
bytes = fread(buffer, 1, bufferSize, inputFile);
if (fwrite(buffer, 1, bytes, outputFile) != bytes) {
printf("Error writing into destination file\n");
return -1;
}
} while (bytes > 0);
fclose(inputFile);
fclose(outputFile);
return 0;
}
But when I try to exe the file it doesn't work. What could be the problem?
Here's the command line:
/Users/jurajc/Documents/Program/C/L1\ 1/C_program/c_program file.txt fileCopy.txt 512
*** Copying from 'file.txt' to 'fileCopy.txt' (Buffer size: 512B) ***
Error opening source file
The input file file.txt cannot be opened: either because it is not present in the current directory or because you do not have read access to it.
You should output more informative error messages. Note also these problems:
if (!bufferSize || bufferSize < 1) is a redundant test. if (bufferSize < 1) is sufficient.
the error messages should be output to stderr
the files should be open in binary mode to reliably copy all file types on legacy systems.
the read/write loop is incorrect: you should stop when fread returns 0 before attempting to write 0 elements to the output file.
Here is a modified version:
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[]) {
if (argc < 4) {
fprintf(stderr, "Missing arguments\n");
return -1;
}
// Check if buffer is valid before reading anything
int bufferSize = atoi(argv[3]);
if (bufferSize < 1) {
fprintf(stderr, "Invalid buffer size: %s\n", argv[3]);
return -1;
}
printf("*** Copying from '%s' to '%s' (Buffer size: %dB) ***\n",
argv[1], argv[2], bufferSize);
// READ SOURCE FILE
FILE *inputFile = fopen(argv[1], "rb");
if (!inputFile) {
fprintf(stderr, "Error opening source file %s: %s\n",
argv[1], strerror(errno));
return -1;
}
// READ DESTINATION FILE
FILE *outputFile = fopen(argv[2], "wb");
if (!outputFile) {
fprintf(stderr, "Error opening destination file %s: %s\n",
argv[2], strerror(errno));
return -1;
}
int buffer[bufferSize];
int bytes;
while ((bytes = fread(buffer, 1, bufferSize, inputFile)) != 0) {
if (fwrite(buffer, 1, bytes, outputFile) != bytes) {
fprintf(stderr, "Error writing into destination file: %s\n", strerror(errno));
return -1;
}
}
fclose(inputFile);
fclose(outputFile);
return 0;
}

How does FILE stream buffer work?

I understand fopen() opens file and creates a buffer for read and write operations on that file. fopen() returns a pointer for that buffer.
So my question is, in the code below, the _copy function body has a temp matrix to transfer between the fread() and fwrite(). why cant I directly transfer from buffer to buffer?
/* example: copyfile.exe xxxxx.txt zzzzzz.txt */
#include <stdio.h>
#include <stdlib.h>
#define BUFF 8192
void _copy(FILE *source, FILE *destination);
int main(int argc, char *argv[])
{
FILE *fp1, *fp2; // fp1 source file pointer// fp2 copied file pointer
if (argc !=3 ) //command line must have 3 arguments
{
fprintf(stderr, "Usage: %s (source file) (copy file)\n", argv[0][0]);
exit(EXIT_FAILURE);
}
if ((fp1 = fopen(argv[1], "rb")) == NULL) //Opening source file
{
fprintf(stderr, "Could not open %s\n",argv[1]);
exit(EXIT_FAILURE);
}
if((fp2 = fopen(argv[2], "ab+")) == NULL) //Opening destination file
{
fprintf(stderr, "could not create %s \n",argv[2]);
exit(EXIT_FAILURE);
}
if( setvbuf(fp1,NULL, _IOFBF, BUFF) != 0) //Setting buffer for source file
{
fputs("Can't create output buffer\n", stderr);
exit(EXIT_FAILURE);
}
if( setvbuf(fp2,NULL, _IOFBF, BUFF) != 0) //Setting buffer for destination file
{
fputs("Can't create input buffer\n", stderr);
exit(EXIT_FAILURE);
}
_copy(fp1, fp2);
if (ferror(fp1)!=0)
fprintf(stderr, "Error reading file %s\n", argv[1]);
if(ferror(fp2)!=0)
fprintf(stderr, "Error writing file %s\n",argv[2]);
printf("Done coping %s (source) to %s (destination) \n",argv[1], argv[2]);
fclose(fp1);
fclose(fp2);
return (0);
}
void _copy(FILE *source, FILE *destination)
{
size_t bytes;
static char temp[BUFF];
while((bytes = fread(temp,sizeof(char),BUFF,source))>0)
fwrite(temp,sizeof(char),bytes,destination);
}
You cannot use the underlying buffer from a FILE * in another FILE *. As you were told in comment, FILE * is an opaque pointer. But you can avoid the overhead of copying data between buffers by forcing both files in non buffered mode:
setbuf(fp, NULL); // cause the stream to be unbuffered

How to Write Differences in Two Text Files to Another Text File in C?

I'd like to write a program that compares two files and writes every byte in file one that is different from file two into a third file. I want to compare the files byte by byte and write any differing single bytes to the third file. I'm not very familiar with file I/O. Can someone give me an example program that accomplishes this task?
This is what I have so far:
int main(int argc, char *argv[]) {
int file1, file2, file1size, file2size;
// int difference1, difference2;
char buf;
if (argc != 3){
fprintf(stderr, "Usage %s <file1> <file2>", argv[0]);
exit(1);
}
if ((file1 = open(argv[1], 0400)) < 0) { //read permission for user on file source
fprintf(stderr, "Can't open source");
exit(1);
}
if ((file2 = open(argv[2], 0400)) < 0) { //read permission for user on file source
fprintf(stderr, "Can't open source");
exit(1);
}
file1size = lseek(file1, (off_t) 0, SEEK_END);
printf("File 1's size is %d\n", file1size);
file2size = lseek(file2, (off_t) 0, SEEK_END);
printf("File 2's size is %d\n", file2size);
}
I'm not sure how to compare file1 and file2's bytes and then write the differences to another file.
This is close to what you are looking for.
#include <stdio.h>
int main(int argc, char *argv[]) {
FILE *file1 = fopen(argv[1], "r");
FILE *file2 = fopen(argv[2], "r");
int i;
for(i = 0; !feof(file1) || !feof(file2); i++) {
int byte1 = getc(file1);
int byte2 = getc(file2);
if(byte1 != byte2) {
printf("%d %d %d\n", i, byte1, byte2);
}
}
return 0;
}
It takes the two files as command line arguments and compares the two byte-by-byte. If two bytes are different, it printf the character #, and the ASCII values of the two characters. -1 means EOF was already reached.
You'll have to (understand and) adapt this to the output format you want. (I'm assuming this is homework.)
feof tests for end-of-file.
getc gets the next character (byte) from the file. It is -1 if the end of the file has been reached.
And you seem already to know what printf does.
This does what you want, compiles, and runs,
#include <stdio.h>
int main(int argc, char *argv[])
{
int offset;
int argi=1;
int ch1, ch2;
FILE *fh1, *fh2, *fh3=stdout;
FILE *fh4=stdout;
if( argc<3 ) {
printf("usage: diff <file> <file> { <outfile> }\n"); return(1);
}
if(argi<argc) {
if(!(fh1 = fopen(argv[argi], "r"))) {
printf("cannot open %s\n",argv[argi]); return(2);
}
}
if(++argi<argc) {
if(!(fh2 = fopen(argv[argi], "r"))) {
printf("cannot open %s\n",argv[argi]); return(3);
}
}
if(++argi<argc) {
if(!(fh3 = fopen(argv[argi], "w+"))) {
printf("cannot open %s\n",argv[argi]); return(4);
}
}
if(++argi<argc) {
//duplicate output to a second file?
if(!(fh4 = fopen(argv[argi], "r"))) {
printf("cannot open %s\n",argv[argi]); return(3);
}
}
for(offset = 0; (!feof(fh1)) && (!feof(fh2)); offset++)
{
ch1=ch2='-';
if(!feof(fh1)) ch1 = getc(fh1);
if(!feof(fh2)) ch2 = getc(fh2);
if(ch1 != ch2) {
fprintf(fh3,"%d:%c %c\n", offset, ch1, ch2);
//additional file here
}
else {
fprintf(fh3,"%c\n", ch1);
//additional file here
}
}
return 0;
}
More typically, you would read entire lines using fgets, and strcmp to compare the lines. Here is how,
char str1[1024], str2[1024];
...
for(offset = 0; (!feof(fh1)) && (!feof(fh2)); offset++)
{
strcpy(str1,"-");strcpy(str2,"-");
if(!feof(fh1)) fgets(str1,sizeof(str1),fh1);
if(!feof(fh2)) fgets(str2,sizeof(str1),fh2);
if(strcmp(str1,str2)!=0)
fprintf(fh3,"%d:%s %s", offset, str1, str2);
else
fprintf(fh3,"%c", str1);
}

Copy file, but error double free or corruption in C

int main(int argc, char **argv)
{
FILE *fe, *fs;
unsigned char buffer[2048];
int bytesreader;
fe = fopen(argv[1], "rb");
fs = fopen(argv[2], "wb");
while((bytesreader = fread(buffer, 1, 2048, fe)))
fwrite(buffer, 1, bytesreader, fs);
fclose(fe);
fclose(fs);
return 0;
}
And my error is...
*** glibc detected *** ./doc2: double free or corruption (top): 0x096b7008 ***
Sorry, now is correct, but i dont undertand. Where is my fail?
You need to check the return values of fopen() for 0.
Calling fclose() on 0 results in the error you are seeing.
Also, check argc for a valid range of arguments.
int main(int argc, char **argv)
{
FILE *fe, *fs;
unsigned char buffer[2048];
int bytesreader;
if(argc != 3) {
fprintf(stderr, "usage: %s file1 file2\n", argv[0]);
exit(1);
}
fe = fopen(argv[1], "rb");
if(fe == 0) {
fprintf(stderr, "%s: Failed to open %s for read\n", argv[0], argv[1]);
exit(1);
}
fs = fopen(argv[2], "wb");
if(fs == 0) {
fprintf(stderr, "%s: Failed to open %s for write\n", argv[0], argv[2]);
exit(1);
}
while((bytesreader = fread(buffer, 1, 2048, fe)))
fwrite(buffer, 1, bytesreader, fs);
fclose(fe);
fclose(fs);
return 0;
}

Resources