![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
c++ reading from a file ...
so i am reading from a file character by character, coz i have to ...
i am taking care of the eof thing as follows... if (c == -1) {tell the program to stop reading from the file} now how do i tackle newline? if (c == 10) { do what?} how do i tell program to start reading from the next line? also 10 is the ascii code for '/n' .. right? (i'm using windows) |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
In the first place, 'c' won't necessarily equal -1 when you hit EOF unless you are reading a file written in Window's text mode, but reading it in binary mode. EOF is a condition, not content, except as mentioned above. Some read mechanisms will stash the condition in there, however. You don't happen to mention the type of input function you are using, which would seem to be a necessary requisite in the minds of anyone with 37 posts. Woops, just noticed, you haven't reached 37....
0x10 is the ASCII code for newline, alright, but you might get 0x13 for carriage return/enter, depending upon how the file was written. Dare I suggest that you produce (for our inspection) some actual code, what you expect of it, how, precisely, it is failing to meet your expectations, and some actual error/warning messages, if any. Time to get on the stick and quit expecting mind-reading, Mr. Noob.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#3 |
|
Hobbyist Programmer
Join Date: Mar 2006
Location: Lebanon
Posts: 148
Rep Power: 3
![]() |
just a few questions:
is the variable "c" the character you are reading from file?? if yes.. are you using .get(c) to fetch each character from the file..? do you want to read a new line when the file pointer still has not finished reading the line itself..? |
|
|
|
|
|
#4 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
Quote:
where fin is the ifstream variable ... when i read a text file ... if it encounters end of line .. i want it to "skip" or should i say ignore? that character and move on to the next line ... so ... text: blah yes it would read 'h' and then it will read /n and then it will be move on to reading b... i hope i am making sense? Last edited by programmingnoob; Apr 22nd, 2006 at 6:43 PM. |
|
|
|
|
|
|
#5 | ||
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Quote:
Quote:
Have you looked into the getline() function at all? assuming you're wanting to read a whole line/word/delimted amount. And your code would be easier to read (IMHO) if you compared the values to the characters rather than the ASCII values (i.e. 'h' and '\n'). |
||
|
|
|
|
|
#6 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
Quote:
and i cant use getline() function because i need to read charcater by character |
|
|
|
|
|
|
#7 |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
i guess let me explain my prob a lil more in detail...
i'm writing a scanner ... and if the current character it is looking at is not a token, then it should keep adding the letters to a string s unless it either encounters space, eof, new line or a character that is a valid token. after this loop, the value i have for s, i can further see if that s represents a token ... let me illustrate it will an example: (*, program, var, = are valid tokens) *program var= var now the first line ... my program will look at * and consider it as a valid token... then it will look at p ... then add r, o, g, r, a, m to the string s which means the string will be "program" (using a while loop)... it should stop adding a new character to the string coz it encountered end of line ... so it should see whether "program " is a valid token etc and then started reading from v ... i also used fin.putback() after the while loop so that it can put back the space, new line, eof, or the character token back into the stream .. so that it's available for next execution. does my prob make sense? |
|
|
|
|
|
#8 | |
|
Battle Programmer
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 770
Rep Power: 3
![]() |
Quote:
and how does your scanner handle input if its not all valid tokens? i.e. for input: *program var=** vvprovargram |
|
|
|
|
|
|
#9 | |
|
Hobbyist Programmer
Join Date: Feb 2006
Posts: 155
Rep Power: 3
![]() |
Quote:
programblah might lead to two tokens: "program" and "blah" .. program being the valid token and blah being an invalid token ... and i dont want that to happen. bc programblah is clearly an invalid token. and to answer your q about how i treat invalid tokens: they go under the category of "error" tokens |
|
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|