Retrieving info from a file...

This is a discussion on Retrieving info from a file... within the C Programming forums, part of the General Programming Boards category; Hi, I'm making an encryption program, not a very serious one really, but still I'm doing one. I'm having a ...

  1. #1
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,262

    Retrieving info from a file...

    Hi, I'm making an encryption program, not a very serious one really, but still I'm doing one. I'm having a problem though. I ask the user to input a string, and depending on what they want to call the file it is to be saved to, the length of the string it will encrypt it like:



    Code:
    for (int w=0; w<len; w++)
    {
    int BIT = sign*(w*(t*(42)));
    fprintf(fp, "%c", MESSAGE[w]+BIT);
    t++;
    system ("CLS");
    sign=-sign;
    }
    qu++;

    something like that anyways. BIT is by what the characters deviate. Anyways, sometimes it puts a new line character in the encrypted codeword and when I try do decode it, it works up till that point. here's how I decode it:


    Code:
    for (int doubleu=0; doubleu<LEN; doubleu++)
    {
    int bit=sign*(doubleu*(T*(42)));
    fscanf(FP, "%c", message[doubleu]+bit;
    T++;
    system ("CLS");
    doubleu++;
    sign=-sign;
    }
    this isn't exactly it, I can't find it on file, but if the file it creates, the one fp points to, has a new line in it, it won't let me decode the rest of it ...

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    You'd be better off with fgetc and fputc, rather than fscanf and fprintf.

    Encode:
    ch = MESSAGE[w];
    ch += BIT;
    fputc( ch, fp );

    Decode:
    ch = fgetc( fp );
    ch -= BIT;
    MESSAGE[w] = ch;

    Decode is just the reverse of encode.

  3. #3
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    well, excatly i belive in what Salem say its better to use fgetc rather than fprintf/fscanf. as u wanted to get the new line chr as well which could actualy done by with these two fucntion fgetc and fputc which would eventually work fine for you case

    ssharish2005

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Need Help Fixing My C Program. Deals with File I/O
    By Matus in forum C Programming
    Replies: 7
    Last Post: 04-29-2008, 07:51 PM
  3. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  4. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  5. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM

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