problem with fread and fwrite? bug?

This is a discussion on problem with fread and fwrite? bug? within the C Programming forums, part of the General Programming Boards category; what's wrong with this? it's supposed to read 50k bytes from a file, add or substract some values, and write ...

  1. #1
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46

    Unhappy problem with fread and fwrite? bug?

    what's wrong with this?
    it's supposed to read 50k bytes from a file, add or substract some values, and write them in another file (in binary)

    Code:
    unsigned char t[STR_MAX];
     int i=0, offset=0, l=strlen(key);
     size_t n;
     do {
      n=fread(t,sizeof(t[0]),STR_MAX,fi);
      for (;i<n;i++){
       int y=(i+offset)%l;
       if (com=='U') t[i] += key[y]; else t[i] -= key[y];
       offset += n%l;
       }
      fwrite(t,sizeof(t[0]),n,fo);
     } while (n>0);
    STR_MAX is 51200
    key is a char *
    fi,fo are FILE *
    com is 'U' or 'D'
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  2. #2
    Registered User
    Join Date
    Jul 2005
    Posts
    69
    unsigned char t[51200];
    is probably exceeding your stack space, better to set it up and call malloc for that amount of space. I didn't look much at the rest so some problems might remain.

  3. #3
    Software Developer jverkoey's Avatar
    Join Date
    Feb 2003
    Location
    University of Waterloo
    Posts
    1,904
    Quote Originally Posted by moonlord
    what's wrong with this?
    Please clarify what the problem is.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Well the fact that i=0 happens only once seems to be a problem to me.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    oh, i fixed it.

    now that you say, the fact that i is initiated only once seem to be bogus, but it isn't (!!??)

    the problem was that int had a problem when getting over 2^15-1 (that is 32xxx) so i changed it to unsigned int

    sorry for the late post, but i was out of town this time.
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    Try it with a large file then
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    it worked for a 1.3 mb file. i mean, you realise that if you do it once with U and then with D the output is identical with the input. i checked it with a hexedit
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    So did you print out the value of i when processing that large file?

    As far as I can tell, you only encrypt and decrypt the first STR_MAX bytes of the file, after that the for (;i<n;i++) has no effect because you've already incremented i up to STR_MAX. So from that point on the for loop ceases to have any effect.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And to prove the point, we use a pair of simple loops:
    Code:
    #include<stdio.h>
    int main( void )
    {
        int outer, inner = 0;
    	for( outer = 0; outer < 10; outer++ )
    	{
    	    printf("Outer: %d.\n", outer );
    	    for( ; inner < 10; inner++ )
    		    printf("\tInner is %d.\n", inner );
    	}
    	return 0;
    }
    
    /*
    gcc -o onetoten onetoten.c -Wall -pedantic -ansi
    ./onetoten
    
    Outer: 0.
    	Inner is 0.
    	Inner is 1.
    	Inner is 2.
    	Inner is 3.
    	Inner is 4.
    	Inner is 5.
    	Inner is 6.
    	Inner is 7.
    	Inner is 8.
    	Inner is 9.
    Outer: 1.
    Outer: 2.
    Outer: 3.
    Outer: 4.
    Outer: 5.
    Outer: 6.
    Outer: 7.
    Outer: 8.
    Outer: 9.
    */

    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    wonder i didn't see that. ask for a problem and have the solution for another

    like in the ol' drinkin' song:
    "100 bugs in code,
    100 bugs in code,
    Fix a bug, compile again,
    101 bugs in code..."

    10x for pointing that out
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,821
    > wonder i didn't see that. ask for a problem and have the solution for another
    Yep, that's programming for you.

    You can do all the testing you can think of, but there's nothing like a 2nd opinion which asks "what about...." to give you something else to think about.

    My quote
    "Testing is like pouring cold water into a chocolate teapot and saying that it works."
    There's always that one extra condition which you might not have thought of which will reduce your program to a gooey mess
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    oh, by the way:

    Code:
    char process_com(void) {
    	char *i=com;
    	switch(com[0]) {
    		case 'a', 'b', 'c': {intr=com[0];break;}
    		default: return(1);
    		}
    	while (*++i) {
    		switch(*i) {
    			case '-': {dash=1;break;}
    			case 'l': {bign=1;break;}
    			case 'i': {ign=1;break;}
    			default: return(1);
    			}
    		}
    	return(0);
    }
    com is a char *; intr,dash,bign,ign are chars
    this function is supposed to check a string if is of the form:

    com[0] is 'a' or 'b' or 'c';
    there might be -, i or l afterwards, or more of them (no need to check if one of them appears more than once)

    problem is it always return 1. i think i'm messing something with the pointer...

    (note that l is tolower('L'), and not toupper('i'))

    examples of 'good' strings: "a-il", "clll-ii-li-il-il", "b", "c-", "bi", "al-" etc.
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    case 'a', 'b', 'c':
    This doesn't do what you think it does. As a matter of fact, it doesn't even compile.


    Quzah.
    Hope is the first step on the road to disappointment.

  14. #14
    EOF
    Join Date
    Aug 2005
    Location
    Constanta, RO, Europe
    Posts
    46
    i don't mind that my compiler does make it, but will you tell me what's it doing and how's right? mistifing doesn't help much...
    Two strings walk into a bar. The first one says, 'Bartender! Bartender! I want a drink!'. The second one says, 'Bartender! Bartender! I want a drink too! Blaaaaaaaaah eeeeeeeek yaaaaaaak oooooooh'. The first one says, 'Please excuse my friend. He isn't null term--'.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You should try compiling with warnings on then. It shouldn't compile. At any rate, this is what you want:
    Code:
    case 'a': case 'b': case 'c':
    You have to have the case section before each actual case, and the appropriate : after it. Instead of:
    Code:
    case 'a', 'b', 'c':
    What you're really doing is having case fall through, where because you don't break out of one case, it drops through to the next. You drop through all the possible valid selections in this case, so they all fall into the common case block. Then you break from it.

    But you still have to form them all as valid cases.


    Quzah.
    Last edited by quzah; 08-21-2005 at 09:44 AM.
    Hope is the first step on the road to disappointment.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 2d array and fwrite and fread
    By totalnewbie in forum C Programming
    Replies: 14
    Last Post: 01-10-2009, 03:45 PM
  2. Weird problem with fwrite() and fread()
    By piote in forum C Programming
    Replies: 2
    Last Post: 11-13-2004, 03:07 PM
  3. buffer type for fread & fwrite
    By daluu in forum C Programming
    Replies: 5
    Last Post: 05-08-2003, 07:57 PM
  4. fwrite - fread
    By PutoAmo in forum C Programming
    Replies: 2
    Last Post: 03-10-2002, 01:48 PM
  5. fread() and fwrite() ?
    By Limblet in forum C Programming
    Replies: 4
    Last Post: 09-25-2001, 08:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21