Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/qt/7.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++中的TXT文件读入字符串和浮点_C++_Stream - Fatal编程技术网

将C++中的TXT文件读入字符串和浮点

将C++中的TXT文件读入字符串和浮点,c++,stream,C++,Stream,我需要读取一个*.txt文件。它包含单词和数字,如下所示: 第一个字:12,13.0第二个字18.7第三个字2,3,89 我需要将单词提取为字符串,将数字提取为浮点数。我无法解决的主要问题是单词前没有分隔符,否则我会使用getline 谢谢大家! 注意:这些词不包含数字,例如,“num1”一词是不可能的 对于sstream,它通常需要一个分隔符,因为文件中没有分隔符 通过使用标准c库中的函数,可以在不使用分隔符的情况下逐个字符地执行此操作。而且它并不优雅 这是从包含第一个单词的文本文件text.

我需要读取一个*.txt文件。它包含单词和数字,如下所示:

第一个字:12,13.0第二个字18.7第三个字2,3,89

我需要将单词提取为字符串,将数字提取为浮点数。我无法解决的主要问题是单词前没有分隔符,否则我会使用getline

谢谢大家!

注意:这些词不包含数字,例如,“num1”一词是不可能的

对于sstream,它通常需要一个分隔符,因为文件中没有分隔符

通过使用标准c库中的函数,可以在不使用分隔符的情况下逐个字符地执行此操作。而且它并不优雅

这是从包含第一个单词的文本文件text.txt中提取单词并加倍的一种方法:12,13.0第二个单词18.7第三个单词2,3,89

此解决方案使用函数isalpha提取任何与字母表相关的内容,并将其存储在数组extractedWords[]中

对于double,它使用isdigit和atof提取任何与数字相关的内容,并将其存储在数组extractedDoubles[]中


这些词能包括数字吗,比如阿波罗13?如果是,则格式不明确;如果没有,那么您可以一次解析一个字符。不,单词不包含数字。可以尝试使用stringstream。@Maria:您需要在char:ifc>='0'&&c:我明白后处理它。我想也许有更优雅的方式。
#include<string>
#include<fstream>
#include<iostream>

#include <stdlib.h>
using namespace std;

char mainBuffer[1024]={' '};
size_t fsize ;
string extractedWords[100];
double extractedDoubles[100]={0};


string buf2str(const char* buffer);
void loadFileToBuffer(char *filename);
void extractWords();
void extractFloats();



int main(){

    loadFileToBuffer("text.txt" );
    extractWords();  
    extractFloats();
    cout<<"\n";
    return 0;
}                                                                             



string buf2str(const char* buffer){
    return string(buffer);
}


void loadFileToBuffer(char *filename){
    ifstream infile;
    infile.open(filename);

    infile.read(mainBuffer, sizeof mainBuffer);
    if (infile.eof()){
         fsize = infile.gcount();
         cout<<"fsize = "<<fsize<<" , data =  " << mainBuffer <<" \n ";
    }
    else if (infile.fail()){
        // some other error...
    }
    else{
        // buf must be full, but the file is larger...
    }
     infile.close();
}

void extractWords(){
    char character;
    int i=0;
    int j=0;
    char shortBuffer[16]={' '};
    int seek=0;


     cout<<"\n";
    seek=0;
    i=0;
    j=0;
    while ( seek<=fsize ) {
        character=mainBuffer[seek];
        if ( isalpha(character) ) {
             shortBuffer[i]=character;
            i++;
        }
         else{
            shortBuffer[i]='\0';
            if (strlen(shortBuffer)>2){
                extractedWords[j]=buf2str(shortBuffer);
                i=0;
                j++;
            }
         } 
         seek++;
    }
    for(int ii=0;ii<j;ii++) cout<<extractedWords[ii] <<"\n";
    cout<<"\n";
}


void extractFloats(){
    char character;
    int i=0;
    int j=0;
    char shortBuffer[16]={' '};
    double dValue=0;
    int seek=0;

    cout<<"\n";
    seek=0;
    i=0;
    j=0;

    while ( seek<=fsize ) {
        character=mainBuffer[seek];
        if ( isdigit(character) || character=='.' ){
              shortBuffer[i]=character;
              i++;
        }
        else{
             shortBuffer[i]='\0';
             dValue=atof(shortBuffer);
             if (dValue != 0) {
                 extractedDoubles[j]=dValue;
             j++;
             i=0;
             }           
        }
        seek++;
    }
    for(int ii=0;ii<j;ii++) cout <<extractedDoubles[ii]<<"\n";
    cout<<"\n";
}