Thread: Retrieving info from a file...

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

    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 int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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,732
    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, 10:46 AM