Re: Storing input into a character array
- From: pete <pfiland@xxxxxxxxxxxxxx>
- Date: Sun, 29 Jun 2008 22:28:27 -0400
Bert wrote:
On Jun 29, 10:27 pm, pete <pfil...@xxxxxxxxxxxxxx> wrote:pete wrote:Bert wrote:If you don't want newline characters in your array,Hi, I'm unhappy: why doesn't this work?char enc[10000];
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
int i, rc;
for(i = 0; 10000 > i; ++i) {
rc = getc(in);
if (rc == EOF || rc == '#') {
break;
}
enc[i] = rc;
}
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
.
- Follow-Ups:
- Re: Storing input into a character array
- From: Bert
- Re: Storing input into a character array
- References:
- Storing input into a character array
- From: Bert
- Re: Storing input into a character array
- From: pete
- Re: Storing input into a character array
- From: pete
- Re: Storing input into a character array
- From: Bert
- Storing input into a character array
- Prev by Date: Re: this calls and checks strtol() correctly?
- Next by Date: Re: how to release memory?
- Previous by thread: Re: Storing input into a character array
- Next by thread: Re: Storing input into a character array
- Index(es):
Relevant Pages
|