encoding scheme

This is a discussion on encoding scheme within the C Programming forums, part of the General Programming Boards category; I'm trying to encode a file that is suppose to be read from stdin and a code file that has ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    9

    encoding scheme

    I'm trying to encode a file that is suppose to be read from stdin and a code file that has char value and it's corresponding binary encoding. I was wondering how would I read the data in and store it into a char array and then compare it to the huffman code?

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    how would I read the data in and store it into a char array
    getc()? Or maybe fgets()?

    Create a decoding table from the chars in the code file and look up the enciphered equivalent for each character you read from stdin.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    I have the following:

    Code:
    #include <iostream>
    #include <stdio.h>
    #include <ctype.h>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        int c;
        c = getchar();
    
        putchar(c);
        printf("\n");
    }
    This only prints out 1 character to stdout, I'm guessing I'll need to do while( c != EOF), but when I do that, I cause an infinite loop on just printing the first character. Any suggestion on fixing this problem and storing each character into a char array and once it hits EOF it will stop and perhaps print out the char array to verify everything was read in correctly. Thanks.

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    This is very simple. You should be able to find something in the tutorials.
    Code:
    int c;  /* must be an int to hold EOF */
    do {
        c = getchar();
        if(c == EOF) break;
        putchar(c);
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Any suggestion on fixing this problem and storing each character into a char array and once it hits EOF it will stop
    For this, use fgets(), my other suggestion.
    Code:
    #include <string.h>
    
    char s[SOME_SIZE], *p;
    
    fgets(s, sizeof(s), stdin);
    if((p = strchr(s))) *p = 0;
    
    puts(s);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Code:
    #include <string.h>
    #include <iostream>
    
    int main()
    {
        char s[256], *p;
    
        fgets(s, sizeof(s), stdin);
        if((p = strchr(s,'\0'))) *p = 0;
    
        puts(s);
    
        return 0;
    }
    complains about strchr, says too few args.
    Last edited by blazer26; 05-02-2006 at 02:45 PM.

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    strchr finds a specific character in a string. What character are you trying to find?


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

  8. #8
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    Well I would like it to only terminate if it hits the EOF, else continue reading each character from stdin, even if they're spaces or n.l. and store them in a char array. In the previous post, dwks suggested to use this segment of code.

    [edit]

    I guess it would stop if the '\0' character is seen and then just print?

    Code:
    #include <string.h>
    #include <iostream>
    
    int main()
    {
        char s[256], *p;
    
        fgets(s, sizeof(s), stdin);
        if((p = strchr(s,'\0'))) *p = 0;
    
        puts(s);
    
        return 0;
    }
    Problem with this, it does not capture everything from stdin, just the first line.

    if I try to cat input | ./a.out

    it woudl only print out the first line of input.
    Last edited by blazer26; 05-02-2006 at 02:58 PM.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You still aren't doing something right. You're using strchr to search for the null character ... and then you replace it with a null character. (Well not exactly, but close. A decimal zero.) Why exactly?


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

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    9
    I thought it would terminate if it sees that character. What should be the second parameter of strch() ?


    [EDIT]

    strch searches for a specific character, I"m trying to read in the entire file and storing it in char array so I don't think strch would even work for me.
    Last edited by blazer26; 05-02-2006 at 06:46 PM.

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It should be whatever character you're trying to find. Why exactly are you using strchr? Do you know?


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

  12. #12
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,164
    Hint. Try this program to see why strchr() is handy after fgets() and to see which character you should be searching for:
    Code:
    #include <stdio.h>
    
    int main(void)
    {
      char buf[10], *p;
    
      printf("Type something: ");
      fflush(stdout);
    
      fgets(buf, sizeof(buf), stdin);
    
      // Print the ASCII value before each character entered
      for(p = buf;*p;++p)
        printf("(%d) %c\n", *p, *p);
    
      return 0;
    }
    Try entering something less than 9 characters long. Notice anything odd about the last character outputted?

    Now try it again, but enter something longer than 8 characters.

    Consult an ASCII table if you have to.

    Disclaimer: The preceding program and explanation assumes you're in an ASCII environment, which you probably are.
    Last edited by itsme86; 05-02-2006 at 11:52 PM.
    If you understand what you're doing, you're not learning anything.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Audio encoding (where to start)
    By cs_student in forum C++ Programming
    Replies: 4
    Last Post: 08-04-2008, 01:23 PM
  2. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM
  3. fopen and encoding of "*filename" argument.
    By techi_talk in forum C Programming
    Replies: 4
    Last Post: 05-16-2006, 11:36 PM
  4. Post your IDE scheme!
    By nickname_changed in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-09-2005, 05:47 AM
  5. Caesar's encoding problem
    By dionys in forum C Programming
    Replies: 10
    Last Post: 11-17-2004, 10:26 PM

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