gpt4 book ai didi

c++ - 从文本文件中读取信息并将其正确存储在数组中,int

转载 作者:太空宇宙 更新时间:2023-11-04 11:30:08 25 4
gpt4 key购买 nike

<分区>

我想要一段从文本文件中读取信息并将信息保存在一些变量、数组中的 C++ 代码...

例如,假设我们有一个文件 Numbers.txt,其中包含 4 个整数。到目前为止我找到的一个示例代码(对我来说没有用)如下所示:

string weight, height, width, depth;
ifstream MyFile("Numbers.txt");
MyFile >> weight;
MyFile >> height;
MyFile >> width;
MyFile >> depth;

但它只读取单条数据,由空格分隔。

我希望代码能够读取同一行中的多条数据。澄清一下,这是一个文本文件格式的示例......

FileName
ProjectName
Names
IDs

以及一个文本文件实际内容的例子......

// SomeData.txt 
FileName: SomeData.txt
ProjectName: Second Attempt // should be able to read with spaces too, and save in a string as "Second Attempt"
Names: John, Frank, Mike // I guess it could be without commas too
IDs: 42, 505, 1591, 4358, 12, 743

因此,主要问题是程序在阅读时跳到下一行,并忽略“关键字”,如“名称:”、“ID”...,同时准备好阅读尽可能多的条目排成一排,即使事先没有关于会有多少人的信息。

刚刚阅读的信息最好这样保存:

FileName -> string
ProjectName -> string
Names -> array of strings
IDs -> array of integers

这样的代码示例会是什么样子?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com