Loop count

This is a discussion on Loop count within the C Programming forums, part of the General Programming Boards category; Long time no C. Well, this is a simple program just to translate ASCII codes to simple text. The program ...

  1. #1
    Registered User slash.hack's Avatar
    Join Date
    Sep 2011
    Posts
    21

    Loop count

    Long time no C.
    Well, this is a simple program just to translate ASCII codes to simple text.
    The program ran without errors, well good enough. But I'm amazed to see how many times the loop in it ran.

    Here's the code:
    Code:
    #include<stdio.h>
    
    int main()
    {
        FILE* fptr;
        int n=0;
        char msg[100];
        int spaces=0; //whitespace
        int norm=0; //any other characters
        fptr= fopen("decipher.txt","r");
        fscanf(fptr,"%d",&msg[0]);
        printf("\nThis is what has been said:\n");
        while(msg[n]!=EOF)
        {
         n++;
         if(fscanf(fptr,"%d",&msg[n])==0)
           spaces++;
         else
           norm++;
        }
        printf("%d\n%d\n%d\n",n,spaces,norm);
        puts(msg);
    return 0;
    }
    I used fscanf assuming that it wouldn't read whitespace characters as the message would be in form of:
    75 105 108 108 32 109 101 32 116 101 110 100 101 114 108 121 32 109 121 32 108 111 118 101 46 4
    Even modified the file but it would return the same value for n.
    114 97 110 100
    The spaces and norms wouldn't add up equal to n.

    And as usual please do point out any sort of stupidity

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    > translate ASCII codes to simple text
    What? There isn't any difference:

    Code:
    printf("%c, ", '&');
    printf("%d, ", '&');
    printf("0x%x ", '&');
    Output = &, 38, 0x26

  3. #3
    Registered User slash.hack's Avatar
    Join Date
    Sep 2011
    Posts
    21
    A friend would update her status that way and I can't translate ASCII plainly with my mind lol
    So this is where the program came from. Nothing serious, just for kicks.
    I posted this here because I can't figure out why the loop would run so many times.

  4. #4
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    Well, you could always try something like this then

    Code:
    #include <ctype.h>
    
    unsigned char val;
    while (scanf(" %d", &val)) 
        if (isprint(val))
            printf("%c ", val);
    (and get rid of the file, just paste in)

    So, when you paste "56 57 68 35 67", it prints "8 9 D # C".

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    The %d format in fscanf will fill an int's worth of memory at the given location.
    How about:
    Code:
    fscanf(fptr,"%d", num);  // n is an int
    msg[n] = num;
    And this doesn't make sense
    Code:
    if(fscanf(fptr,"%d",&msg[n])==0)
        spaces++;
    else
        norm++;
    fscanf returns the number of items it reads.
    And reading with %d will skip spaces first.
    It will probably never return 0 in your case, just 1 when it reads a number and EOF (probably -1) when it reaches the end of the file.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    Registered User slash.hack's Avatar
    Join Date
    Sep 2011
    Posts
    21
    memcpy, that's a nice idea and its new for me, will certainly try it.

    Quote Originally Posted by oogabooga View Post
    The %d format in fscanf will fill an int's worth of memory at the given location.
    How about:
    Code:
    fscanf(fptr,"%d", num);  // n is an int
    msg[n] = num;
    I used that but for some reason the program worked weirdly. It just crashed.

    And this doesn't make sense
    Code:
    if(fscanf(fptr,"%d",&msg[n])==0)
        spaces++;
    else
        norm++;
    fscanf returns the number of items it reads.
    And reading with %d will skip spaces first.
    It will probably never return 0 in your case, just 1 when it reads a number and EOF (probably -1) when it reaches the end of the file.
    I used that just for counting the loop.

    I made it echo what it was reading along with the n count and the pointer, it showed something amazing, it seems that it never met the EOF even after it read everything.
    Code:
    while(msg[n]!=EOF)
        {
         n++;
         if(fscanf(fptr,"%d",&msg[n])==0)
           spaces++;
         else
            norm++;
        printf("\n%u %d %d\n",fptr,n,msg[n]);
    
         }
    However, the loop could be forced to be end by adding -1 at the end which I did to test. It showed 25 counts, the exact number of characters (1 counted less obviously). I'm confused how and why it would read things from the file that never existed. Also what causes fscanf to move reading from one point of the file to another because I saw that fptr never changed.

  7. #7
    Registered User slash.hack's Avatar
    Join Date
    Sep 2011
    Posts
    21
    Still confused and sad about the loop not being exact.
    Also if I use the same code in the program this manner, when it decodes the program crashes. I'm clueless. Help please.

    Code:
    #include<stdio.h>
    
    ///GLOBALS
    FILE *fptr;
    char path[80];
    char msg[100];
    char choice;
    int n=0;
    /////////////
    
    void thedoor()
    {
        printf("Incorrect choice.");
    }
    
    void encode()
    {
    
          printf("Write something:\n");
          gets(msg);
          printf("Encoded without failure!\n");
          while(msg[n]!='\0')
                {
                  printf("%d ",msg[n]);
                  n++;
                }
          printf("\nWrite this to file?:");
          choice=getchar();
          getchar();
          if(choice=='Y'||choice=='y')
            {
                printf("Enter filename (full path maybe):");
                gets(path);
                n=0;
                fptr = fopen(path,"w");
                 if(fptr==NULL)
                {
                    printf("Error creating the file.");
                }
                while(msg[n]!='\0')
                {
                  fprintf(fptr,"%d ",msg[n]);
                  n++;
                }
                fprintf(fptr,"%c",'\0');
            fclose(fptr);
            }
           else
            printf("See ya...");
    
    }
    
    void decode()
    {
          printf("\nEnter a file name to decipher(path if required):");
          gets(path);
    
          fptr= fopen(path,"r");
          if(fptr==NULL)
          {
              printf("Error opening the file.");
          }
          else
          {
              fscanf(fptr,"%d",&msg[0]);
              printf("\nThis is what has been said:\n");
              while(msg[n]!=EOF)
              {
                fscanf(fptr,"%d",&msg[n]);
                n++;
              }
              puts(msg);
          }
    }
    
    int main()
    {
    
      printf("Encode or Decode?(E\\D):");
          choice=getchar();
          getchar();
    
          if(choice=='E'||choice=='e')
           encode();
          else if(choice=='D'||choice=='d')
           decode();
          else
           thedoor();
          return 0;
    }

  8. #8
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by slash.hack View Post
    However, the loop could be forced to be end by adding -1 at the end which I did to test. It showed 25 counts, the exact number of characters (1 counted less obviously). I'm confused how and why it would read things from the file that never existed.
    Because there's no way to store EOF in a char your loop condition never exits the while loop, so you're reading beyond the end of a file. Find a different way to handle that issue.

    Also what causes fscanf to move reading from one point of the file to another because I saw that fptr never changed.
    What's pointed to by ftpr will, but the details of how that works are implementation defined so you'll have to trust the compiler. Maybe look into ftell() if you want to make sure that it's correctly moving through the file. But fix the major problem first.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. count bit loop
    By aromash in forum C Programming
    Replies: 2
    Last Post: 03-08-2011, 08:11 AM
  2. While loop to count the charecters of a string
    By simpatico_qa in forum C Programming
    Replies: 11
    Last Post: 04-24-2009, 03:51 AM
  3. Help for some loop count problem..
    By jochen in forum C Programming
    Replies: 3
    Last Post: 11-30-2007, 07:24 AM
  4. Replies: 6
    Last Post: 10-23-2006, 07:22 PM
  5. homework help. cant get for loop to count right
    By bluegoo06 in forum C++ Programming
    Replies: 11
    Last Post: 03-10-2005, 05:05 PM

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