Re: Storing input into a character array



Bert wrote:
On Jun 29, 10:27 pm, pete <pfil...@xxxxxxxxxxxxxx> wrote:
pete wrote:
Bert wrote:
Hi, I'm unhappy: why doesn't this work?
char enc[10000];
char temp[70];
for(int i=0;i<10000;i++){
fscanf(in,"%s",&temp);
if(temp[0]=='#')break;
else
for(int j=0;j<70;j++)
if(temp[j]!='\0')
enc[i]=temp[j];
else break;
}
The input is from a text file (.txt) that contains an encrypted
message between 1 and 10 000 letters long that may be split across
several lines in the input file; each line will contain between 1 and
70 letters inclusive and the final line will contain one #. I want the
entire message to be stored in enc (excluding the #). I thought I'm
using temp to store one line of text, checking if the first character
is a # and if not storing those characters into the next available
slots in the array enc until enc is filled up.
I'm unhappy
char enc[10000];
int i, rc;
for(i = 0; 10000 > i; ++i) {
rc = getc(in);
if (rc == EOF || rc == '#') {
break;
}
enc[i] = rc;
}
If you don't want newline characters in your array,
which I think is the case:

char enc[10000];
int rc;
int i = 0;

while (10000 > i) {
rc = getc(in);
if (rc == EOF || rc == '#') {
break;
}
if (rc != '\n') {
enc[i++] = rc;
}

}

--
pete

Thanks, that's just what I needed.

Now the first part of this output from the code below is given
correctly but there are strange symbols appearing after that in
command prompt.

char rows[numrows][100];
int j = 0;
for (int k=0; k<i; k+=(numrows*2-2)) {
rows[0][j] = enc[k];
++j;
}
As in my OP, this program is intended to decrypt messages and here is
PART of the decryption. If the input indicates 4 rows (different rows
means that the message has been ENcrypted differently) I want the
computer to take every sixth letter of the string in enc and store it
in the rows.
From an input I've tested with enc having 24 letters and 4 rows
encryption, it does correctly output the 4 characters of the first row
(the exact number of characters wanted in the right order; this output
is SOLELY what I wanted) correctly but is followed by symbols such as
an up arrow and @ etc.

Try this for your first line instead:

char rows[numrows][100]= {0};

--
pete
.



Relevant Pages

  • Re: Works on Windows but not on HPUX, Help Please
    ... You're counting on your compiler to optimize the evaluation of strlen. ... In particular, if char is signed, the result is implementation-defined, since you're subtracting an int from a char, which gets you an int. ... The resulting block of data could contain NUL characters, or in general characters you can't print. ... Breaking the encryption is trivial. ...
    (comp.lang.c)
  • Re: Enigma-like cipher: variable rotor stepping
    ... position would be zero, and the rotors would step once, on O they would ... rotos with 27 characters ... appropriate for encryption. ... char val3=R2.GetCharacterIndex; ...
    (sci.crypt)
  • RE: XP password and encryption
    ... I have heard that any password less than 15 characters is worthless on NTLM ... because it's in reality just two 7 char passwds. ... increases the encryption in a non-linear way... ... We provide Ethical Hacking, Advanced Ethical Hacking, Intrusion Prevention, ...
    (Security-Basics)
  • Re: Enigma-like cipher: variable rotor stepping
    ... position would be zero, and the rotors would step once, on O they would ... rotos with 27 characters ... appropriate for encryption. ... char val3=R2.GetCharacterIndex; ...
    (sci.crypt)
  • Re: heeeeeeeeeeeeeeeellllllllllllllppppppppppppppppppppp
    ... Why is using char* a bad thing and why using sprintf a bad thing to, ... can be up to MAX_PATH characters). ... LPSTR lpMsgBuf; ... MessageBox(NULL, lpMsgBuf, "GetLastError() for ...
    (microsoft.public.vc.mfc)