Thread: Chopping up a string

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    13

    Chopping up a string

    I'm a newbie, so please be nice to me ;-)

    I've been programming in C for about a month now, but I haven't really done alot. I'm trying to write one of those simple XOR encryption programs.

    I want to open a text file, read form it charcter by charcter
    encrypt each line
    write the number to a a file

    then

    read file line by line
    decrypt each line
    write decrypted file

    I have no problems encrypting the file, it is decrypting thats my problem. I'm using spaces to separate each charcater, and I have no clue how to make the string of numbers into intergers.

    I hope thats clear enough... If you have any questions let me know. Thank you!

    Here is a piece of the code...
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include "encfunc.h"
    #include "fexist.h"
    
    void enc_file(void)
    {
      char buffer[256];
      int key = 99;
      int count = 0;
      int enc = 0;
      FILE *in, *out;
    
      if ((in = fopen("in.txt", "r")) == NULL)
        {
          fprintf(stderr, "Error opening file!");
          exit(EXIT_FAILURE);
        }
    
      if ((out = fopen("out.txt", "w")) == NULL)
        {
          fprintf(stderr, "Error opening file!");
          exit(EXIT_FAILURE);
        }
    
      while (1) {
        enc = fgetc(in);
    
          if (!feof(in))
    	{
    	  enc = enc ^ key;
    	  fprintf(out, "%d ", enc);
    	}
          else
    	break;
      }
    
      fclose(in);
      fclose(out);
    }
    
    void denc_file(void)
    {
      char buffer[256];
      int key = 99;
      int tmp = 0;
      int enc = 0;
      int len = 0;
      FILE *fp;
    
      if ((fp = fopen("test.txt", "r")) == NULL)
        {
          fprintf(stderr, "Error opening file!");
          exit(EXIT_FAILURE);
        }
    
      fclose(fp);
    }
    #define punkCow

  2. #2
    King of the Internet Fahrenheit's Avatar
    Join Date
    Oct 2001
    Posts
    128
    > and I have no clue how to make the string of numbers into intergers.

    atoi

    EDIT: You'll probably want to get each individual number with strtok

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    If this is encode
    enc = enc ^ key;
    fprintf(out, "%d ", enc);


    This would be decode
    fscanf(in, "%d ", &enc);
    enc = enc ^ key;
    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.

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    13

    Talking Thanks

    Thank you so much, Salem
    I was totatly clueless. I wish I had some friends that knew C. I'm 15 y/o, and none of my freinds are into programming.
    #define punkCow

  5. #5
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    I wish I had some friends that knew C. I'm 15 y/o, and none of my freinds are into programming.

    Well now you do. You've got us and we are all into programming.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    13
    Originally posted by Bubba
    Well now you do. You've got us and we are all into programming.
    Awww, you guys are great. I've always been impressed at how people on message boards are so willing to help newbies. I honestly am very thankfil for people who are willing to help newbies with the stupidest problems

    Enough of my mushy stuff Thanks everyone!
    #define punkCow

  7. #7
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    Originally posted by Salem
    If this is encode
    enc = enc ^ key;
    fprintf(out, "%d ", enc);


    This would be decode
    fscanf(in, "%d ", &enc);
    enc = enc ^ key;

    ...and don't forget that sometimes enc == key...
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  3. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 11:06 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM