-1

I'm running my code and give inputs via a .txt file(./a.out<input.txt ), but it seems it does not recognize end of file.

when I copy the contents in ubuntu it works. I think that files were created in MAC.

EOF is like CTRL+C in terminal so it should stop executing the code but the last command continuously is parsing as input. I can not change the file (It is test file and the format is unchangeable.). Can anyone please help me with this problem? I have attached my code, but I think that is the file problem.

EDIT: I found the solution. By adding

if(!getline(cin, s)){
   break;
}

or similar lines, the problem will be fixed!

Maryam
  • 119
  • 1
  • 1
  • 6
  • 2
    A [mcve] producing your problem(s) belong in your question. I don't see any c++ code, so its going to be kinda hard to tell you what is possibly wrong with it. – WhozCraig Oct 27 '19 at 10:09
  • 1
    Fristly, much of this depends on the terminal you use. Traditionally, Control-C sends SIGINT or SIGTERM (look it up, I'm too lazy ATM). Triggering EOF is rather Control-D. – Ulrich Eckhardt Oct 27 '19 at 10:10
  • I added my entire code. But I think It relates to the file format. As I mentioned when I copy the contents in ubuntu text file, there is no problem. – Maryam Oct 27 '19 at 10:12
  • In the terminal I am using, CTRL+C force the code stop running. – Maryam Oct 27 '19 at 10:16
  • `while (!fin.eof())` [is considered wrong](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons) and may cause some problems, since you never check if `fin.get(my_character);` is successfull. – Lukas-T Oct 27 '19 at 10:22
  • I have no fin. I mean I do not open a file in my code. I am giving the inputs as a file in terminal. I mean there is no input.txt in code. If i could open the file in the code, yes that would be the correct answer. But I have no fstream fin! – Maryam Oct 27 '19 at 10:27
  • It is definitely not a problem of the input file. Ot is a problem of your code. I found at least 2 endless loops. Additionally, the code seems to be very overcomplicated. What do you want to do? Can you specify the task? And what is the content of your data file. If you show this to me, then I will provide a complete solution to you. – A M Oct 27 '19 at 10:30
  • 1
    @Maryam So you see, that you have much code there that is irrelavant to the question. Please Create a **Minimal** Reproducible Example. – Lukas-T Oct 27 '19 at 10:32
  • I found the solution and I added it in EDIT part. Thanks. :) – Maryam Oct 27 '19 at 11:59
  • @Maryam Maybe not remove all the code from your question. After the last edit, it can become rather meaningless for another reading it for the first time in the future, since the original code is missing. Also comments made here in the comment field can lose there meaning if the question is edited too much. Please make sure that your question still has meaning to future readers.. – Håkon Hægland Oct 27 '19 at 12:08

1 Answers1

0

I found the solution. By adding

if(!getline(cin, s)){
   break;
}

or similar lines, the problem will be fixed!

Maryam
  • 119
  • 1
  • 1
  • 6