在xv6

问题描述

我正在尝试创建一个函数,该函数逐行读取txt.file并按每一行的每个索引保存到char指针数组中。

由于Xv6环境没有任何strcat或任何典型的STL库。我从头开始创建了strcat函数

为此算法,我放sizeof(buf[0])的原因是要逐个阅读字母,因为它在循环中会逐个字母逐出。

我成功地逐个字母地提取内容,并尝试添加缓冲区2的指针数组。只有在看到行更改并更新index=counter++时,数组的索引才会移动。

但是,当我尝试使用str(buff2,buff)进行字符串concat并按索引打印出buffer2时。 结果显示

-buff2 0: NNNNNNNNNNn  
-buff2 1: NNNNNNNNNn 
-buff2 2: NNNNNNNNn 
-buff2 3: NNNNNNNn 
-buff2 4 : NNNNNNn 
-buff2 5: NNNNNn 
-buff2 6: NNNNn 
-buff2 7: NNNn 
-buff2 8: NNn 
-buff2 9 : Nn 
-buff2 10: n 

txt文件包含

no.1 \next line
no.3 \next line
no.5 \next line
no.6 \next line
no.7 \next line
......
#include "types.h"
#include "stat.h"
#include "user.h"

char *starCat(char *a,char *b){
 int i=0,j=0; 
 int cont=0;
 int len=strlen(a)+strlen(b);
 char *combined;
  combined=(char*)malloc(len*sizeof(char));

    for(i=0;i<strlen(a);i++){
        combined[i]=a[i];
    }

    for(j=i;j<strlen(a)+strlen(b);j++){
       combined[j]=b[cont++];
    }

 combined[len]='\0';
    return combined;
}

char buf[100];

void
trial(int fd,char *name)
{
   int n,i=0,count=0,count2=0,l,g;
   char *buff2;
 
  buff2=malloc(512*sizeof(char*));
  while((n = read(fd,buf,sizeof(buf[g]))) > 0){
    
    for(i=0; i<n; i++){
      count++;
    
      if(buf[i] == '\n'){
    buff2[count2]=*starCat(&buff2[count2],&buf[i]);
        L++;
    count2++;
    }
      else {   
    printf(1,"buf1 value: %s",&buf[i]);//shows the individual char for 
    buff2[count2]=*starCat(&buff2[count2],&buf[i]);
    
      }
         printf(1,"\n");
    
    }    
        
  }
    if(n < 0){
      printf(1,"uniqtrial: read error\n");
      exit();
    }

 printf(1,"\n");
 
int t;
for(t=0;t<count2;t++){
    printf(1,"buff2: %s \n",&buff2[t]);
}
    
    
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...