The program is done in C. This program opens a file containing binary or text and reads every byte in the file and writes both the ASCII hex value for that byte as well as it’s printable (human-readable) character (characters, digits, symbols) to standard output. The issue I am having is when the file has multiple lines being read. The first line is read and done properly but the other lines do not work correctly. For instance, the text file will contain this:
.......1.unsigne
...unsigned char
------------------------
and results in: 0000000: 2e2e 2e2e 2e2e 2e31 2e75 6e73 6967 6e65 .......1.unsigne
0000010: 0a00 0000 0000 0000 0000 0000 0000 0000 ................
but should result in: 0000000: 2e2e 2e2e 2e2e 2e31 2e75 6e73 6967 6e65 .......1.unsigne
0000010: 2e2e 2e75 6e73 6967 6e65 6420 6368 6172 ...unsigned char
Any ideas on how to fix this?
--------------------------------------------------------------------
CODE
--------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 1000
void hexDump (char *desc, void *addr, int len) {
int i;
unsigned char buffline[17];
unsigned char *pc = (unsigned char*)addr;
if(desc != NULL){
printf("%s: \n", desc);
}
for (i = 0; i < len; i++){
if ((i % 16) == 0){
if (i != 0)
printf(" %s\n", buffline);
if(pc[i] == 0x00) exit (0);
printf(" %07x: ", i); //prints address
}
printf ("%02x", pc[i]);
if ((i % 2) == 1)
printf(" ");
if ((pc[i] < 0x20) || (pc[i] > 0x7e)){ //non-printable
chars
buffline[i % 16] = '.';
}
else{
buffline[i % 16] = pc[i];
}
buffline[(i % 16) + 1] = '\0'; //clear array
}
while ((i % 16) != 0) { //'.' fill last line
printf (" ");
i++;
}
printf (" %s\n", buffline);
}
int main (int argc, char *argv[]) {
FILE *fp;
char buff[SIZE];
fp = fopen(argv[1], "r");
memset(buff, '\0', sizeof(buff));
fgets(buff, SIZE, fp);
hexDump("buff", &buff, sizeof(buff));
fclose(fp);
return 0;
}
Note: Main problem was you were reading only one line and passing it to the hexDump function.
So I have added a loop. Now its reading both the lines. I have solved your main problem.
Now, There is need to modify your function because you have used a logic that you finds best,thats why I am not modifying the code otherwise whole program has to be modified.
Remember second line consists of non printable characters so modify accordingly.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 1000
void hexDump(void * addr, int len) { // no need to pass
desc message
int i;
unsigned char buffline[17];
unsigned char * pc = (unsigned char * ) addr;
/*if (desc != NULL) {
printf("%s: \n", desc); //removing this message you should add this
is main
}*/
for (i = 0; i < len; i++) {
if ((i % 16) == 0) {
if (i != 0)
printf(" %s\n", buffline);
if (pc[i] == 0x00) {
return; //exit will end the program and you cant modify
next line
}
printf(" %07x: ", i); //prints address
}
printf("%02x", pc[i]);
if ((i % 2) == 1)
printf(" ");
if ((pc[i] < 0x20) || (pc[i] > 0x7e)) { //non-printable
chars
buffline[i % 16] = '_'; //changed to _ so that you
can identity whether it is . or non printable char
} else {
buffline[i % 16] = pc[i];
}
buffline[(i % 16) + 1] = '\0'; //clear array
}
while ((i % 16) != 0) { //'.' fill last line
printf(" ");
i++;
}
printf(" %s\n", buffline);
}
int main(int argc, char * argv[]) {
FILE * fp;
char buff[SIZE];
int count=1;
fp = fopen("c:\\users\\faraz\\desktop\\a.txt", "r");
memset(buff, '\0', sizeof(buff));
while(fgets(buff, SIZE, fp)){ // added for reading multiple
lines in a file
hexDump(& buff, sizeof(buff));
}
fclose(fp);
system("PAUSE");
return 0;
}
The program is done in C. This program opens a file containing binary or text and...
This question has been asked before but the responses have been wrong. Please do not copy and paste their answers. Currently the program opens a file and reads every byte in the file and write both the ASCII hex value for that byte as well as it’s printable character to standard output with non-printable characters printing a "." Now, I want have an option where the program prints in binary instead of hex by typing "-b" at the command line....
Convert C to C++ I need these 4 C file code convert to C++. Please Convert it to C++ //////first C file: Wunzip.c #include int main(int argc, char* argv[]) { if(argc ==1){ printf("wunzip: file1 [file2 ...]\n"); return 1; } else{ for(int i =1; i< argc;i++){ int num=-1; int numout=-1; int c; int c1; FILE* file = fopen(argv[i],"rb"); if(file == NULL){ printf("Cannot Open File\n"); return 1; } else{ while(numout != 0){ numout = fread(&num, sizeof(int), 1, file); c...
Here is the (A3b-smallgrades.txt) text file:
Here is the (A3b-largegrades.txt) text file:
Here is the Program A3a without modification:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
void getGrades(int ROWS, int COLS, int grades[ROWS][COLS], char
students[COLS][20]);
void printGrades(int ROWS, int COLS, int
grades[ROWS][COLS]);
void getStudents(int COLS, char students[COLS][20]);
void printStudents(int COLS, char students[COLS][20]);
void calcGrades(int ROWS, int COLS, int grades[ROWS][COLS], char
Fgrades[]);
void printFinalGrades(int COLS, char Fgrades[]);
int main()
{
srand(time(0));
int stu = 0, assign = 0;...
In this exercise we “reverse-engineer” some code to try to
determine how the heap in a C program is managed. Consider the
following C program (compiled as an executable called memory):
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
int chunk_sizes[4];
if ((argc != 2) ||
(sscanf(argv[1], "%d,%d,%d,%d", &chunk_sizes[0], &chunk_sizes[1], &chunk_sizes[2], &chunk_sizes[3]) != 4) ||
(chunk_sizes[0] < 0) || (chunk_sizes[1] < 0) || (chunk_sizes[2] < 0) || (chunk_sizes[3] < 0) ) {
fprintf(stderr,"Usage: %s a,b,c,d\n", argv[0]);
fprintf(stderr," where...
Modify the client server system program given below so that instead of sendto() and recvfrom(), you use connect() and un-addresssed write() and read() calls. //Server.c #include #include #include #include #include #include #include #include #include #include # define PortNo 4567 # define BUFFER 1024 int main(int argc, char ** argv) { int ssd; int n; socklen_t len; char msg[BUFFER]; char clientmsg[BUFFER]; struct sockaddr_in server; struct sockaddr_in client; int max_iterations = 0; int count = 0, totalChar = 0, i = 0;...
PLease explain output of these two programs: 1. #include <stdio.h> typedef struct { char *name; int x, y; int h, w; } box; typedef struct { unsigned int baud : 5; unsigned int div2 : 1; unsigned int use_external_clock : 1; } flags; int main(int argc, char** argv){ printf("The size of box is %d bytes\n", sizeof(box)); printf("The size of flags is %d bytes\n", sizeof(flags)); return 0; } 2. #include <stdio.h> #include <string.h> /* define simple structure */ struct { unsigned...
/* myloggerd.c * Source file for thread-lab * Creates a server to log messages sent from various connections * in real time. * * Student: */ #include <stdio.h> #include <sys/types.h> #include <sys/stat.h> #include <unistd.h> #include <fcntl.h> #include <sys/socket.h> #include <sys/un.h> #include <stdlib.h> #include <pthread.h> #include "message-lib.h" // forward declarations int usage( char name[] ); // a function to be executed by each thread void * recv_log_msgs( void * arg ); // globals int log_fd; // opened by main() but accessible...
Help with my code: The code is suppose to read a text file and when u enter the winning lotto number it suppose to show the winner without the user typing in the other text file please help cause when i enter the number the code just end without displaying the other text file #include <stdio.h> #include <stdlib.h> typedef struct KnightsBallLottoPlayer{ char firstName[20]; char lastName[20]; int numbers[6]; }KBLottoPlayer; int main(){ //Declare Variables FILE *fp; int i,j,n,k; fp = fopen("KnightsBall.in","r"); //...
Assume I don't understand C++ Can someone explain this program to me Line by Line? Basically what each line actually does? whats the function? whats the point? Don't tell me what the program does as a whole, I need to understand what each line does in this program. #include #include #include #include #include #define SERVER_PORT 5432 #define MAX_LINE 256 int main(int argc, char * argv[]) { FILE *fp; struct hostent *hp; struct sockaddr_in sin; char *host;...
Writing a program in C please help!! My file worked fine before but when I put the functions in their own files and made a header file the diplayadj() funtion no longer works properly. It will only print the vertices instead of both the vertices and the adjacent like below. example input form commnd line file: A B B C E X C D A C The directed edges in this example are: A can go to both B and...