Thread: File-reading problems

  1. #1
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    File-reading problems

    In my code, I have it to read the file into int c untill EOF (-1). This works perfect for text files, but when I try to read other files (say, mp3 or doc) it stops way short, at around 59 b to 2 kb. What's the problem, and how can I solve it?

  2. #2
    Registered User
    Join Date
    May 2002
    Posts
    132
    How about posting some code so we can see what your problem is? I can't even imagine what your problem could be. Well actually I could, but it could be millions of little errors that your missing.

    Also include OS and compiler, and maybe anything else that might help us help you.

    Tyouk

  3. #3
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Thats really hard to answer without seeing how you are reading in the data.

    With the data files did you open them as binary or normal?

  4. #4
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218

    here's some

    Code:
    while(c != EOF)
    {
    	c = fgetc(pla);
    	if(c == EOF)
    	{
    		break;
    	}
    	c += key[i];
    	fputc(c,cip);
    	i++;
    	if(i > strlen(key))
    	{
    		i = 0;
    	}
    	c = 0;
    }
    The code is an encrypting program: this is from the encrypt function. It's very easy to understand.

  5. #5
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Compiler: DMC (Digial Mars C)
    OS: Windows (Longhorn build 4051) [I know it's not an OS problem cuz I've tried it on others]

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Are you opening in with "rb" or just "r"?

  7. #7
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    "r" (to read) and "w" to write.

  8. #8
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    it'd be easier for me to read on byte at a time, instead of a block :/

  9. #9
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok now let me ask you this.. is a MP3 file a text file? If not why are you treating it as such?

  10. #10
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    I type in the filename ("ACDC.mp3" or "whatever.doc"), and have the program try to encrypt it. Problem is, the encrypted file is only a few bytes or a couple kb big, which is way to small.

  11. #11
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    the file is a REAL mp3 file, with real Audio on it, not a text file with a .mp3 extension.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Exactly. Try opening as a binary file and try it.

  13. #13
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    but, then wouldn't c = 1 or 0? couldn't that screw up the encryption?

  14. #14
    Registered User kinghajj's Avatar
    Join Date
    Jun 2003
    Posts
    218
    Nvm, it works just fine: the mp3 plays find after decryption now.

    Thanks again, thantos!

  15. #15
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Glad it worked.

    Yeah the binary file thing is a little confusing at first
    Luckily the functions deal with the hard stuff.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  3. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. I have some problems reading struc from file
    By samc2004 in forum C Programming
    Replies: 4
    Last Post: 12-18-2003, 02:47 PM