将输入存储到字符数组中 [英] Storing input into a character array

查看:98
本文介绍了将输入存储到字符数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很不高兴:为什么这不行?


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]!=''\''')

enc [i] = temp [j];

其他休息;

}


输入来自包含加密

消息,长度在1到10 000之间,可以分成输入文件中的几行
;每行包含1到

70个字母,最后一行包含一个#。我想将

整个邮件存储在enc中(不包括#)。我以为我是
使用temp存储一行文本,检查第一个字符

是否为#并且如果没有将这些字符存储到下一个可用

数组中的插槽,直到填满。


我不开心

Bert


我很不高兴:为什么这不行?


char enc [10000];

char temp [70];


for(int i = 0 ; i< 10000; i ++){

fscanf(in,""%s"& temp);



这里有几个问题:首先,你没有保护你的缓冲区免受超过69个字符的

输入。用%69s而不是%s修复它。

其次,你没有从fscanf获取返回来检查

输入是否成功。通过与1(要解析的

字段的数量)的比较来修复它。第三,你使用& temp而不是temp -

因为temp(在这种情况下)被视为指向

第一个字节的指针临时数组,不需要&符号。第四,

你的解析将停在第一个空格,这可能不是你想要的b $ b。要解决这个问题,请使用fgets而不是fscanf。第五,如果您正在阅读的加密数据包含您的

输入例程视为哨兵的字符(例如fgets的换行符),则您的解析将失败
失败,但是

打算成为数据Feed的一部分。修复,如果有必要,通过读取固定大小的输入块到缓冲区(例如使用fread代替

fgets)并解析它为''#' '你自己,用例如memchr。


< snip>


-

Richard Heathfield< http:// www。 cpax.org.uk>

电子邮件:-http:// www。 + rjh @

谷歌用户:< http://www.cpax.org.uk/prg/writings/googly.php>

Usenet是一个奇怪的放置" - dmr 1999年7月29日


Bert写道:


我很不高兴:为什么没有这个工作吗?


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]!=''\''')

enc [i] = temp [j];

else break;

}


输入来自文本文件(.txt),其中包含1到10 000个字母长的加密

消息可能在输入文件中分成几行

;每行包含1到

70个字母,最后一行包含一个#。我想将

整个邮件存储在enc中(不包括#)。我以为我是
使用temp存储一行文本,检查第一个字符

是否为#并且如果没有将这些字符存储到下一个可用

数组中的插槽,直到填满。


我不开心

Bert


Bert写道:


我很不高兴:为什么这不起作用?


char enc [10000];

char temp [70];


for(int i = 0; i< ; 10000; i ++){

fscanf(in,"%s"& temp);


if(temp [0] ==' '#'')休息;

其他

for(int j = 0; j< 70; j ++)

if(temp [j ]!=''\''')

enc [i] = temp [j];



你没有更新我。

你要多次写到同一个地方。


else break;

}


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
Bert

解决方案

Bert said:

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);

Several problems here: firstly, you''re not protecting your buffer against
inputs longer than 69 characters. Fix that with %69s rather than %s.
Secondly, you''re not capturing the return from fscanf to check that the
input succeeded. Fix that with a comparison against 1 (the number of
fields you want to parse). Thirdly, you''re using &temp rather than temp -
since temp is (in this context) treated as if it were a pointer to the
first byte in the temp array, the ampersand is not required. Fourthly,
your parse will stop at the first space, which probably isn''t what you
want. To fix that, use fgets rather than fscanf. Fifthly, your parse will
fail if the encrypted data you''re reading contains a character that your
input routine treats as a sentinel (e.g. newline for fgets), but which is
intended to be part of the data feed. Fix that, if necessary, by reading
fixed-size blocks of input into a buffer (e.g. using fread instead of
fgets) and parse it for the ''#'' yourself, using e.g. memchr.

<snip>

--
Richard Heathfield <http://www.cpax.org.uk>
Email: -http://www. +rjh@
Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
"Usenet is a strange place" - dmr 29 July 1999


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
Bert


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];

You''re not updating i.
You''re writing multiple times to the same location.

else break;
}


这篇关于将输入存储到字符数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆