1

In order to solve Euler Project 8 without resorting to a "Big Number" library, I would like to read the separate digits in an txt.-file to separate spots in an array. The digits in the txt.-file are arranged as follows:

094239874......29837429837 [50 of them],

192319274......12837129873 [50 of them]

such that there are in total 20 lines with 50 digits, all separated by enters. So I am trying to write a program which writes the first digits to the first spot in the array and continues this process (paying attention to the spaces) until the 1000th digit. I have tried finding solutions to this problem in tutorials and elsewhere online, but I cannot make it work for this specific example. Up to now I have something like

 int main() {

 int array[999];
 string trial[999];

 ofstream myfile;
 myfile.open ("example.txt");
 
 for(i=1 ; i<=1000 ; i++) {
 myfile >> trial;
 // Somehow convert string to int as well in this loop?
 }
Community
  • 1
  • 1
Funzies
  • 129
  • 1
  • 1
  • 8
  • Remember that in a text file, digits are just characters like any other. And it's the same in strings, a character in a string (or file) is a character no matter if it's a letter, digit, punctuation or whitespace. – Some programmer dude Jan 13 '15 at 08:22
  • Also, in the small code snippet you show, you declate `trial` as an array of 999 strings. Are you trying to create one string with 999 characters? – Some programmer dude Jan 13 '15 at 08:23
  • And lastly, why declare arrays of 999 entries and attempt to read 1000 entries from the file? There's a size mismatch here, as well as arrays starting their index at zero (so you can't use `i` in the loop without subtraction). – Some programmer dude Jan 13 '15 at 08:25
  • Change to `int array[1000];` (the syntax needs the number of elements including the 0-indexed one, not the largest index you'll use), then in your loop ``char c; myfile >> c; array[i] = c - '0';`. Would be better with error handling, but that's another story.... – Tony Delroy Jan 13 '15 at 08:27

4 Answers4

1

You can read your file line by line, then add your digits to an array like this way:

// out of your loop
std::vector<int> digits;
// in your loop
std::string buffer = /*reading a line here*/;
for (auto c : buffer) {
    digits.push_back(c - '0');
}

Furthermore, STL containers are better than C-style arrays (std::vector / std::array).

vincentp
  • 1,433
  • 9
  • 12
1

I suppose this is what you're looking for

int main(void)
{
    unsigned char numbers[20][50];
    FILE *pf = fopen("example.txt", "r");
    for(int i = 0; i < 20; i++)
    {
        // read 50 characters (digits)
        fread(&numbers[i], 1, 50, pf);
        // skip line feed character);
        fseek(pf, 1, SEEK_SET);
    }
    fclose(pf);

    // conversion from ascii to real digits by moving the digit offset (subtracting by the first digit char in ascii table)
    for(i = 0; i < 20*50; i++)
        ((unsigned char*)numbers)[i] -= (unsigned char) '0';

    // the digits are now stored in a 2-dimensional array (50x20 matrix)

    return 0;
}
Noxoreos
  • 23
  • 5
1

You can try to do it this way (first read the file contents into a string, then convert each char to an int, btw you should use a vector<int> instead of a raw array):

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using namespace std;

int main()
{
    string str;
    string total;
    ifstream a_file("data.txt");

    while (getline(a_file, str))
        total += str;

    vector<int> vec;

    for (int i = 0; i < total.size(); i++)
    {
        char c = total[i];
        int a = c - '0';
        vec.push_back(a);
    }     
}
w.b
  • 11,026
  • 5
  • 30
  • 49
  • I have some trouble understand some parts of your code. Maybe you can enlighten me. 1) when does getline(a_file,str) become zero, i.e., when does the whileloop stop? 2) what happens when you do total+= str? 3) Does total.size() automatically give the full number of characters in the txt.file 4) what does vec.push_back(a) do? 5) I do not really see where you tell the program that the lines of digits are separated by enters. Many thanks! – Funzies Jan 13 '15 at 10:43
  • `std::getline` returns a stream on which it was called (in this case `ifstream`, file stream on the data.txt file), and while loop tests the validity of this stream, it tests true if there is more data to read – w.b Jan 13 '15 at 10:53
  • `total += str` appends `str` to `total`, it's a shortcut for `total = total + str;` and `string::size()` returns the number of characters in the `string`: http://www.cplusplus.com/reference/string/string/size/ – w.b Jan 13 '15 at 10:59
0

This approach will not work. According to this question, any built-in integral type is likely to be too small to represent the value of a number with 50 decimal digits.

Community
  • 1
  • 1
Codor
  • 17,447
  • 9
  • 29
  • 56
  • that is why I am trying to read every digit of the file into a new position in the array. So I am only storing single digit numbers into the array. After that I can perform the operations I want to. – Funzies Jan 13 '15 at 08:24