Thread: Comparing and integer variable with a char variable.

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    8

    Comparing and integer variable with a char variable.

    Hello People! I hope everything's well...
    Im a noob coder and I have the next question,

    Here's my code, it copies character by character to another file.
    Code:
    #include <stdio.h>int main (int argc, char *argv[])
    {
    
    
      char caracter;
    char encrypt;
      char *archivaldo[20];
      int cont;
      FILE *fp;
      FILE *fp2;
    
    
    
    
      if((fp=fopen(argv[1], "r"))==NULL)
        {
          printf("no se puede abrir");
        }
      else
        {
          printf("si se pudo abrir");
          fp2=fopen(argv[2], "w");
        }
    
    
      while((caracter=getc(fp))!=EOF)
        {
    
    
    ////I want to take the caracter's ASCII value and save it to another variable, how can I do that?////
          putc(caracter, fp2);
        }
    
    
      fclose(fp);
      fclose(fp2);
      
    }
    }

    How In earth can I take the ASCII value of the 'caracter' variable and save it on another variable, let me remind you that the 'caracter' is always changing, you never know which letter its going to get.

    If you need any more info, Im glad to give it.

    Thank you for helping me out & taking the time to read.

    ****Btw(on a further note) I want to print on the second file as backwards, for example you read "Hello" I want it to be printed on another file as "olleH" I know you have to use LIFO structures, I know this may not be the place but can someone explain how to use this kind of structure?

    ///////////EDITTTTT////////
    Ok, So, let me explain myself.


    *Im reading the file character by character because I want to encrypt it using the ASCII values.


    for example I've read a character 'R' which I believe is 82

    I want ascii_variable=82///Its not going to be 'R' always, depends on the file.

    How can I save the ascii value on to the variable ascii_variable...how is this possible?

    //////////More EDITTTT////////////////////////////


    Also, I want to take that ascii value and find the character that it is.

    For example

    ascii_variable=82

    character_variable = ascii_variable interpretation (in this case 'R')

    How In earth can I do this!!!!
    Last edited by XIIIX; 09-19-2011 at 12:22 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by XIIIX
    How In earth can I take the ASCII value of the 'caracter' variable and save it on another variable
    Just assign to the other variable. The value is the value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by XIIIX View Post
    How In earth can I take the ASCII value of the 'caracter' variable and save it on another variable
    In my experience the equals sign has always done a fine job of this task... junk = caracter; ... should be just the ticket.
    However if you are reading and writing the file character by character (which is painfully slow) you can do this...

    Code:
    fputc(fgetc(fp),fp2);

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    First:
    • You said main would return an int, so do so. &nbsp;Put a return 0; at the bottom of main for success.
    • You never check if fp2 was successfully opened.
    • If opening either the input file or the output file fails, you need to abort, since you can't read from or write to an unopened file.
    • Look at the specification/documentation for getc. &nbsp;It returns an int. &nbsp;You need to change the type of your "caracter" variable to int.

    A couple of those errors would be apparent if you compiled your code with warnings turned all the way up.

    An "ASCII value" is just a number from 0 to 127. Each of those numeric values has a special meaning. &nbsp;The value 65 is the letter 'A', the value 32 is ' ' (space). The different characters you see (like 'A', 'x', '?') are just little pictures associated with each numeric value called "glyphs". Google "ASCII", and you should find plenty of info. When you read from a file using getc, the numeric value is returned, not the glyph. Your code properly writes that character to fp2 using putc(). If you want to "save it on another variable", you just use the assignment operator, =, like so: caracter1 = caracter2;

    As for your LIFO structure, what you're looking for is what's called a "stack". We have a tutorial here, but it's C++. Google should turn up plenty of info on this as well.
    Last edited by anduril462; 09-19-2011 at 10:26 AM.

  5. #5
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Ok, So, let me explain myself.


    *Im reading the file character by character because I want to encrypt it using the ASCII values.


    for example I've read a character 'R' which I believe is 82

    I want ascii_variable=82///Its not going to be 'R' always, depends on the file.

    How can I save the ascii value on to the variable ascii_variable...how is this possible?

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    8
    Ok, new doubt, the original question is edited.
    Last edited by XIIIX; 09-19-2011 at 12:30 PM.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    'R' and 82 are the same thing in ASCII. They're just different representations of the same value. A char type in C is just a small integer, limited to one byte. ASCII is just a mapping of 7-bit values to different glyphs (and a few control code, but those aren't the issue here). Try this program and see if it helps you understand:
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
        char c1 = 'R';  // set c1 to the value associated with upper case R
        char c2 = 82;  // set c2 to 82
    
        // the %d prints the numeric representation of c1/c2, and the %c prints the character representation or glyph    printf("c1 = %d, which is represents the ASCII character'%c'\n", c1, c1);
        printf("c2 = %d, which is represents the ASCII character'%c'\n", c2, c2);
        return 0;
    }

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by XIIIX View Post
    Ok, So, let me explain myself.


    *Im reading the file character by character because I want to encrypt it using the ASCII values.


    for example I've read a character 'R' which I believe is 82

    I want ascii_variable=82///Its not going to be 'R' always, depends on the file.

    How can I save the ascii value on to the variable ascii_variable...how is this possible?
    You do it with the = sign... really...
    Code:
    ascii_variable = caracter;
    There's no magic here... the letter 'R' is just a number...

    Try this, you'll see...
    Code:
    printf("%d", 'R');
    A character is just a short short int... one byte in length.
    The character R is just a number representing the letter R in the ascii table.

    It's all just numbers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 09-04-2011, 09:29 PM
  2. What does it mean when a integer variable has a word value
    By Nathan the noob in forum C++ Programming
    Replies: 2
    Last Post: 06-26-2008, 02:44 AM
  3. Comparing integer char[] elements
    By ccflyer in forum C Programming
    Replies: 7
    Last Post: 04-20-2008, 03:40 PM
  4. How many bits are '1' in an integer variable?
    By cuthbert in forum C Programming
    Replies: 51
    Last Post: 09-13-2006, 04:14 PM
  5. How many bits are '1' in an integer variable?
    By cuthbert in forum C++ Programming
    Replies: 0
    Last Post: 09-12-2006, 01:08 PM

Tags for this Thread