DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   read to end of line problem (http://www.daniweb.com/forums/thread1283.html)

one1082 Oct 10th, 2003 6:18 pm
read to end of line problem
 
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.

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  char temp;

  ifstream infile("in.txt");

  infile >> temp;
  while ((!infile.eof()) && (temp != '\n')) {
    cout << temp;
    infile >> temp;
  }
 
  cout << endl;

  return 0;
}

output received:
I1kimabeloxxxxabcdxxxyI2abelotimmyD1kimabeloD2timmyT1T2IDIS


Thanks in advance.

subtronic Oct 10th, 2003 6:57 pm
Re: read to end of line problem
 
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

this is
data
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;
}

Dante Shamest Oct 10th, 2003 10:29 pm
Re: read to end of line problem
 
Replace

infile >> temp;

with

temp = infile.get() ;

That should do the trick. :)

one1082 Oct 11th, 2003 5:04 pm
Re: read to end of line problem
 
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

kwvanderlinde Mar 25th, 2008 3:10 pm
Re: read to end of line problem
 
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

jephthah Mar 29th, 2008 4:08 am
Re: read to end of line problem
 
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"


All times are GMT -4. The time now is 4:14 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC