简体   繁体   English

从还包含字符串的.txt文件中读取整数和双精度

[英]Reading integers and double from .txt file which also contains strings

I want to write a program that reads lines of text from a file. 我想编写一个从文件中读取文本行的程序。 Each line contains several strings, one integer and one double value. 每行包含几个字符串,一个整数和一个双精度值。 The number of lines is not known in advance (ie, the program needs to read lines until end of file) and the length of each line is not known as well. 行数事先未知(即程序需要读取行直到文件末尾),并且每行的长度也不知道。 The while loop checks if the line ordered in following manner: while循环检查该行是否按以下方式排序:

 --  string -- integer -- double --- string  
Cat.     3:           18.00 Kr.       [ Tvål ]    
Cat.     1:           14.50 Kr.       [ Äppelmos Bob ]     
Cat.     1:           12.00 Kr.       [ Coca Cola 2 lit. ]     
Cat.     1:           18.00 Kr.       [ Kroppkakor 4 st. ] 

The problem is the last string contains several spaces, so the program does not take it as a one entire string. 问题是最后一个字符串包含多个空格,因此程序不会将其视为一个完整的字符串。 The last string is accepted like several strings and I see on my screen only Cat. 最后一个字符串像几个字符串一样被接受,我在屏幕上只看到Cat。 3: 18.00 Kr. 3:18.00韩元。 instead of whole list of lines. 而不是整个行列表。

I tried to handle program like this: 我试图处理这样的程序:

double doubleValue;

int intValue;

string str1, str2, str3;

ifstream infile("Register.txt", ifstream::in);

while (infile >> str1 >> intValue >> str2 >> doubleValue >> str3)
{   
   cout << intValue << " " << doubleValue << endl;
}

Thanks in advance. 提前致谢。

This is because operator>> will stop parsing at white spaces. 这是因为operator>>将在空白处停止解析。

To work out, you can read the whole line first by using std::getline() . 要进行计算,您可以先使用std::getline()阅读整行。 Then, parse the first four parts (by applying std::stringstream ), and finally get the remaining part by calling std::getline() again. 然后,解析前四部分(通过应用std::stringstream ),最后通过再次调用std::getline()获得其余部分。

#include <sstream>
using namespace std;

string line;
while (getline(infile, line)) // read the whole line from the file
{
    stringstream ss(line);
    ss >> str1 >> intValue >> str2 >> doubleValue; // pause the first four parts
    getline(ss, str3); // parse the remaining part to str3, e.g. "Kr. [ Tvål ]"
}

you could always use fscanf, it will let you, as long as you know the format: 您可以始终使用fscanf,只要您知道以下格式,它就可以让您:

fscanf(f, "%s %d [ %[^\x5D]\x5D %lf", str1, &int1, str2, &double1);

I personally prefer scanf, here is a simple table: 我个人更喜欢scanf,这是一个简单的表:

fmt   meaning
%s    non-whitespace string
%d    integer
%u    unsigned integer
%ld   long
%lu   unsigned long
%f    float
%lf   double
%llf  long double

It also handles special formats as well, but that is beyond the scope of this. 它还处理特殊格式,但这超出了此范围。 But its useful if you say have a file like this: 但是如果您说有这样的文件,它很有用:

30.1 multi word string

you can read it by 你可以阅读

scanf("%lf %[^\n]\n", &mydouble, strbuf);

Preference is key here, but I recommend you use fscanf for it 首选项在这里很关键,但是我建议您使用fscanf

fscanf(FILE *f, char *fmt, ...);

http://www.manpagez.com/man/3/fscanf/ http://www.manpagez.com/man/3/fscanf/

It's working, thanks to herohuyongtao . 多亏了herohuyongtao ,它的工作正常了 This is a full code snippet that allows to read .txt file which contains strings, integers, doubles and print only integer and double values. 这是完整的代码段,可读取包含字符串,整数,双精度数的.txt文件,并且仅输出整数和双精度值。
This is a part from a txt file. 这是txt文件的一部分。

Cat.     3:           14.50 Kr.       [ Äppelmos Bob ]     
Cat.     2:           12.00 Kr.       [ Coca Cola 2 lit. ]     
Cat.     5:           18.00 Kr.       [ Kroppkakor 4 st. ]

..........

..........

Solution is below. 解决方案如下。

    using namespace std;

        cout << "Category totals for last opening period: " << endl;

        double doubleValue;
        int intValue;
        string line, str, str2, str3;

        ifstream infile("Register.txt", ifstream::in);
        getline(infile, line);

        while (getline(infile, line))
        {   
            stringstream infile(line);
            infile >> str >> intValue >> str2 >> doubleValue;
            getline(infile, str3);
            cout << endl;

                cout << setw(3) << str << setw(1) << intValue << setw(7)                                                 << str2 << doubleValue << str3;
        cout << endl;

        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM