4 Replies - 1262 Views - Last Post: 21 October 2010 - 09:11 PM Rate Topic: -----

#1 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Reading Strings from Binary File

Posted 21 October 2010 - 06:53 PM

Alright, so we were assigned to create a small program in C that would read from a file and if it found a sequence of 4 or more printable characters (he gave us the designated range), it would print that string. The string can be any size so I decided to play with the FSEEK method to make sure that it could accomodate any size without having to make a buffer the size of the file.

here is my code:
#include <stdio.h>

int main(int argc, char* argv[]){
	char *c;
	int fSeekNum = 0;
	int printNum = 0;
        FILE *sfile = fopen(argv[1], "rb");

	/* Checks for null file */
	if(sfile == NULL){
		printf("The file you input was not in the working directory or was not found");
	return 0;
	fseek (sfile , 0 , SEEK_SET);
	}
	while(!feof(sfile)){
	    fread(&c, 1, 1, sfile);
	    if(*c >= 32 && *c <= 126){
		printNum++;
	    }
	    else if(printNum >= 3){
	       fSeekNum = printNum+1;
	       fSeekNum = 0 - fSeekNum;
	       fseek(sfile,fSeekNum , SEEK_CUR); 
	      while(printNum >= 0){
	          fread(&c,1,1,sfile);
	          printf("%c", *c);
	          printNum--;
		}
	printf("\n");
	}
	else{
	    printNum = 0;
	}
	}
	fclose(sfile);
 return 0;   
}



In my mind, this code logic works. But not when I look at the giberish output and compare it to what strings on unix does.

Is This A Good Question/Topic? 0
  • +

Replies To: Reading Strings from Binary File

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Reading Strings from Binary File

Posted 21 October 2010 - 07:17 PM

*Warning* We are currently having trouble reading minds. Until we work out all the bugs, it would be really helpful if you post your code and explain the problem you are having thoroughly. Sorry for the inconvience.

Please supply us a short test file to run your program against.
Please also supply the output you expect for that test file and the output you actually get.
Was This Post Helpful? 0
  • +
  • -

#3 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1831
  • View blog
  • Posts: 4,927
  • Joined: 27-December 05

Re: Reading Strings from Binary File

Posted 21 October 2010 - 07:27 PM

First, you want line 16 to be
deleted erroneous suggestion


since c is already a pointer.


Edit: I take that back. You haven't provided any buffer at all. Instead, leave line 16 as it is and change line 4 to
char c;




Next I think you can replace lines 21 - 22 with
fseekNum = 0-printNum;


else if the file starts off with a char string, you will set the file pointer back to -1. Think it through -- what if you have a 3-char string at the beginning of the file? If the 4th char is not printable, printNum==3 and filepos is 3. You don't want to set filepos to -1.

and replace line 24 with
while(printNum > 0){


or you are printing one char more than the value of printNum (the number of chars in the string).

I haven't tested this, but I'm pretty sure about it.

This post has been edited by r.stiltskin: 21 October 2010 - 07:37 PM

Was This Post Helpful? 1
  • +
  • -

#4 Ember  Icon User is offline

  • D.I.C Head

Reputation: 70
  • View blog
  • Posts: 160
  • Joined: 24-April 10

Re: Reading Strings from Binary File

Posted 21 October 2010 - 08:43 PM

@r.stiltskin: I got it working after doing 2 things using kind of your logic. I just had to think about how the SEEK works.

So if I had a word "Car" at the beginning, and ran across a symbol that wasn't compatible (let's say it is a ~)
so:
1 2 3 4 5
C A R ~ p

p is where the SEEK is pointing at when I start the 2nd while loop, so I would have to jump back +2 more than printNum.
After printing, I returned the seek back to the original position thought it is slightly unnecessary since we already know ~ is a non-character.

@janotte: I am usually the one helping and it is hard when there is little information so sorry for being vague. I shoulda said that this can be used on any binary file so I didn't include any test file since it works on any generic binary file, and in linux that is just a text file(it will display something resembling the original text in windows).

This post has been edited by Ember: 21 October 2010 - 08:44 PM

Was This Post Helpful? 0
  • +
  • -

#5 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Reading Strings from Binary File

Posted 21 October 2010 - 09:11 PM

Yes, sorry, my evil twin couldn't resist.

Is all good now after the actually useful contribution from r.stiltskin.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1