C++ 将第一列数据文件作为字符串读取
我有一个像这样的dat文件C++ 将第一列数据文件作为字符串读取,c++,C++,我有一个像这样的dat文件 AAAA 1861 9 7 0.00 -1.50 -19.50 9999.00 9999.00 9999.00 9999.00 18.20 9999.00 6.70 135.00 8.00 9999.00 9999.00 9999.00 9999 0 193 250180280600000000 BBBBBBBB
AAAA 1861 9 7 0.00 -1.50 -19.50 9999.00 9999.00 9999.00 9999.00 18.20 9999.00 6.70 135.00 8.00 9999.00 9999.00 9999.00 9999 0 193 250180280600000000
BBBBBBBBB 1861 9 7 0.00 -7.50 36.50 9999.00 9999.00 21.40 1018.10 22.60 9999.00 1.00 68.00 2.00 9999.00 9999.00 9999.00 9999 0 193 451720280600000000
VVVVVVVVV 1861 9 7 0.00 -58.50 10.50 9999.00 9999.00 27.50 9999.00 26.90 9999.00 15.40 45.00 6.00 9999.00 9999.00 9999.00 9999 0 193 357610280600000000
SSSSSSSS 1861 9 7 0.00 117.50 13.50 9999.00 9999.00 28.00 1010.30 28.00 9999.00 4.60 158.00 5.00 9999.00 9999.00 9999.00 9999 0 193 370170280600000000
DD 1861 9 7 1.00 -19.50 6.50 9999.00 9999.00 24.70 1014.80 25.30 9999.00 6.70 203.00 2.00 9999.00 9999.00 9999.00 9999 0 193 343600280600000000
E 1861 9 7 1.00 44.50 -28.50 9999.00 9999.00 20.40 9999.00 19.00 9999.00 2.60 135.00 2.00 9999.00 9999.00 9999.00 9999 0 193 218240280600000000
ZZZZZZZZZZZZ 1861 9 7 1.00 -15.50 38.50 9999.00 9999.00 22.00 9999.00 21.90 9999.00 6.70 68.00 2.00 9999.00 9999.00 9999.00 9999 0 193 458840280600000000
我想将文件的第一列存储在字符串向量中。有什么想法吗
这就是我试过的
for(int k=0;k<ROW;k++){
INNA >> ID;
for(int j=1;j<=COL;j++) INNA >> discardID;
IDname[k]= ID;
}
for(int k=0;k>ID;
对于(int j=1;j>id;
IDname[k]=ID;
}
但是在ID中,我将所有列作为一个唯一的字符串
非常感谢阅读整行,然后获得第一列
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
std::vector<std::string> vec;
std::string line;
while (std::getline(std::cin, line)) {
std::istringstream strm(line);
std::string firstcol;
strm >> firstcol;
vec.push_back(firstcol);
}
#包括
#包括
#包括
#包括
std::vec;
std::字符串行;
while(std::getline(std::cin,line)){
std::istringstream strm(线路);
std::字符串firstcol;
strm>>第一列;
向量推回(第一列);
}
或者在C语言中,您可以只读入第一列,然后丢弃剩余的。类似如下:
#include <stdio.h>
#include <malloc.h>
#include <string.h>
int main( )
{
FILE * fp = NULL;
fp = fopen("filename", "r");
if (!fp)
return -1;
char buffer[256];
char ** IDname = NULL;
int idcount = 0;
while (fscanf(fp, "%s %*[^\n]", buffer) == 1)
{
idcount++;
IDname = realloc(IDname, idcount * sizeof * IDname);
IDname[idcount - 1] = strdup(buffer);
}
return 0;
}
#包括
#包括
#包括
int main()
{
FILE*fp=NULL;
fp=fopen(“文件名”、“r”);
如果(!fp)
返回-1;
字符缓冲区[256];
字符**IDname=NULL;
int idcount=0;
而(fscanf(fp,“%s%*[^\n]”,缓冲区)==1)
{
idcount++;
IDname=realloc(IDname,idcount*sizeof*IDname);
IDname[idcount-1]=strdup(缓冲区);
}
返回0;
}
以下是我解决问题的方法:
#include <fstream>
#include <strtk.hpp> // http://www.partow.net/programming/strtk
std::string filename("your_input_data.txt");
// assuming the file is text
std::fstream fs;
fs.open(filename.c_str(), std::ios::in);
if(fs.fail()) return false;
const char *whitespace = " \t\r\n\f";
std::string keyword;
std::vector<float> floats;
std::vector<string> keywords;
// process each line in turn
while( std::getline(fs, line ) )
{
// remove beginnning and ending whitespace
// can prevent parsing problems
// from different line endings.
strtk::remove_leading_trailing(whitespace, line);
// strtk::parse combines multiple delimeters in these cases
if( strtk::parse(line, whitespace, keyword, floats ) )
{
std::cout << "succeed" << std::endl;
// keyword contains the string and the rest of the values
// are placed in the floats vector
keywords.push_back( keyword );
// you can also process the numbers
}
}
#包括
#包括//http://www.partow.net/programming/strtk
std::字符串文件名(“your_input_data.txt”);
//假设文件是文本
std::fstream fs;
open(filename.c_str(),std::ios::in);
if(fs.fail())返回false;
常量字符*空格=“\t\r\n\f”;
std::字符串关键字;
向量浮动;
std::向量关键词;
//依次处理每条生产线
while(std::getline(fs,line))
{
//删除开头和结尾空格
//可以防止解析问题
//从不同的行尾。
strtk::删除前导和尾随(空格、行);
//在这些情况下,parse组合了多个delimeter
if(strtk::parse(行、空格、关键字、浮点))
{
你试过什么?哪里出了问题?你做过什么研究?请在发帖子之前仔细阅读。问题是什么?简单地逐行阅读文件,将行拆分,然后得到第一项