Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 切片文件字符串_C_File_Slice - Fatal编程技术网

C 切片文件字符串

C 切片文件字符串,c,file,slice,C,File,Slice,我有一个文本文件,它在一行中保存ID和名称 314 name1 122 name3 884 name2 ID是3位数字(我们将其处理为char),然后在名称后面有一个空格,后面是8位数字(包括空格),然后是另一个人的ID/姓名,依此类推 因此,我打开一个文件,读取它,并想“切片”它 *我要提到的struct并不重要,只是一个示例,可以在底部找到 基本上,取前3个数字并将它们放在ID下的结构中,然后忽略下面的空格,然后抓住后面的8个数字并将它们放在同一个结构的名称下,依此类推(每次只需删

我有一个文本文件,它在一行中保存ID和名称

314 name1   122 name3   884 name2
ID是3位数字(我们将其处理为
char
),然后在名称后面有一个空格,后面是8位数字(包括空格),然后是另一个人的ID/姓名,依此类推

因此,我打开一个文件,读取它,并想“切片”它

*我要提到的
struct
并不重要,只是一个示例,可以在底部找到

基本上,取前3个数字并将它们放在ID下的结构中,然后忽略下面的空格,然后抓住后面的8个数字并将它们放在同一个结构的名称下,依此类推(每次只需删除文件的前12个数字),问题只是切片

我来自Python,如果我有一个
字符串
,并且只想保留前两个数字,我会做一些类似
string=string[0-2]

//ProductTree is a struct 

char initial_s[sizeof(initialStockFile)];
ProdudctTree* temp = NULL;
fscanf(initialStockFile, "%s", initial_s);
        temp = malloc(sizeof(ProductTree));
        while( initial_s != '\0' ){
            temp->id = initial_s[0-9];
            temp->productName = initial_s[10-20];
            initial_s = initial_s+21;
            temp = temp->right;
        }
        temp->right = NULL;
        return temp;
这是我尝试过的,但显然不起作用

如果有人好奇的话,这个结构

typedef struct ProductTree {
    char* id;               //Product ID number. This is the key of the search tree.
    char* productName;      //Name of the product.
    struct ProductTree *left, *right;
} ProductTree;

您可能需要使用LibC
strtok
功能。它允许您在给定分隔符列表的情况下拆分字符串

以下是一个与您的项目类似的示例:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int     main(int argc, char **argv)
{
  int   i;
  char  *line;
  char  *tok;

  i = 0;
  if (!(line = strdup("123 Hello 345 World 678 Foo 901 Bar")))
    return EXIT_FAILURE;
  tok = strtok(line, " ");
  while (tok)
    {
      if ((i % 2) == 0)
        printf("ID: ");
      else
        printf("Name: ");
      puts(tok);
      tok = strtok(NULL, " ");
      ++i;
    }
  free (line);
  return EXIT_SUCCESS;
}

由于您来自Python,
strtok(char*str,const char*delim)
,这里已经提到过,它的工作与Python中的
string.split(s[,sep[,maxslit]])
类似(有一些区别,例如:没有默认分隔符。它不返回字符串列表。它只返回第一个拼接,然后您需要将
str
设置为
NULL
)再次调用它)

但是不需要使用任何函数在字符串中搜索某个分隔符,因为您知道要将文件切片为每个12个字符长的字符串(然后将每个字符串切片为包含前3个字符的字符串和包含后8个字符的字符串,丢弃它们之间的一个字符)

对于第一部分,将文件切片为12个字符长的字符串,您有两个选项:

  • 您可以使用
    fgets(tmp_str,13,filename)
    从文件中一次读取12个字符(大小为13,因为它读取的字符比大小少一个,因为它需要多一个字符以
    '\0'
    结束字符串)
  • 或者您可以将所有文件读入一个字符串。然后在一个循环中,使用
    strncpy(tmp_str,&initial_s[i*12],12)
    (假设
    i
    是我们的计数器,从
    0开始,文件被读入
    initial_s
    )将12个字符从
    i*12
    位置复制到tmp_str中(不要忘记用
    tmp_str[12]='\0'
    终止字符串
对于第二部分,您还有两个选项:

  • 您可以再次使用
    strncpy()
    。这一次先将3个字符复制到
    temp->id
    ,从
    tmp\u str
    开始,然后将8个字符复制到
    temp->productName
    ,从位置
    4
    开始(记住用“\0”终止所有字符串)。如果这样做,您可以将
    struct
    更改为具有字符数组,而不是指向字符的指针:

    typedef struct ProductTree {
        char id[4];               //Product ID number. This is the key of the search tree.
        char productName[9];      //Name of the product.
        struct ProductTree *left, *right;
    } ProductTree;
    
  • 或者对于第二部分,您可以有点“聪明”。让
    temp->id
    指向
    tmp\u str
    的开头,让
    temp->productName
    指向
    tmp\u str
    中的第五个字符,并将它们之间的空格更改为
    '\0'
    以终止
    temp->id

    temp->id = tmp_str;
    tmp_str[3] = '\0';
    temp->productName = tmp_str[4];
    

无论您选择哪个选项,请记住为所有字符串分配内存,检查所有输入是否有错误,完成后释放字符串和结构。

到目前为止您尝试了什么?如果字段不包含空格,请尝试
strok()
(谷歌主页)您的具体问题是?您的代码在哪里?它有什么问题?我们不是编码服务。我尝试了
lfind()
fseek())
运行不太顺利,文件将包含空间,因此…@Olaf我不是在寻找代码,我是在问切片是什么done@Badda:仅仅因为功能不是线程安全的并不意味着它是“不完美的”。跑车不适合越野,但它比吉普车更适合在街上行驶(不是那些城市时髦的SUV)。
temp->id = tmp_str;
tmp_str[3] = '\0';
temp->productName = tmp_str[4];