0

I have the standard reading in of a text file but what I need is the first 3 characters of a line to be read in as int and the remainder of the line as a string on a line by line basis. I've put the code below with the example text.

#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;
}
kaya3
  • 47,440
  • 4
  • 68
  • 97
DragonFly
  • 19
  • 4
  • What example text? And that code doesn't do anything like what you are asking about, and is in any case wrong. –  May 16 '11 at 10:57

5 Answers5

2

Something like this (pseudo code, I'm sure you can figure out the real operations!)

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...
  }
}
Nim
  • 33,299
  • 2
  • 62
  • 101
1

Check this and this from stackoverflow.

Either you use sscanf on your buffer (supposing your string if NULL terminated) specifying a format string like this: "%d%s", or you use operator<< from std::stringstream.

NB: In case your string include white spaces, you should use "%d%n" instead of "%d%s" with sscanf, like here:

     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;
Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123
1

Oh, I'd actually recommend Boost Spirit (Qi), see below later for an example

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

Just for fun, here is a more flexible Boost based solution:

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

If you really must have the first three characters as integers, i'd stick with the pure STL solution for now

sehe
  • 374,641
  • 47
  • 450
  • 633
  • argh.. you've completely negated my answer!!! :) I did not provide all the code, just pseudo... ah well... – Nim May 16 '11 at 11:14
0

Use 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);
}
Jmoney38
  • 3,096
  • 2
  • 25
  • 26
  • 1
    eh? what's the purpose of the `calloc()`? Anways, the OP states that only the first *three characters* form the int, it's *not* separated by a space.. – Nim May 16 '11 at 11:04
  • this would read the string only to the first whitespace. so if the line was "1 stack overflow", str would only contain "stack" – Marius Bancila May 16 '11 at 11:07
0

I believe you are assuming MAX length of line is 100 chars. char szInt[4];
strncpy(szInt, buffer, 3);
szInt[3] = 0;
buffer += 3;
int errCode = atoi(szInt);

errCode has your int and buffer has your string now.

Mayank
  • 5,454
  • 9
  • 37
  • 60