Here's a reference page for stdio.h. Specifically, look up fgets and puts. If you have any more specific questions, feel free to ask them, but this page should give you a general idea.
Posts: 2437 | Registered: Apr 2005
| IP: Logged |
posted
gets is dangerous, but fgets has a parameter where you can put the buffer size, so there is much less danger.
Posts: 2437 | Registered: Apr 2005
| IP: Logged |
posted
But what I hate about "fgets" is that it reads the string including the carriage return. Also, if I remember correctly, if you read using a buffer less than the actual string length, the rest of the string still sits waiting to be read, unlike the above which will ignore the remainder of the line.
Posts: 3486 | Registered: Sep 2002
| IP: Logged |
posted
I've always thought of that as a handy feature of fgets, actually. But I guess it depends on what you're using it for.
Posts: 120 | Registered: Jun 2005
| IP: Logged |
Blayne Bradley
unregistered
posted
ok one thing to know the function another to know how to implement it, for somehting as simple as just reading and outputting lines of a file how would I do it? My memory shorted out on my for when my prof was explaioning this.
IP: Logged |
posted
I think you are misusing the terminology, there. I would be extremely surprised if your assignment is to 'implement' a get/put function; that's low-level stuff for compiler and API writers. What you are trying to do is use the function, and already somebody has very kindly linked you the documentation. Go read it.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
Blayne Bradley
unregistered
posted
it makes no sense to me though.
code:
char* fgets(char* s, int n, FILE* stream);
Copies characters from (input) stream stream to s, stopping when n-1 characters copied, newline copied, end-of-file reached or error occurs. If no error, s is NUL-terminated. Returns NULL on end-of-file or error, s otherwise.
like do I just do fgets(filename); and it will read it will read the first line?
wait is it say fgets( streamvar, MAX, filename);?
IP: Logged |
posted
a FILE* is returned by fopen. s is the buffer to write the data into (which should be created with something like char buffer[n] where n is big enough to store your data. The int n parameter is the size of your buffer, so that there isn't an overflow.
Posts: 2437 | Registered: Apr 2005
| IP: Logged |
Blayne Bradley
unregistered
posted
okays got it to work,magically thing called "google" on fgets, turns out I needed to use my variable "tline", make a point called pFile use fopen on "test.exe" and stuff blah easier to paste it.
posted
Incidentally, you should get used to thinking in loops. Almost everything is a loop. Loops are what separate markup languages like HTML from real Turing-complete languages with hair on their chests. You should also look up Turing completeness, it's a fascinating concept. Then look up brain****, and ponder the fact that it is provably Turing complete.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
Blayne Bradley
unregistered
posted
the lines from a fie and I got that to work, basically
code:
pfile = fopen("test.txt");
loop {
fgets( tline, max, pfile); puts(tline); }
fclose;
problem, at the last line it keeps on putting the last line and not exiting the rpgm, i have it so that if strlen(tline) > 0, loop. So if it hits an empty line it shuld exit but it aint.
IP: Logged |
Blayne Bradley
unregistered
posted
ok for some reason a blank line counts as being 1 char/number so strlen always reutrned 1 if it was a blank line.
IP: Logged |
posted
You don't want to use a strlen call to delimit the loop. Look at the return value of fgets.
code:
while(fgets(...))
Look here they tell you what kinds of things fgets can return. (Actually, judging from your example above it looks like you've already been there.)
Edit: read the docs more carefully. (or even this thread). fgets considers a new line a valid character so a blank line should have a length of one. (the '\n' char)
Posts: 1621 | Registered: Oct 2001
| IP: Logged |
posted
okay so I use fgetc o read in characters from a file.
quote:
doing and wrong-doing , there is a field. Furthermore, he asserted-
now part of the code is that if I hit a End of Word Event (EOWvnt) example ", .;-" then replace the first instance of it with a "#" an leave he rest alone.
however I am supposed to keep hypthenation in mind so:
if you have "wrong-doing" leave the hyphen alone.
Currently my code works under the assumtion of as soon as I hit a EOWvnt, cout << "#"; instead of the char.
and change the flag so that all next encounters of EOW chars are ignored and outputted as normal.
But how do I get it so that it ignores the first instance of "-" if there is a word next?
since it reads only one char at a time by the time I get to check if the next char is either a EOWvnt or a normal character the first hyphen would alrdy be either a '-' or a '#'.
posted
Since it's a special case, treat it special. There is no rule that says you can only use fgetc at the start of the loop.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
Blayne Bradley
unregistered
posted
ack why wont this work,
code:
#include <iostream>
using namespace std;
void main() {
cout << "hello world" << endl;
}
im getting unresolved external, i hate errors that arent descriptive.
I'm trying to make a test program for the function chfind but i notice that all my errors fall back to this for some reason if i comment everything out.
IP: Logged |
posted
Sounds like your compiler can't find the implementation (not the header file, but the actual binary) of iostream - at least, if you're really showing all your code, that's all I can think of. Check your path settings and whatnot.
Posts: 10645 | Registered: Jul 2004
| IP: Logged |
Blayne Bradley
unregistered
posted
okay i found the problem for sme reason when I created a new project i mustve chosen a wrong setting, i created a new project and works fine, I'm doing some pretty wierd things here.
Basically im thinking of using a function "chfind" provided by the teacher to look for what the next char is to see if its a letter or a hyphen or not.
In the end im confident it will work im only worried if im kinda deviating from my teachers specifications, as is I cant understand what he really wants chfind to do, all I know is I pass it the char I'm looking for and the target string and it returns a number, -1 or th position of n, n being the position of the char im looking for in the target string.
IP: Logged |