2011-05-16 78 views
0

我有一个文本文件的标准读取,但我需要的是一行读取的行的前3个字符作为int和行的其余部分作为字符串上一行一行的基础上。我把下面的代码放在示例文本中。C++读取整数和字符串的文本文件

谢谢

#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; 
} 
+0

什么样的文字?而且该代码不会像你所问的那样做,而且在任何情况下都是错误的。 – 2011-05-16 10:57:34

回答

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); 
} 
+1

呃? 'calloc()'的目的是什么?总之,OP指出只有第一个*三个字符*形成整数,它不会被空格分开。 – Nim 2011-05-16 11:04:48

+0

这将只读取字符串到第一个空格。所以如果行是“1堆栈溢出”,str只会包含“stack” – 2011-05-16 11:07:06

+3

标签C++,任何人? – sehe 2011-05-16 11:07:54

1

检查this和计算器this

无论您使用的sscanf你的缓冲区(假设你的字符串,如果NULL终止)指定格式字符串像这样:"%d%s",或者你使用 operator<<std::stringstream

注意:如果您的字符串包含空格,应使用 “%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; 
0

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

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

1

哦,其实我建议升压灵(齐),见下文稍后再例如

#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; 
    } 

只是为了好玩,这里是一个更灵活的升压基础的解决方案:

#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解决方案

+0

ARGH ..你已经完全否定了我的答案! :)我没有提供所有的代码,只是伪...啊很好... – Nim 2011-05-16 11:14:19

+0

增强解析为基础的例子添加好措施 – sehe 2011-05-16 11:37:46

2

这样的事情(伪鳕鱼e,我确定你可以弄清楚真正的操作!)

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... 
    } 
} 
相关问题