Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Apr 22nd, 2006, 4:45 PM   #1
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 12 programmingnoob is on a distinguished road
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)
programmingnoob is offline   Reply With Quote
Old Apr 22nd, 2006, 5:30 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 22 DaWei will become famous soon enoughDaWei will become famous soon enough
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.
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Apr 22nd, 2006, 5:57 PM   #3
hbe02
Hobbyist Programmer
 
hbe02's Avatar
 
Join Date: Mar 2006
Location: Lebanon
Posts: 164
Rep Power: 12 hbe02 is on a distinguished road
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..?
hbe02 is offline   Reply With Quote
Old Apr 22nd, 2006, 6:21 PM   #4
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 12 programmingnoob is on a distinguished road
Quote:
Originally Posted by hbe02
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..?
int c = fin.get()

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.
programmingnoob is offline   Reply With Quote
Old Apr 22nd, 2006, 6:35 PM   #5
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 887
Rep Power: 12 Jimbo is on a distinguished road
Quote:
when i read a text file ... if it encounters end of text file .. i want it to "skip" or should i say ignore?
This doesnt make sense. If you're at the end of the file, there's no skipping or ignoring to do. If you mean end of line, keep reading the file.
Quote:
then it will read /n
just a minor detail: its \n, but that can mess things up if you aren't careful.

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').
Jimbo is offline   Reply With Quote
Old Apr 22nd, 2006, 6:44 PM   #6
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 12 programmingnoob is on a distinguished road
Quote:
Originally Posted by Jimbo
This doesnt make sense. If you're at the end of the file, there's no skipping or ignoring to do. If you mean end of line, keep reading the file.

just a minor detail: its \n, but that can mess things up if you aren't careful.

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').
woopsie ... yeah i meant end of line!

and i cant use getline() function because i need to read charcater by character
programmingnoob is offline   Reply With Quote
Old Apr 22nd, 2006, 6:55 PM   #7
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 12 programmingnoob is on a distinguished road
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?
programmingnoob is offline   Reply With Quote
Old Apr 22nd, 2006, 7:14 PM   #8
Jimbo
Battle Programmer
 
Jimbo's Avatar
 
Join Date: Feb 2006
Location: Bellevue, WA, USA
Posts: 887
Rep Power: 12 Jimbo is on a distinguished road
Quote:
Originally Posted by programmingnoob
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 ...
Shouldn't it stop since "program" is a valid token, then read in the end of line, have an empty token to discard, and then read in the v?

and how does your scanner handle input if its not all valid tokens? i.e. for input:
*program
var=**
vvprovargram
Jimbo is offline   Reply With Quote
Old Apr 22nd, 2006, 7:17 PM   #9
programmingnoob
Hobbyist Programmer
 
Join Date: Feb 2006
Posts: 155
Rep Power: 12 programmingnoob is on a distinguished road
Quote:
Originally Posted by Jimbo
Shouldn't it stop since "program" is a valid token, then read in the end of line, have an empty token to discard, and then read in the v?

and how does your scanner handle input if its not all valid tokens? i.e. for input:
*program
var=**
vvprovargram
no it wont stop just b/c program is a valid token ... b/c
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
programmingnoob is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:32 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2017, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC