How to Read Groups of Integers from a File, Line by Line in C++

How to read groups of integers from a file, line by line in C++

It depends on whether you want to do it in a line by line basis or as a full set. For the whole file into a vector of integers:

int main() {
std::vector<int> v( std::istream_iterator<int>(std::cin),
std::istream_iterator<int>() );
}

If you want to deal in a line per line basis:

int main()
{
std::string line;
std::vector< std::vector<int> > all_integers;
while ( getline( std::cin, line ) ) {
std::istringstream is( line );
all_integers.push_back(
std::vector<int>( std::istream_iterator<int>(is),
std::istream_iterator<int>() ) );
}
}

Read integers AND characters from file line by line in C

Here is a minimal exaple on how to address every single character in your file and transforming into integer if one is met. I am sure you could try and adapt this code to your needs.
The code in addition jumps newlines and EOF.

To better understand how this works have a look at the standard ASCII.

#include<stdio.h>

int main(){
FILE * handle;
handle = fopen("data.txt","r");
char var;

while(var!=EOF){
var=fgetc(handle);
if(var>47&&var<58){
printf("Value: %d",var);
printf(" Integer: %d \n",var-48);
}
else if(var!=10 && var!=-1){
printf("Value: %d",var);
printf(" Char or other: %c\n",var);

}
}

}

Output:

Value: 49 Integer: 1 

Value: 49 Integer: 1

Value: 49 Integer: 1

Value: 48 Integer: 0

Value: 66 Char or other: B

Value: 85 Char or other: U

Value: 49 Integer: 1

Value: 85 Char or other: U

Value: 48 Integer: 0

Value: 85 Char or other: U

Value: 48 Integer: 0

Value: 85 Char or other: U

Read integers from file - line by line

std::ifstream file_handler(file_name);

// use a std::vector to store your items. It handles memory allocation automatically.
std::vector<int> arr;
int number;

while (file_handler>>number) {
arr.push_back(number);

// ignore anything else on the line
file_handler.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Read file line by line using ifstream in C++

First, make an ifstream:

#include <fstream>
std::ifstream infile("thefile.txt");

The two standard methods are:

  1. Assume that every line consists of two numbers and read token by token:

    int a, b;
    while (infile >> a >> b)
    {
    // process pair (a,b)
    }
  2. Line-based parsing, using string streams:

    #include <sstream>
    #include <string>

    std::string line;
    while (std::getline(infile, line))
    {
    std::istringstream iss(line);
    int a, b;
    if (!(iss >> a >> b)) { break; } // error

    // process pair (a,b)
    }

You shouldn't mix (1) and (2), since the token-based parsing doesn't gobble up newlines, so you may end up with spurious empty lines if you use getline() after token-based extraction got you to the end of a line already.

How to read a text file with number intervals? C++

This is a parsing problem. There are tools to help you with parsing, but they have to be learned. Sometimes it's best to just dive in and write the code. It's not so hard if you are methodical about it. The trick is to make the structure of your code match the structure of the grammar you are parsing.

The following code assumes you want to store your integers in a vector. It just prints out the contents of that vector at the end.

The following code does no error checking of the input at all. In a real program that would be a serious problem. I'll leave you to add the error checking.

#include <iostream>
#include <sstream>
#include <string>
#include <vector>
#include <algorithm>
#include <cstdint>

typedef uint64_t int_type;

void parse(const std::string& line, std::vector<int_type>& numbers)
{
numbers = { 0 };
size_t i = 0;
while (i < line.size())
{
char ch = line[i++];
if (ch == '[')
{
// start a group
std::string group;
while (line[i] != ']')
{
char lo = line[i++];
if (line[i] == '-')
{
++i;
char hi = line[i++];
while (lo <= hi)
{
group += lo;
++lo;
}
}
else
{
group += lo;
}
}
++i;
// add the new numbers implied by the group
std::vector<int_type> new_numbers;
for (auto num : numbers)
{
for (auto digit : group)
new_numbers.push_back(10 * num + (digit - '0'));
}
numbers.swap(new_numbers);
}
else
{
std::transform(numbers.begin(), numbers.end(), numbers.begin(),
[=](auto n) { return 10 * n + (ch - '0'); });
}
}
}

int main()
{
std::string data = "12345678[5-8][0-9]\n"
"3684567150\n"
"329465207[023456789]\n"
"132478026[13]\n"
"778941351[02-689]\n"
"84364575[89][0-9]\n";
std::istringstream input(data);
std::string line;
while (getline(input, line) && line.size() >= 0)
{
std::vector<int_type> numbers;
parse(line, numbers);
for (auto num : numbers)
{
std::cout << num << '\n';
}
std::cout << '\n';
}
}

Only briefly tested, but it seems to work.



Related Topics



Leave a reply



Submit