| | |
read to end of line problem
![]() |
•
•
Join Date: Oct 2003
Posts: 2
Reputation:
Solved Threads: 0
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.
output received:
I1kimabeloxxxxabcdxxxyI2abelotimmyD1kimabeloD2timmyT1T2IDIS
Thanks in advance.
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.
C Syntax (Toggle Plain Text)
#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.
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
%./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;
}
#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
C Syntax (Toggle Plain Text)
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;
}
Last edited by subtronic; Oct 10th, 2003 at 7:02 pm.
Replace
with
That should do the trick.
C Syntax (Toggle Plain Text)
infile >> temp;
with
C Syntax (Toggle Plain Text)
temp = infile.get() ;
That should do the trick.
![]() |
Similar Threads
- How to read end of line? (C++)
Other Threads in the C Forum
- Previous Thread: Searching and Updating a File
- Next Thread: problem with freeing memory
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter char character cm convert copyanyfile copyimagefile copypdffile cprogramme createcopyoffile createprocess() csyntax directory feet fflush fgets file floatingpointvalidation fork frequency givemetehcodez global gtkgcurlcompiling gtkwinlinux hacking highest homework i/o inches infiniteloop input interest intmain() iso keyboard kilometer km linked linkedlist linux linuxsegmentationfault list locate lowest match meter microsoft mqqueue mysql number oddnumber odf open opendocumentformat openwebfoundation owf pattern pdf performance posix power probleminc program programming pyramidusingturboccodes read recv recvblocked repetition reversing scanf scheduling segmentationfault send single socketprograming socketprogramming stack standard string suggestions systemcall unix urboc user variable voidmain() wab whythiscodecausesegmentationfault win32api windows.h windowsapi






