本文介绍了知道确切的字符串数组长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好。我是C语言的新手。我有一个包含许多字符的二进制文件

定义为50个字符的数组


字符数组[50]


但在某些情况下,这50个字符中的许多字符都没有被使用。我想知道我怎么知道每个阵列中有多少个字符真的是




谢谢

Alberto

Hi. Im newbie in C language. I have a binary file with many character
arrays of 50 character defined as

char array[50]

But in some cases, many of these 50 characters are not being used. I
would like to know how could I know how many characters are really being
used in each array ?

Thanks
Alberto

推荐答案



你用什么意思使用?你的意思是他们不是零吗?

以下将给你一个计数,其中有多少首字符

非零。


#include< stdlib.h>

#include< stdio.h>

#include< string.h>


#define BLOCK_SIZE 50

int

main(无效)

{

char buf [BLOCK_SIZE + 1];

int count = 0;


buf [BLOCK_SIZE] = 0;

而(fread(buf,sizeof * buf,BLOCK_SIZE,stdin)== BLOCK_SIZE)

printf(" Block%d used%d bytes \ n",++ count,

strlen(buf));

}


What do you mean by "used"? Do you mean that they are non-zero?
The following will give you a count of how many of the first characters
are non-zero.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define BLOCK_SIZE 50
int
main(void)
{
char buf[BLOCK_SIZE+1];
int count=0;

buf[BLOCK_SIZE] = 0;
while(fread(buf, sizeof *buf, BLOCK_SIZE, stdin) == BLOCK_SIZE)
printf("Block %d used %d bytes\n", ++count,
strlen(buf));
}





如果你可以定义你的内容意思是用过,答案很明显。

如果你不能,那就没有答案。


-

Keith Thompson(The_Other_Keith)< http://www.ghoti.net / ~kst>

圣地亚哥超级计算机中心< *> < http://users.sdsc.edu/~kst>

我们必须做点什么。这是事情。因此,我们必须这样做。



If you can define what you mean by "used", the answer will be obvious.
If you can''t, there is no answer.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.




你用什么意思使用?你的意思是他们不是零吗?
以下内容将为你计算出第一个角色中有多少是非零的。

#include< stdlib .h>
#include< stdio.h>
#include< string.h>

#define BLOCK_SIZE 50
int
main( void)
{char buf [BLOCK_SIZE + 1];
int count = 0;

buf [BLOCK_SIZE] = 0;
while(fread (buf,sizeof * buf,BLOCK_SIZE,stdin)== BLOCK_SIZE)
printf(" Block%d used%d bytes \\\
",++ count,
strlen(buf));
}


What do you mean by "used"? Do you mean that they are non-zero?
The following will give you a count of how many of the first characters
are non-zero.

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#define BLOCK_SIZE 50
int
main(void)
{
char buf[BLOCK_SIZE+1];
int count=0;

buf[BLOCK_SIZE] = 0;
while(fread(buf, sizeof *buf, BLOCK_SIZE, stdin) == BLOCK_SIZE)
printf("Block %d used %d bytes\n", ++count,
strlen(buf));
}



tnbx为您的答案。是的,我的意思是,如果我有阵列


char buf [50]


那么有时它将包含20个字符而不是'' \'''我想知道

这些字符的数量,因为我想用指针做一个链接的动态

列表,每个节点应该有串QUOT; 50个字符的静态

数组,真正的字符不是''\0'',所以我必须知道

数字,之后使用calloc或malloc函数


tnbx for your answer. Yes, I mean that if I have the array

char buf[50]

then some times it will contain 20 characters not ''\0'' I want to know
the amount of these characters, because I want to do a linked dynamic
list with pointers and each node should have the "string" of the static
array of 50 chars with really characters not ''\0'', so I must know the
number and after that use calloc or malloc functions


这篇关于知道确切的字符串数组长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 19:16