c ++将文本文件读入int和字符串

时间:2011-05-16 10:55:46

标签: c++ int iostream ignore

我有一个文本文件的标准读入,但我需要的是以int为单位读取的行的前3个字符,以及逐行读取的行的其余部分。我把下面的代码放在示例文本中。

谢谢

#include <fstream>
#include <iostream>
using namespace std;

int main () 
{
 char buffer[256];
 ifstream myfile ("example.txt");

  while (! myfile.eof() )
  {
    myfile.getline (buffer,100);
    cout << buffer << endl;
  }
  return 0;
}

5 个答案:

答案 0 :(得分:2)

像这样的东西(伪代码,我相信你可以弄清楚真正的操作!)

std::string line;
ifstream myfile ("example.txt");

// this gets a line of text from the file.
while(std::getline(myfile, line))
{
  // now you need to extract three characters and convert to int, so is it always guranteed?
  if (line.size() > 3)
  {
    std::string int_s = <substring from 0, size: 3>; // lookup this function in a reference!
    std::string rest_s = <substring from 3 to end>; // ditto for the lookup

    // now convert the integer part.
    long int int_v = <conversion routine, hint: strtol>; // lookup syntax in reference.
    // use...
  }
}

答案 1 :(得分:1)

从stackoverflow检查thisthis

你在你的缓冲区上使用sscanf(假设你的字符串,如果NULL终止)指定一个这样的格式字符串:"%d%s",或者你使用 来自std::stringstreamoperator<<

注意:如果您的字符串包含空格,则应使用“%d%n”代替“%d%s”和sscanf,如下所示:

     int val = 0;
 int pos = 0;
 sscanf(buffer, "%d%n", &val, &pos);

 std::cout << "integer: " << val << std::endl;
 std::cout << "string: " << buffer+pos << std::endl;

答案 2 :(得分:1)

哦,我实际上推荐了Boost Spirit(Qi),稍后会看到一个例子

   #include <fstream>
    #include <iostream>
    #include <sstream>
    #include <string>
    using namespace std;

    int main () 
    {
        ifstream myfile ("example.txt");

        std::string line;
        while ( std::getline(myfile, line) )
        {
            std::istringstream iss(line.substr(0,3));
            int i;
            if (!(iss >> i))
            {
                i = -1;
                // TODO handle error
            }

            std::string tail = line.size()<4? "" : line.substr(4);
            std::cout << "int: " << i << ", tail: " << tail << std::endl;
        }
        return 0;
    }

只是为了好玩,这是一个更灵活的基于Boost的解决方案:

#include <boost/spirit/include/qi.hpp>
#include <fstream>
#include <iostream>
using namespace std;

int main () 
{
    ifstream myfile ("example.txt");

    std::string line;
    while ( std::getline(myfile, line) )
    {
        using namespace boost::spirit::qi;

        std::string::iterator b(line.begin()), e(line.end());

        int i = -1; std::string tail;
        if (phrase_parse(b, e, int_ >> *char_, space, i, tail))
            std::cout << "int: " << i << ", tail: " << tail << std::endl;
        // else // TODO handle error
    }
    return 0;
}

如果你真的必须将前三个字符作为整数,我现在坚持使用纯STL解决方案

答案 3 :(得分:0)

使用fscanf:

char str[256];
int  num = 0;

FILE *myFile = (FILE*) calloc(1, sizeof(FILE);
myFile = fopen("example.txt, "r");

while (fscanf(myFile, "%d %s\n", &num, str))
{
  printf("%d, %s\n", num str);
}

答案 4 :(得分:0)

我相信你假设MAX长度为100个字符。 char szInt[4];
strncpy(szInt, buffer, 3);
szInt[3] = 0;
buffer += 3;
int errCode = atoi(szInt);

errCode 你的int和缓冲区现在有你的字符串。