0

I want to read only the characters from the first line of a file and then stop. The following code is not working in Codeblocks because the output file is empty. What did I do wrong?

#include <fstream>
using namespace std;
ifstream cin ("test.in");
ofstream cout ("test.out");
char s;
int main()
{
while (cin>>s)
if (s=='\n')
{
cout<<"end of line"; return 0;
}
return 0;
}
Abacus
  • 1
  • 1
  • 2
    You cannot catch an end of line like that. Have a look at `getline` – Damien May 19 '20 at 12:13
  • Try `std::string line; std::getline(std::cin, line);` Here is a full example: [https://en.cppreference.com/w/cpp/string/basic_string/getline](https://en.cppreference.com/w/cpp/string/basic_string/getline) – drescherjm May 19 '20 at 12:14
  • Thanks. Getline is not working because the no. of characters on that line is too high and I'm running out of the time limit for the code to run. – Abacus May 19 '20 at 12:16
  • What is the size of your line ? – Damien May 19 '20 at 12:25
  • 1000000 characters – Abacus May 19 '20 at 12:37
  • You can preallocate the string before you start. From my example put line.reserve(1000000); before std::getline(). With that said this may require a different solution because they may time smaller inputs. – drescherjm May 19 '20 at 14:43

1 Answers1

1

There are many issues with your code, but the one that stops it working is that >> cannot be used to read whitespace characters, they are just ignored by this operator, so s will never equal '\n'.

Fortunately wanting to read a single line is a very common requirement and there's already a function getline which does exactly that. Here's your program rewritten to use getline and with all the other various issues fixed.

#include <fstream>
#include <string>

int main()
{
    std::ifstream in("test.in");
    std::ofstream out("test.out");
    std::string line;
    if (getline(in, line))
        out << "end of line\n"; 
    return 0;
}
john
  • 85,011
  • 4
  • 57
  • 81
  • Thanks, but.getline is not working because the no. of characters on that line is too high and I'm running out of the time limit for the code to run – Abacus May 19 '20 at 12:20
  • 1
    @Abacus It won't be getline that is causing your time limit to be exceeded, it will be the rest of your code. – john May 19 '20 at 12:20
  • @Abacus And in any case what makes you think that reading characters one at a time would be any faster? – john May 19 '20 at 12:21
  • Thank you for answering, I'll try again with getline. However, there is no other way to read characters one by one until the end of the line? – Abacus May 19 '20 at 12:27
  • @Abacus You can use `in.get()` which will read exactly one character, even if it's a whitespace character. – john May 19 '20 at 12:27
  • And how do you stop get() at the end of the line? – Abacus May 19 '20 at 12:28
  • A TLE is most likely caused by some other part of your code. Reading character by character if anything will be slower than reading a line at a time. – drescherjm May 19 '20 at 12:38
  • @Abacus Just as you were `if (s == '\n')` – john May 19 '20 at 12:38