-2

I have a file in which I have the maximum length of the string on the first line and the string itself on the second line. It is an educational task so I can't use the <string> library.
I did this:

ifstream input;
input.open("input.txt");
int n;
input >> n;
char* str = new char[n];
input.getline(str, n); // This is the thing i want to get rid of.
input.getline(str, n);

And it works, but I have to call getline() twice, because after I get the length with input >> n; there is still a '\n' char after it on that line, and to go to the second line of the file I have to call getline(), that is the only solution that I came up with. Any help will be much appreciated!

  • `std::string s; input >> s; std::cout << n.length();` In general, using current C++ you should not have to use new/delete much, it is not even needed to read the length of the string first. (Even if a lot of older examples/books still show this) – Pepijn Kramer Jan 24 '23 at 19:00
  • "It is an educational task so I can't use library": what library? Do you mean you can't `#include `? – Fabio says Reinstate Monica Jan 24 '23 at 19:01
  • You say your code works, so what exactly is your question? Are you asking for other ways to achieve this? – Drew Dormann Jan 24 '23 at 19:02
  • 1
    You contradict yourself, std::ifstream (don't use `using namespace std;` by the way) is from the standard library too. So if you can use that then you can also use std::string. – Pepijn Kramer Jan 24 '23 at 19:03
  • (n.length() in my example should be s.length()) – Pepijn Kramer Jan 24 '23 at 19:07
  • Tweak to above example code: `std::string s; std::getline(input , s); std::cout << n.length();` is closer to original code. `>>` will read exactly one whitespace-delimited token. – user4581301 Jan 24 '23 at 19:08
  • @FabiosaysReinstateMonica yes, i wrote but it got removed for some reason, formatting issue probably. – Sasha Dynin Jan 24 '23 at 19:10
  • @PepijnKramer I think I made a complicated question, so here's the thing: I cant use string type, i have to use c-strings(char arrays). I have a file in which i have smth like 10 hello When i get 10 with input >> n; i'm still on the same line of the file and getline() doesn't work for the first time – Sasha Dynin Jan 24 '23 at 19:12
  • @SashaDynin you may still [edit] your question to clarify that you can use some standard library types, but not others. – Drew Dormann Jan 24 '23 at 19:13
  • 1
    Ahhhh. I've misread the question. You have [Why does std::getline() skip input after a formatted extraction?](https://stackoverflow.com/q/21567291/4581301) – user4581301 Jan 24 '23 at 19:15
  • @SashaDynin "It is an educational task so I can't use library" -- The `std::string` has been an official part of C++ for 25 years now. This is a stupid requirement, in this day and age. – PaulMcKenzie Jan 24 '23 at 19:29
  • 1
    This doesn't address the question, but get in the habit of initializing objects with meaningful values rather than default-initializing them and immediately overwriting the default values. In this case that means changing `ifstream input; input.open("input.txt");` to `ifstream input("input.txt");`. – Pete Becker Jan 24 '23 at 19:30
  • Sorry for the confusion, but I sill hope teachers will do a better job at learning C++. All in all , now that I understand the restrictions, why don't you use [`input.read(n);`](https://en.cppreference.com/w/cpp/io/basic_istream/read) to read exactly n bytes? (Do it into a buffer of n+1 size to add the trailing '\0') – Pepijn Kramer Jan 24 '23 at 19:45

1 Answers1

0

Thanks everyone, cin.ignore() did it for me. Now the code is better in my opninon.

input >> n;
cout << n << endl;
char* str = new char[n];
input.ignore();
input.getline(str, n);
  • 2
    You need to allocate n + 1 characters for a string of length n. C strings have an extra *nul terminator* character at the end. – john Jan 24 '23 at 19:19
  • 1
    Recommendation: don't trust a mere `ignore()`. See the example at the end of [this documentation page](https://en.cppreference.com/w/cpp/io/basic_istream/ignore) for how to make sure any crap on the end of the line is eliminated along with the newline. – user4581301 Jan 24 '23 at 19:20
  • Good advice, but OP can't use ``; I doubt `` will be okay. Anything else is a guess. – sweenish Jan 24 '23 at 19:28
  • @user4581301 thanks, then I'll std::ws, it works as well. Thanks for your replies! – Sasha Dynin Jan 24 '23 at 19:38
  • Does trailing whitespace count as part of the string? – Dúthomhas Jan 24 '23 at 20:13