C++ 从 .txt 文件中读取字符串和整数并仅将输出打印为字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5214252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 17:41:30  来源:igfitidea点击:

Reading strings and integers from .txt file and printing output as strings only

c++

提问by screename71

I'm new to C++, and I'm trying to write a short C++ program that reads lines of text from a file, with each line containing one integer key and one alphanumeric string value (no embedded whitespace). The number of lines is not known in advance, (i.e., keep reading lines until end of file is reached). The program needs to use the 'std::map' data structure to store integers and strings read from input (and to associate integers with strings). The program then needs to output string values (but not integer values) to standard output, 1 per line, sorted by integer key values (smallest to largest). So, for example, suppose I have a text file called "data.txt" which contains the following three lines:

我是 C++ 新手,我正在尝试编写一个简短的 C++ 程序,该程序从文件中读取文本行,每行包含一个整数键和一个字母数字字符串值(无嵌入空格)。预先不知道行数(即,继续读取行直到到达文件末尾)。该程序需要使用“std::map”数据结构来存储从输入读取的整数和字符串(并将整数与字符串关联)。然后程序需要将字符串值(但不是整数值)输出到标准输出,每行 1 个,按整数键值(从小到大)排序。因此,例如,假设我有一个名为“data.txt”的文本文件,其中包含以下三行:

10 dog
-50 horse
0 cat
-12 zebra
14 walrus

10狗
-50马
0猫
-12斑马
14海象

The output should then be:

然后输出应该是:

horse
zebra
cat
dog
walrus


斑马


海象

I've pasted below the progress I've made so far on my C++ program:

我已经在我的 C++ 程序上粘贴了迄今为止取得的进展:

#include <fstream>  
#include <iostream>  
#include <map>
using namespace std;
using std::map;
int main () 
{
string name;
signed int value;
ifstream myfile ("data.txt");

while (! myfile.eof() )
{
getline(myfile,name,'\n');
myfile >> value >> name;
cout << name << endl;
}
return 0;
myfile.close();
}

Unfortunately, this produces the following incorrect output:

不幸的是,这会产生以下不正确的输出:

horse
cat
zebra
walrus



斑马
海象

If anyone has any tips, hints, suggestions, etc. on changes and revisions I need to make to the program to get it to work as needed, can you please let me know?

如果有人对我需要对程序进行更改和修订以使其按需要运行有任何提示、提示、建议等,请告诉我吗?

Thanks!

谢谢!

回答by fpointbin

See it:

看见:

#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main()
{
        string name;
        int value;
        ifstream myfile("text.txt", ifstream::in);
        while(myfile >> value >> name)
                cout << name << endl;
        return 0;
}

回答by ssegvic

You are having problems because you attempt to read each line twice: first with getline and then with operator>>.

您遇到问题是因为您尝试读取每一行两次:首先使用 getline,然后使用 operator>>。

回答by Puppy

You haven't actually used std::mapin any regard, at all. You need to insert the integer/string pair into the map, and then iterate over it as the output. And there's no need to close()the stream.

你根本没有std::map在任何方面实际使用过。您需要将整数/字符串对插入到映射中,然后对其进行迭代作为输出。并且不需要close()流。

回答by PANDIYARAJAN G

Instead of using "! myfile.eof()" use this code it will help.

使用此代码而不是使用“!myfile.eof()”它会有所帮助。

ifstream is;
string srg;
is.open(filename);
 while(getline(is,srg))
 {//your code
  }