在c中调整字符数组的大小 [英] Resize the character array in c

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

问题描述

大家好,

MFC编码器希望学习一些关于charecter数组初始化和元素删除的基本知识。以下示例使用MFC(具有CString,因此不需要内存分配或de分配)但同样需要c。)

(没有std :: string接口: - ()



示例1: -

要构造字符串,我们按照MFC中的代码进行操作。

 CString constructString; 
constructString = ;
constructString = ABC;
constructString = constructString + PQR;
constructString = constructString + LMN;



无论我们有什么大小的字符串都可以使用。



for C i使用以下代码

  #define DEFAULT_ARRAY_SIZE 20000 
char * constructString = new char [DEFAULT_ARRAY_SIZE];
strcpy(constructString, );
strcat(constructString, ABC);
strcat(constructString, PQR);
strcat(constructString, LMN);



问题: -

1)代码将正常工作,直到我的char * constructString大小更小超过20000,但当它超过我没有解决方案,如何调整我的阵列,所以它将需要更多的字符。

2)我初始化char * constructString 20000但当我的字符串非常小的大小10然后我剩下的18990字符是浪费或不知道我不知道,这会影响我的可执行性能。如果是,那么如何删除我剩下的虚拟字符。



示例2: -

要从文件中读取内容,我们在MFC中使用以下代码。

 CStdioFile ReadFile; 
ReadFile.Open( Sample.txt,CFile :: typeText | CFile ::读);
CString CurrentString;
CStringArray WholeFile;
while (ReadFile.ReadString(CurrentString))
{
WholeFile.Add(CurrentString);
}



白色文件大小可以正常使用。



对于C我使用以下代码

  #define MAX_FILE_SIZE 65534 
FILE * ptr_file;
const char * list [MAX_FILE_SIZE];
wchar_t CurrentString [ 1000 ];
ptr_file = fopen( Sample.txt rb);
int __index = 0 ;
while (fgetws(CurrentString, 1000 ,ptr_file)!= NULL)
{
char * errorDes;
errorDes = new char [ 1000 ];
wcstombs(errorDes,CurrentString, 1000 );
list [__ index] = errorDes;
__index ++;
}



问题: -

1)如果我的一行字符超过1000,则不再考虑超过1000个字符,反之亦然。

2)如果我的文件大小超过65534,那么char * list array将无法正确填充反之亦然。



________________________________________

请提供任何链接,代码块,建议,帮助我解决纯c中的所有问题。



谢谢。

解决方案

使用动态内存分配解决了所有问题:你不应该分配一个静态数组但有点复杂的数据结构(模拟 CString std :: string ,行为)。例如

  typedef   struct  _MyString 
{
char * str;
int len;
int 容量;
} MyString;





然后你应该提供一些操作这样一个字符串的函数,例如:

  int  mystring_init(MyString * pms, const   char  * src); 
int mystring_cat(MyString * pms, const char * src);
int mystring_destroy(MyString * pms);
// ......等等......







粗略的 mystring_init 实施可能是:

  //  为简洁起见,省略了错误检查 
int mystring_init(MyString * pms, const char * src)
{
pms-> len = strlen(src + 1);
pms-> capacity = pms-> len;
pms-> str =( char *)malloc(pms-> capacity + 1);
strcpy(pms-> str,src);
return 0 ;
}



in mystring_cat 实现你需要重新分配内部存储器,''使用 realloc [ ^ ]功能:

  //  为简洁省略了错误检查 
int mystring_cat(MyString * pms, const char * src)
{
pms-> len + = strlen(src + 1);
pms-> capacity = pms-> len;
pms-> str =( char *)realloc(pms-> str,pms-> capacity + 1);
strcat(pms-> str,src);
return 0 ;
}





请注意以上是(未经测试的)幼稚实施,只是为了给你一个想法


Hi guys,
A MFC coder want to learn some basic about charecter array intialisation and deletion of element.Take following examples campare with MFC (there is CString so no need of memory allocation or de allocation but same needed in c.)
(dont have std::string interface :-()

Example 1:-
To construct string we us following code in MFC.

CString constructString;
constructString = "";
constructString = "ABC";
constructString = constructString + "PQR";
constructString = constructString + "LMN";


whatever size of string we have this will work.

for C i used following code

#define DEFAULT_ARRAY_SIZE			20000
char* constructString  = new char[DEFAULT_ARRAY_SIZE];
strcpy(constructString ,"");
strcat(constructString ,"ABC");
strcat(constructString ,"PQR");
strcat(constructString ,"LMN");


Problem :-
1)Code will work fine till my char* constructString size is less than 20000 but when it exceed i dont have solution,how to resize my array so it will take more charecters.
2)I intialize char* constructString with 20000 but when my string is very small of size 10 then my remaining 18990 charecters are wasted or not i dont know,will this effect my executable perfomance.If yes then how to delete my remaining dummy charecters.

Example 2:-
To read content from file we use following code in MFC.

CStdioFile ReadFile;
ReadFile.Open("Sample.txt",CFile::typeText|CFile::Read);
CString CurrentString;
CStringArray WholeFile;
while(ReadFile.ReadString(CurrentString))
{
    WholeFile.Add(CurrentString);
}


Whitever size of File it will work fine.

For C i use following code

#define MAX_FILE_SIZE				65534
FILE *ptr_file;
const char* list[MAX_FILE_SIZE];
wchar_t CurrentString[1000];
ptr_file =fopen("Sample.txt","rb");
int __index = 0;
while(fgetws (CurrentString , 1000 , ptr_file) != NULL)
{
	char* errorDes;
	errorDes = new char[1000];
	wcstombs(errorDes, CurrentString, 1000);
	list[__index] = errorDes;
	__index++;
}


Problem :-
1)Same as above if my one line charecters exceed 1000 then more than 1000 charecters are not consider and vise versa.
2)If my file size exceed 65534 then char* list array will not fill properly and vise versa.

________________________________________
Please provide me any link,block of code,suggestion that help me to solve all problem in pure c.

Thanks.

解决方案

All your problems are solved using dynamic memory allocation: you shouldn''t allocate a static array but a bit more complex data structure (emulating CString or std::string, behaviour). e.g.

typedef struct _MyString
{
  char * str;
  int len;
  int capacity;
} MyString;



Then you should provide some functions for manipulating such a string, for instance:

int mystring_init(MyString * pms, const char * src);
int mystring_cat(MyString *pms, const char * src);
int mystring_destroy(MyString * pms);
// ...and so on....




A rough mystring_init implementation could be:

// error checking omitted for sake of brevity
int mystring_init(MyString * pms, const char * src)
{
  pms->len = strlen(src+1);
  pms->capacity = pms->len;
  pms->str = (char*) malloc(pms->capacity+1);
  strcpy(pms->str, src);
  return 0;
}


in mystring_cat implementation you need to reallocate the internal memory, that''s accomplished using the realloc[^] function:

// error checking omitted for sake of brevity
int mystring_cat(MyString * pms, const char * src)
{
  pms->len += strlen(src+1);
  pms->capacity = pms->len;
  pms->str = (char*) realloc(pms->str, pms->capacity+1);
  strcat(pms->str, src);
  return 0;
}



Please note the above are (untested) naive implementation, written only to give you an idea.


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

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