2012-06-19 85 views
0

如何将字符串(存储在fgets()函数中)标记为各种所需的大小? 这些值之间没有任何分隔符(\n,,,:,:,等)。 字符串是在c编程的期望长度中标记字符串?

“ABCD 1234 EFG56H1.223.445.66 J”

我想要得到的令牌应该像看起来

“ABCD” “1234”““/ 读取空间作为字符/“EF” “56” “H” “1.22”/ (。)点也字符/“3.44”, “5.66”

如何使用strtok函数执行此操作?

这个问题实际上是使用C.

+0

不要使用'strtok'了点。如果你知道这个长度,可以使用它,例如'strncpy(dest,src + off,len);'。 –

+0

请勿使用strncpy()。如果你知道长度,使用'memcpy(dst,src + off,len); dst [len] = 0;' – wildplasser

回答

0

PDB文件我不认为你可以使用strok提取ATOM记录。你需要编写自己的子功能,但像这样的工作:

int position = 0; 
int i = 0; 

for (int i = 0; i < lengthOfColumnWidths; i++) { 
    if (columnWidths[i] <= 0) { 
     # Error, column width must be greater than 0. 
    } 

    char* columnVal = substring(lineOfText, position, position + columnWidths[i]); 
    # store columnVal in some final array, e.g. finalArray[i] = columnVal 

    position = position + columnWidths[i]; 
} 

列宽是一个整数数组,其中第i个值是第i列的宽度。所以对你来说会是:

[4, 5, 1, 2, 2, 1, 4, etc...] 
+0

lengthofcilumnwidths代表什么?这是我们将要制动成子串的字符串的长度吗? –

+0

substring函数不工作,显示“undefined reference to substring” –

+0

对不起,那不是很清楚。 lengthOfColumnWidths是columnWidths数组的长度。这也是您最终想要的列数。 – carmenism

0

您可以简单地使用的sscanf像

char a[100],b[100],c[100],d[100],e[100],f[100],g[100],h[100]; 
sscanf("ABCD 1234 EFG56H1.223.445.66 J","%s%s%2s%*1s%2s%1s%4s%4s%4s",a,b,c,d,e,f,g,h); 
+0

它可以用于空格和空值吗? –