Another set of eyes


 
Thread Tools Search this Thread
Top Forums Programming Another set of eyes
# 1  
Old 07-31-2008
Another set of eyes

Original code used fwrite instead of putc.
What is expected is that the destination file will be written to. Instead I end up with a zero length file. I'm sure there is something simple I'm missing.
tia.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(int argc, char **argv) {
int x = 0, freq = 0, cnt = 0;
FILE *fpt, *wpt;
char comment_block[2];
char bin;

                                
                                if (argc != 5) {printf("Error: Please provide\n 1.) Source filename\n 2.) Destination filename\n 3.) Frequency for commitment\n 4.) Message to embed\n as arguments to %s.\n",argv[0]); exit(0);}
                                if ( (fpt = fopen(argv[1],"r")) == NULL || (wpt = fopen(argv[2],"a+")) == NULL) {printf("Error opening source or destination files.\n"); exit(0);}
                                freq = atoi(argv[3]);
                                
                                while (fread(&comment_block,sizeof(comment_block),2,fpt) > 0) {
                                      printf("At byte %d.\n",(x += 2));
                                      if ((comment_block[0] & 0xFF) && (comment_block[1] & 0xD8)) {printf("Start of image found due to comment_block[0]: %p = %x and comment_block[1]: %p = %x\n",&comment_block[0],comment_block[0],&comment_block[1], comment_block[1]); x = 0; putc(comment_block[0],wpt); putc(comment_block[1],wpt); break;}
                                      putc(comment_block[0],wpt); putc(comment_block[1],wpt);
                                }
                                
 
                                while (fread(&bin,sizeof(bin),1,fpt) > 0) {
                                      printf("Integer value for %p at character count %d = %x.\n",&bin,(x++),bin);
                                      if (x > 1 && x % freq  == 0 && cnt <= strlen(argv[4])) {bin = argv[4][cnt++]; printf("At character count: %d replacing to %x.\n",x,bin);}
                                      putc(bin,wpt);
                               }
                               fclose(fpt); fclose(wpt);
                               return 0;
}

# 2  
Old 07-31-2008
Question source file

Is there something in the source file or is it empty.
# 3  
Old 07-31-2008
Also - ran your code on a binary file - generated output. I agree with Shamrock.
# 4  
Old 07-31-2008
Please make sure the disk have space and you have the permission to write on the disk.
# 5  
Old 08-01-2008
Bizarre. This is definitely a single machine malf; nothing so obvious as diskspace or permissions. I'll have to do more work to find out what is wrong. Thanks for the feedback.
# 6  
Old 08-03-2008
I found an error on your code, not sure if it is the only one.

Code:
while (fread(&comment_block,sizeof(comment_block),2,fpt) > 0) {

from the manual:

Code:
The function fread() reads nmemb elements of data, each size bytes long, from the stream pointed to by stream, storing them at the location given by ptr.

So what you're doing is reading 2 elements of data from fpt, BOTH with size sizeof(comment_block) thus overflowing your buffer.
What you should be doing is,
Code:
fread(&comment_block, sizeof(comment_block)/2, 2, fpt) > 0)

# 7  
Old 08-05-2008
That's right. The correct fix is just to use sizeof(char).
Login or Register to Ask a Question

Previous Thread | Next Thread

5 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help needed with shell script to search and replace a set of strings among the set of files

Hi, I am looking for a shell script which serves the below purpose. Please find below the algorithm for the same and any help on this would be highly appreciated. 1)set of strings need to be replaced among set of files(directory may contain different types of files) 2)It should search for... (10 Replies)
Discussion started by: Amulya
10 Replies

2. Shell Programming and Scripting

Would appreciate a quick second set of eyes on a script (regarding doing things in the background)

What I'm trying to do is leave a tcpdump running all the time on a server, and at the end of every day kill it and start a new one. For some reason my boss doesn't want to set this up as a cron job, so I've come up with the following.: #!/bin/bash PCAPFILE=/tmp/mgmt.$(date... (8 Replies)
Discussion started by: DeCoTwc
8 Replies

3. Shell Programming and Scripting

Broke Perl Script Second pair of eyes NET::FTPSSL

Hi all, Let me first start out by saying I'm a perl newbie and hope somebody can help, for the life of me I can't figure out why my script will not find and download a remote file via FTPSSL. What it's supposed to do is find the latest file named... (4 Replies)
Discussion started by: Styles
4 Replies

4. What is on Your Mind?

use ears to protect eyes before the computer.

If you listen to the files on the computer with ears, your eyes are protected. Microsoft Text-To-Speech(TTS) voice engine has been installed by default on Windows 2000, XP, Vista, you can find what voice engines have been installed on the computer by following Control Panel->Speech->Speech... (0 Replies)
Discussion started by: tgst
0 Replies

5. Shell Programming and Scripting

Need some help with this script -- extra eyes

I have two issues with this script. 1. I cannot seem to get my counters to count correctly. 2. My function to eject to CAP1 or CAP2 is hung in a loop and doens't exit back to the previous function. I would like to be able to select which cap to eject to . Each cap holds only 40 tapes, so when one... (15 Replies)
Discussion started by: gzs553
15 Replies
Login or Register to Ask a Question