我对论坛很陌生,所以我希望我不会搞砸。我有一个从文件读取的程序,到目前为止它将文件放入二维字符数组中。我现在需要 strtok 二维“字符串”并将每个部分放入一个结构中
这是代码
struct processes
{
char processNumber[20];
int quanta;
int priority;
}process;
和
int readSave(int argc, char ** argv)
{
int i,j,k,count;
size_t blocksize = 16;
char originalFile[256], newFile[1000][20];
int fileDes;
ssize_t status;
unsigned char buffer[blocksize];
strcpy(originalFile, argv[1]);
fileDes = open(originalFile, O_RDONLY); // open for reading
i=0;
status = 99;
while(status > 0 ) // while no error
{
status = read(fileDes, buffer, blocksize);
strcpy(newFile[i],buffer); //line 71
for(k = 0; k <= blocksize; k++)
{
buffer[k] = 0;
}
i++;
if(status < 0)
{
printf("\nERROR\n");
exit(6);
}
}
//remove later
for(j = 0; j < i; j++) // prints out string to make sure it was input properly
{
printf("%s", newFile[j]);
}
printf("\n");
close(fileDes);
//Don't know how to carry on
}
我希望你能提供帮助,因为我迷路了 EDIT struct processStruct processes[7000]; while(newFile != NULL) { strcpy(processes[count].processNumber, strtok(newFile[count], "\n")); processes[count].quanta = atoi(strtok(NULL, "\n")); 进程[count].priority = atoi(strtok(NULL, "\n"));
count ++;
}
我更改了@Igor 给出的结构和输入,但是当我运行它时,出现分段错误,当我使用 -Wall 编译时,我得到 readtostring.c: In function 'readSave': readtostring.c:71:3: warning:传递 'strcpy' 的参数 2 的指针目标在符号上不同 [-Wpointer-sign] /usr/include/string.h:128:14: 注意:预期的 'const char * restrict ' 但参数的类型是 'unsigned char * '</p>