如何解决Memcpy并从指针数组中拆分出一个字符串
我正在尝试拆分分配给指针数组的字符串,并将其分配给矩阵数组。分配给数组的字符数取决于用户输入的数字(字节),因此我无法使用
import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.io.Writable;
public class IndexMapRecordWritable implements Writable {
private LongWritable offset;
private Text docid;
public LongWritable getoffsetWritable() {
return offset;
}
public Text getDocidWritable() {
return docid;
}
public long getoffset() {
return offset.get();
}
public String getDocid() {
return docid.toString();
}
public IndexMapRecordWritable() {
this.offset = new LongWritable();
this.docid = new Text();
}
public IndexMapRecordWritable(long offset,String docid) {
this.offset = new LongWritable(offset);
this.docid = new Text(docid);
}
public IndexMapRecordWritable(IndexMapRecordWritable indexMapRecordWritable) {
this.offset = indexMapRecordWritable.getoffsetWritable();
this.docid = indexMapRecordWritable.getDocidWritable();
}
@Override
public String toString() {
StringBuilder output = new StringBuilder()
output.append(docid);
output.append(offset);
return output.toString();
}
@Override
public void write(DataOutput out) throws IOException {
}
@Override
public void readFields(DataInput in) throws IOException {
}
}
之类的函数来分割字符串,因为我没有实际的分度数。
无论如何,如果输入的字节数为1,我可以从头到尾成功填写我的“矩阵”。
当输入为2或4时,我遇到了问题。由于某种原因,代码跳过了原始指针数组中的第一个字符,而从第二个字符开始。我有一个怀疑,因为 import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
import java.util.HashSet;
import org.apache.hadoop.io.Writable;
public class IndexRecordWritable implements Writable {
// Save each index record from maps
private HashSet<IndexMapRecordWritable> tokens = new HashSet<IndexMapRecordWritable>();
public IndexRecordWritable() {
}
public IndexRecordWritable(
Iterable<IndexMapRecordWritable> indexMapRecordWritables) {
}
@Override
public String toString() {
StringBuilder output = new StringBuilder();
return output.toString();
}
@Override
public void write(DataOutput out) throws IOException {
}
@Override
public void readFields(DataInput in) throws IOException {
}
}
跳过了第一个字符,因为它存储在指针中。我认为可能是这种情况,因为从技术上讲指针数组不是数组而是数组的指针,但这并不能真正解释为什么输入字节为1时为什么要存储第一个char。
下面是我的代码的一小段,其中包括动态数组分配和memcpy()的使用。
strtok()
在两行之间计算出我必须为矩阵分配的列数之后,我尝试使用memcpy。
memcpy()
如果我原来的理论是正确的,我将如何纠正这个问题?
char * fileArray = (char*)malloc(size*sizeof(char));
if (fileArray== NULL){
printf("NULL");
return 1;
}
fgets(fileArray,size+1,fp);
为了更清楚地说明我要执行的操作,基本上是将字符总数分成n个字节(读取字符)的列。我已经填充了#个字符,以使n总是可以被字符串整除。
我正在寻找类似这样的输出。
如果字节== 2:
char maTrix[numOfCols][bytes];
if (bytes == 1){
for (i = 0; i<numOfCols; i++) {
memcpy(&maTrix[i],&fileArray[i],bytes);
}
}
else if (bytes == 2 || bytes == 4) {
for (i = 0; i < numOfCols; i++) {
int k = i * bytes;
int p = k + bytes;
while (k < p) {
memcpy(&maTrix[i],&fileArray[k],bytes);
k++;
}
}
}
依次类推,直到填满整个矩阵。
相反,我得到了:
The highest forms of understanding we can achieve are laughter and human compass
ion. Richard Feynman
对于4字节的输入,只有较少的列和4行,预期也是如此。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。