![]() |
|
#1
|
|||
|
|||
C++ file I/OHello i wa swondering could anyone help me out. I'm new to file I/O and im trying to read in a file name given by the user with some names in it do various things to it and print it out to the screen. My problem is the file gets read but it only prints out a single chracter at a time. So if my file had
SOME THING IS IN THE FILE I would want that printed to the screen however it prints S O M E T H I N G and so on. This is the way I read in the file. CPP / C++ / C Code:
Last edited by dsmith : 08-Mar-2004 at 10:58.
Reason: Fixed C-syntax highlighting
|
|
#2
|
||||
|
||||
|
Hi Cronox. Welcome to GIDForums. I have moved your post into the C/C++ help forum as the other forum is restricted for posting tutorials or coding samples and is not for asking for help with a program. I also enclosed the code with the bbcode flags to c-syntax highlight it. It really helps with the readability. See the sticky post at the top of this forum for more details.
Thanks, d |
|
#3
|
||||
|
||||
|
I seen no definition for array1 so I don't know how big it is.
I see no write() function, so can't tell if there's a probelm there either. It looks like your input routine works correctly the way you have it written, but what you wrote may not be what you want. Do you in fact want the entire file in one big buffer or each line in separate buffers? |
|
#4
|
|||
|
|||
|
for array1 i have it at 2000
I wanna read the lines in seperate buffers. The write function is what I'm having problems with Right now i have it as a big switch statement where each case outputs certain words if something is read so if the file has.. int thing = 1 I would get INT ID (an identifier for thing) EQ (for the equal symbol NUM (for 1) I'm trying to ensure its reading the line correctly so I need to read int by itself then thing by itself and so on. |
|
#5
|
|||
|
|||
|
As we have seen im new at this and In response to some requests Im gonna put the rest of the code in here and explain what im trying to do. I'm trying to implement a lexical analyzer and given a file containing info like
Program { int in1 =5; } My program would give PROG LBRACKET (For the { ) INT IDEN (identifier in1) EQ (for =) NUM (for 5) SEMMICOLON RBRACKET the full code im using CPP / C++ / C Code:
Last edited by dsmith : 08-Mar-2004 at 18:33.
Reason: Use [c] & [/c] for c syntax highlighting
|
|
#6
|
||||
|
||||
|
Hi Cronox. Enclose your code in [c] and [/c] tags to highlight C code properly.
This is a pretty complex problem and I think your approach of just trying to read a file and verifying that it is read correctly as a first step is what I would do. First I don't see any reason why you are reading this char by char and storing it into a big buffer. I think I would do a loop like this:
I would recommend using the fgets statement and only processing one line at a time. This way your program can read in *any* size of file (right now it is limited to 2k) and only has to have storage for a single line. Your program also uses quite a few global variables. I am not going to say that you should never use a global variable, but they should be the exception, not the rule. Once a line is read in, I would pass it to another function that would split out a "token" (not identify it) and pass this token to another function that would identify it. It would do this until it reached '\n' or '\r' or NULL. I think if you break this down into several steps it may be easier. |
|
#7
|
|||
|
|||
|
Ok so i've never used an fget before would you be able to tell from the code whether it would require I make grand changes in my read or write functions or would I only need to make small changes. Thanks again for the help.
|
|
#8
|
||||
|
||||
|
Quote:
Adding an fgets would not make great changes to your code in general, but it also probably is not going to help greatly with your current problems. It *should* help to simplify your code though. You main read loop would be CPP / C++ / C Code:
This would require a #define of MAX_SIZE (probably about 200) and in the main function define read_string as: CPP / C++ / C Code:
For a description of fgets see this post. Once you are sure that the string is being read correctly, then go on and try to parse the string for the tokens. |
|
#9
|
|||
|
|||
|
So after a look over after that post with the fget info. Would I have to include something like fput into my writeToken function or if I successfully get it to read the file does it look like it would output with my current write function
Thanks again. |
|
#10
|
||||
|
||||
|
No, I think all your writeToken function does is output the token to the screen. Fputs would put it out to a file. Is this what you wanted to do?
Also, if you want to shorten your write routine you could place all of your token strings into a static array like: CPP / C++ / C Code:
CPP / C++ / C Code:
Takes a bit to set up, but can simplify your code. |
Recent GIDBlog
Welcome to Baghdad by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| CD burner wont burn!! | robertli55 | Computer Hardware Forum | 1 | 18-Jun-2004 10:53 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 11:28 |
| [Tutorial] Standard I/O | aaroncohn | C Programming Language | 20 | 27-Feb-2004 21:07 |
| read specified range of lines from file | etron | C Programming Language | 8 | 18-Feb-2004 08:04 |
| How Do i get php to find out the file type of a file for me? | viperman95833 | MySQL / PHP Forum | 2 | 08-Mar-2003 09:48 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The