1

I want to read the 3rd line from a text file in as a string, convert it to a long long int, and return that value.

The data on the 3rd line of the text file is 1234567890123456

long long int File::Getline3(int user1, int user3)
{
    std::string filename = std::to_string(user1);

    std::ifstream fin(filename + ".txt");
    fin.getline (line1, 5);
    fin.getline (line2, 5);
    fin.getline (line3, 20);
    fin.close();

    // convert line 3 to a string called str
    const char *line3;
    std::string str(line3);

    // convert str to long long int called user3
    long long int strtoll(const char *nptr, char **endptr, int base);
    char* endptr = NULL;
    user3 = strtoll(str.c_str(), &endptr, 10);

    return user3;
}

The comments are in to show what I think I'm doing, however I am probably wrong (I'm new to pointers).

I get an "unresolved external" error when I try to build my program.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
user3001499
  • 811
  • 4
  • 16
  • 32
  • any answers with explanations of whats being done please – user3001499 Feb 19 '14 at 17:02
  • Code does not compile – Raxvan Feb 19 '14 at 17:03
  • it doesn't, error is "unresolved external" – user3001499 Feb 19 '14 at 17:04
  • 1
    Ummm, why are you declaring strtoll instead of `#include`-ing `cstdlib`? And why do you send in as argument `int user3` if you don't use it, just assign to it and return? – Xarn Feb 19 '14 at 17:05
  • Are you sure of the requirements, are you sure your requirements say "read the 3rd line from a text file in as A char". I think the intent might be to read it in as a string of chars, then perform your own conversion of that string into its integer form as would be held by the computer. – trumpetlicks Feb 19 '14 at 17:06
  • I've updated the question/title to make this more clear, since you *can't* read a line of text as a 'char' and since you really mean as a string. – crashmstr Feb 19 '14 at 17:08
  • I decided that the best way was to use a char, I've never heard of a string of chars before, will look into that now – user3001499 Feb 19 '14 at 17:08
  • @user3001499: You _are_ using strings of `char`s... Don't confuse `char` with `char*`. Which book are you using? – Lightness Races in Orbit Feb 19 '14 at 17:22
  • using a mixture of internet research (probably a bad idea in hindsight) and I'm currently just starting to use The Complete Reference C++ by Herbert Schildt (just arrived in the post 2 days ago) – user3001499 Feb 19 '14 at 17:26

3 Answers3

5
long long int strtoll(const char *nptr, char **endptr, int base);

This line declares a function. It means that when you call strtoll a few lines down, you're going to be calling that function as it's the most obvious candidate. However, you never defined it.

Instead you meant to call std::strtoll, which is defined (by the standard library), and which will be found through your presumed using namespace std directive if you do not hide it by falsely declaring this non-existent function of your own with the same name. :)

Simply remove the excess declaration:

// convert str to long long int called user3
char* endptr = NULL;
user3 = strtoll(str.c_str(), &endptr, 10);

You have a similar issue with your const char* line3, which you declare inside the function, never assign anything to, then construct a string out of. That's undefined; the pointer is uninitialised. Assuming you have some line3 data member (along with your line1 and line2), again you're hiding it with a local variable of the same name.

Finally, passing user3 in by value is utterly pointless if you're just writing to it and returning it. Remove that parameter.

Putting all this together, your code should probably look like:

// convert line3 to long long int
char* endptr = NULL;
return strtoll(line3, &endptr, 10);

In short, be less declaration-happy!

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • It now compiles at least, however the returned value is still not what is on line 3 of the text file, but 10 random digits, any suggestions? – user3001499 Feb 19 '14 at 17:35
  • also strtoll didnt work, through up an error saying it was undefined, so had to change it to strtol, which maybe why it isn't working correctly? – user3001499 Feb 19 '14 at 17:36
  • I'm not going to iteratively debug your program line by line in comments... but if you look at the manual for [`strtoll`](http://en.cppreference.com/w/cpp/string/byte/strtol) you'll see it's in C++ only since C++11 (though if you were on Linux you should have had it anyway, via POSIX). – Lightness Races in Orbit Feb 19 '14 at 17:37
0

https://connect.microsoft.com/VisualStudio/feedback/details/758053/missing-strtold-strtoll-strtoull-functions-from-stdlib-h

A work around for Visual C++ that does not have these included.

0
long long int File::Getline3(int user1)
{
    std::string filename = std::to_string(user1);

    std::ifstream fin(filename + ".txt");

    std::string line1, line2; // place to actually store the lines

    getline(fin, line1); // read a line
    getline(fin, line2); // read another line

    long long int number_on_line_3; // somewhere to store the number
    fin >> number_on_line3;         // store the number
    fin.close();

    return number_on_line_3;        // return the number
}
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88