0

Here is code to take input from text file. I used charecter wise reading. When I print B, it prints extra line after 'rao' only.

My text file input is :

1 singh
2 sen singh rao singh pal sen jain sen de rao 
3

Here is code

int main() {
  ifstream in;
  in.open( "Text.txt" );

  string s;
  while(getline(in,s)){
    char x;
    x=in.get();
    if(x=='1'){
      string s;
      in>>s;
    }

    if (x=='2'){
      char c;
      c=in.get();
      c=in.get();
      do{
        string A;
        string B;

        do{
          A=A+c;
          c=in.get();
        } while(c!=' ');

        c=in.get();

        do{
          B=B+c;
          c=in.get();
        } while(c!=' '&&c!='1'&&c!='2'&&c!='3'&&c!='4'&&c!='5'&&c!='6'&&c!='7');

        cout<<B<<endl;
      }

      while(c!='1'&&c!='2'&&c!='3'&&c!='4'&&c!='5'&&c!='6'&&c!='7');
      if(c!='1'||c=='2'||c=='3'||c=='4'||c=='5'||c=='6'||c=='7'){
        in.putback(c);
      }
    }
  }
}
Morten Kristensen
  • 7,412
  • 4
  • 32
  • 52
  • 2
    You're concatenating `endl` after every row. Of course you will end up with a finishing new line – Itay Sep 14 '13 at 14:34
  • You can work with a regular `while` instead of `do while` and print the `endl` at the loop's beginning – Itay Sep 14 '13 at 14:37
  • even if i do not use endl the output is :"singhsinghsensenrao" with a line break – user2779366 Sep 14 '13 at 14:57
  • Suggestion, instead of comparing for all those `!=`, try something like: `unsigned int value; in >> value; } while ((value > 0) && (value < 8));` – Thomas Matthews Sep 14 '13 at 17:35
  • well, here is better method to convey Q.: I took text input as 1 nikhil 2 amit nikhil arpit amit ramesh tushar 3 (3 is written in new line) Click here to see snapshot: s24.postimg.org/kugjbp11h/utkarsh_debug.png – Nikhil Chilwant Sep 14 '13 at 17:52

2 Answers2

2

std::endl inserts a newline character and flushes the stream. If you want to just flush use std::flush but to be honest most of the time you need neither.

A. H.
  • 942
  • 10
  • 20
  • And what will you use instead? – Itay Sep 14 '13 at 14:35
  • @italy most of the time you don't need to flush – A. H. Sep 14 '13 at 14:35
  • It's Itay not Italy. But what is your alternative to `std::endl`? You said `most of the time you need neither` – Itay Sep 14 '13 at 14:36
  • And why is it better than `std::endl`? – Itay Sep 14 '13 at 14:39
  • @itay std::cout is flushed anyway after extraction from std::cin or output to std::cout or program termination. There are some special cases in which you would need to explicitly flush. usually if you are doing logging in a program that may terminate abnormally (crash). Also on many implementations std::cout is line buffered and will flush on '\n' anyway. Explicitly flushing when not required neeedlesly degrades performance. [cppreference](http://en.cppreference.com/) has good notes on these situations I suggest you look them up for further information – A. H. Sep 14 '13 at 14:40
  • Thanks! I'll look at the link you gave me. Anyway one can understand from your answer that there's no use for `std::endl`. I asked what is the alternative and you and Alan said '\n'. Is it any more efficient or something? – Itay Sep 14 '13 at 14:43
  • 2
    @itay you generally don't want to interfere with how a stream is buffered. That being said there **are** special cases where you need/ would be safer with an explicit flush. – A. H. Sep 14 '13 at 14:47
  • So, does using `std::endl` interferes also to the stream and it's advisable to use "\n" instead? – Itay Sep 14 '13 at 14:48
  • 3
    @Itay `stream << std::endl` is equivalent to `stream << '\n' << std::flush`. That is, it's a combined command for "insert newline and flush the stream." – Angew is no longer proud of SO Sep 14 '13 at 14:54
  • but i do not understand why all of u are concerned about the endl ,because without the endl also it automatically breaks the line. – user2779366 Sep 14 '13 at 15:06
  • Downvote. This doesn't answer the question at all. He doesn't want to flush, he wants a newline there, but the problem is that there is another newline into B. – DarioP Sep 14 '13 at 15:28
  • @DarioP there problem is an extra newline and he uses std::cout << B << std::endl; not I want an extra new line – A. H. Sep 14 '13 at 15:29
  • @A.H. Yes, he wants it all the time that the cycle is executed, but not the last. This is just because the last time a newline is already in B. Please take a look at my answer to have a better view of the problem. I have to admit that this program is pretty weird. – DarioP Sep 14 '13 at 15:34
  • well, here is better method to convey Q.: I took text input as 1 nikhil 2 amit nikhil arpit amit ramesh tushar 3 (3 is written in new line) Click here to see snapshot: http://s24.postimg.org/kugjbp11h/utkarsh_debug.png – Nikhil Chilwant Sep 14 '13 at 17:50
  • better I put my input as: 1 nikhil [newLine]2 amit nikhil arpit amit ramesh tushar [newLine]3 – Nikhil Chilwant Sep 14 '13 at 17:58
0

First of all you may want to revise your code as if you forget the empty space at the end of the line in the input file, you enter into an infinite cycle here:

do{
  A=A+c;
  c=in.get();
} while(c!=' ');

If the last char in the line is a ' ' it's fine: you exit from this cycle but with the c=in.get(); that follows you load the char newline into c.

The while cycle over the string B first of all loads c (which is a newline char) into the empty string B. Then you read the char 3 from the file and so you exit the cycle.

Now it's clear that the cout prints two new lines: one in B and the other one coming from endl.

Maybe it is clearer if you consider that you input file is like this:

1 singh[newline]2 sen singh rao singh pal sen jain sen de rao [newline]3

A fast solution comes putting this line right before the cout:

if (B=="\n") B.clear();

I'm not sure about the objective of the code so it's hard to me to propose a better design.

DarioP
  • 5,377
  • 1
  • 33
  • 52
  • check out for purpose (make sure to read comments as well) http://stackoverflow.com/questions/18801526/how-to-search-in-this-tree?noredirect=1#comment27731867_18801526 – Nikhil Chilwant Sep 14 '13 at 17:55