C++ 如何在两个分隔符之间提取数据或从文件中读取数据-->'|';在c+中使用getline函数+;?

C++ 如何在两个分隔符之间提取数据或从文件中读取数据-->'|';在c+中使用getline函数+;?,c++,C++,我在文件中的分隔符之间排列了数据。我想在一条直线上从一个特定点读到另一个特定点。看起来是这样的: |编号|名称|产品价格| 我想一次读取一个数据。 由这些分隔符划分的每个部分包含一个或多个音节,即一个或几个单词。 如何读取它?分隔文件的基本方法是将整行读取为字符串,然后从该行创建一个stringstream,并使用分隔符('|')在stringstream上循环调用getline,将值分隔到字段中 (这样就不需要提前知道字段的数量——如果没有stringstream,您必须使用getline将字

我在文件中的分隔符之间排列了数据。我想在一条直线上从一个特定点读到另一个特定点。看起来是这样的: |编号|名称|产品价格| 我想一次读取一个数据。 由这些分隔符划分的每个部分包含一个或多个音节,即一个或几个单词。
如何读取它?

分隔文件的基本方法是将整行读取为
字符串
,然后从该行创建一个
stringstream
,并使用分隔符(
'|'
)在stringstream上循环调用
getline
,将值分隔到字段中

(这样就不需要提前知道字段的数量——如果没有
stringstream
,您必须使用
getline
将字段的读取限制在当前的数量,否则
getline
将很乐意从下一行开始读取数据)

下面的示例只是将输入文件的每个字段读入一个
字符串
,但出于您的需要,您可能需要进行额外的转换(例如
stol
或类似转换),将单个字段转换为
int
double
(由您决定)

示例使用/输出

$ ./bin/iostream_strstream_fields dat/fields.txt
line: | 1   | Joe   | marbles  |   1.25 |

  value:  1
  value:  Joe
  value:  marbles
  value:    1.25

line: | 2   | Mike  | jacks    |  13.49 |

  value:  2
  value:  Mike
  value:  jacks
  value:   13.49

line: | 3   | Jill  | pail     |   4.50 |

  value:  3
  value:  Jill
  value:  pail
  value:    4.50
$ ./bin/fieldparse ~/dev/src-cpp/tmp/dat/fields.txt
  1 Joe        marbles    1.25
  2 Mike       jacks      13.49
  3 Jill       pail       4.50
仔细检查一下,如果你还有其他问题,请告诉我


用C语言重写32年前的操作系统

因为你试图用一个仍然不确定的C++编译器在一个32年的操作系统上编译(在你原来的问题中没有一个是指定的),那么你在C.编写你的解析例程可能更好的原因是什么?当时的C++标准是非常无定形的。与C++相比,它更像是C的超集。在当天的Turbo C和Borland C++编译器中,所有包含的文件仍然使用C头文件,包括格式(其中包含“代码>”。h’//>在标题名称的末尾,例如“代码>包含“< /代码>”),这解释了“查找失败的标题”问题。

另一方面,C更接近今天的可用性。虽然它经过了几次标准修订,但当时和现在可用的基本功能是相同的。以下是编译DOS 3.2运行在C++代码上的任何一个更好的机会。
#include <stdio.h>
#include <string.h>

#define FLDSZ 32   /* maximum file size for name and product */
#define MAXC 256   /* maximum length for line (including '\0') */

int main (int argc, char **argv) {

    char buf[MAXC] = "";    /* buffer to hold each line */
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
        return 1;
    }

    while (fgets (buf, MAXC, fp)) {
        int id;                     /* id from file */
        char name[FLDSZ] = "",      /* name */
            product[FLDSZ] = "";    /* product */
        float price;                /* price */
        size_t len = strlen (buf);  /* length of string in buffer */
        if (len == MAXC - 1 && buf[len - 1] != '\n') {  /* check it fit */
            fputs ("error: line too long.\n", stderr);
            return 1;
        }
        if (sscanf (buf, "|%d | %31s | %31s | %f",      /* parse fields */
                    &id, name, product, &price) != 4) {
            fputs ("error: failed to parse line.\n", stderr);
            continue;
        }
        /* output parsed fields */
        printf ("%3d %-10s %-10s %.2f\n", id, name, product, price);
    }
    if (fp != stdin) fclose (fp);   /* close file if not stdin */

    return 0;
}

我能告诉你的就是试一下,让我知道会发生什么。

你打电话给它
getline(stream,token,“|”)
将获取从开始到第一个、两个|之间或从最后一个|到流结束的所有内容。或者,更好的方法是,读取整行内容,然后使用
stingstreams(line)
然后使用
while(getline(s,val,“|”)
因此您一次读取的数据不会超过一行。谢谢您,先生。但问题是我的编辑器不支持某些函数,因为它无法识别头文件。你能给出其他的选择吗?什么编译器?(这些都是C++标准的标题。)你的编译器说什么是不可识别的?你能用
-std=C++11
选项编译吗?@VinitaKumari我在Linux和Windows7(旧版VS)上都编译过,并且在这两个平台上都能很好地编译和运行。我不能想象你有比W7更古老的东西吗?不,实际上我用MS-DOS写我的C++程序,像Borland C++的日子?什么版本的MSDOS?我曾经在DOS4.1上使用Borland(从DOS3.X中去掉了33M的分区限制)。你在用什么编译器,我可以查到它有什么?(哦,天哪,那是我的386DX和数学协处理器)我能想到的唯一没有的头是
,这是你的编译器找不到的吗?让我知道。
#include <stdio.h>
#include <string.h>

#define FLDSZ 32   /* maximum file size for name and product */
#define MAXC 256   /* maximum length for line (including '\0') */

int main (int argc, char **argv) {

    char buf[MAXC] = "";    /* buffer to hold each line */
    FILE *fp = argc > 1 ? fopen (argv[1], "r") : stdin;

    if (!fp) {  /* validate file open for reading */
        fprintf (stderr, "error: file open failed '%s'.\n", argv[1]);
        return 1;
    }

    while (fgets (buf, MAXC, fp)) {
        int id;                     /* id from file */
        char name[FLDSZ] = "",      /* name */
            product[FLDSZ] = "";    /* product */
        float price;                /* price */
        size_t len = strlen (buf);  /* length of string in buffer */
        if (len == MAXC - 1 && buf[len - 1] != '\n') {  /* check it fit */
            fputs ("error: line too long.\n", stderr);
            return 1;
        }
        if (sscanf (buf, "|%d | %31s | %31s | %f",      /* parse fields */
                    &id, name, product, &price) != 4) {
            fputs ("error: failed to parse line.\n", stderr);
            continue;
        }
        /* output parsed fields */
        printf ("%3d %-10s %-10s %.2f\n", id, name, product, price);
    }
    if (fp != stdin) fclose (fp);   /* close file if not stdin */

    return 0;
}
$ ./bin/fieldparse ~/dev/src-cpp/tmp/dat/fields.txt
  1 Joe        marbles    1.25
  2 Mike       jacks      13.49
  3 Jill       pail       4.50