read to end of line problem

Reply

Join Date: Oct 2003
Posts: 2
Reputation: one1082 is an unknown quantity at this point 
Solved Threads: 0
one1082 one1082 is offline Offline
Newbie Poster

read to end of line problem

 
0
  #1
Oct 10th, 2003
I have no clue why this isn't working! Basically, I've got my tree and
everything working, but now I need to fit it to the input the instructor
gave me. What I need to do is read the first part of a line and then
input each following term into the tree... My problem come with reading
until the end of the line. For some reason, it won't recognise the newline
'\n' character. Right now I'm using strings but I've created a little
sample program below that shows what I'm talking about:

my input looks like this:
I1 kim abelo xxxx abcd xxxy
I2 abelo timmy
D1 kim abelo
D2 timmy
T1
T2
ID
IS

What I'm thinking this code should do is print out that first line only
but it keeps going.

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main () {
  7. char temp;
  8.  
  9. ifstream infile("in.txt");
  10.  
  11. infile >> temp;
  12. while ((!infile.eof()) && (temp != '\n')) {
  13. cout << temp;
  14. infile >> temp;
  15. }
  16.  
  17. cout << endl;
  18.  
  19. return 0;
  20. }

output received:
I1kimabeloxxxxabcdxxxyI2abelotimmyD1kimabeloD2timmyT1T2IDIS


Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 1
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: read to end of line problem

 
0
  #2
Oct 10th, 2003
Why not use fgetc? I don't really use C++ constructs, so in C it would be (and I'm just writing this on the fly so expect some errors):

#include <stdio.h>
#include <stdlib.h>

int main() {

char myChar;
FILE *in = fopen("myfile.txt", "r"); // Open myfile.txt read-only
while((myChar=fgetc(in)) != EOF) {
if(myChar=='\n') printf("\n");
else printf("%c",myChar);
}
return 0;
}

File: myfile.txt

  1. this is
  2. data
  3. hehe

%./a.out
this is
data
hehe


If you want to break after the first line:

#include <stdio.h>
#include <stdlib.h>

int main() {

char myChar;
FILE *in = fopen("myfile.txt", "r"); // Open myfile.txt read-only
while((myChar=fgetc(in)) != EOF) {
if(myChar=='\n') {
printf("\n");
break;
} else printf("%c",myChar);
}
return 0;
}
Last edited by subtronic; Oct 10th, 2003 at 7:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2003
Posts: 46
Reputation: Dante Shamest is an unknown quantity at this point 
Solved Threads: 0
Dante Shamest's Avatar
Dante Shamest Dante Shamest is offline Offline
Light Poster

Re: read to end of line problem

 
0
  #3
Oct 10th, 2003
Replace

  1. infile >> temp;

with

  1. temp = infile.get() ;

That should do the trick.
Reply With Quote Quick reply to this message  
Join Date: Oct 2003
Posts: 2
Reputation: one1082 is an unknown quantity at this point 
Solved Threads: 0
one1082 one1082 is offline Offline
Newbie Poster

Re: read to end of line problem

 
0
  #4
Oct 11th, 2003
temp = infile.get() works perfectly. Thanks a ton. If you don't mind though could you explain the difference? I don't understand why one works and one doesn't. I appreciate it.

Tim
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 1
Reputation: kwvanderlinde is an unknown quantity at this point 
Solved Threads: 0
kwvanderlinde kwvanderlinde is offline Offline
Newbie Poster

Re: read to end of line problem

 
0
  #5
Mar 25th, 2008
The reason that
temp = infile.get()
works and
infile >> temp
does not is because the >> operator ignores whitespace and new lines. get() reads in every character, no matter what
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 1,602
Reputation: jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of jephthah has much to be proud of 
Solved Threads: 120
jephthah's Avatar
jephthah jephthah is offline Offline
Posting Virtuoso

Re: read to end of line problem

 
0
  #6
Mar 29th, 2008
Thank the gods.

Someone finally answered this question that we've all been struggling with this for 4 1/2 long years,

what a relief.

someone let Tim know he can mark this as "solved"
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC