More i/o problems
Well, it wasn''t too long ago I posted about the same topic, but I need some more help with file i/o in c++.
I have been searching everywhere for some good information on FILE fopen() fclose() etc. But I can''t find anything anywhere! The only thing I have been able to get is the short page in the vc++ help file which is a reference page and difficult to learn from.
Does anyone know where I can find some good info, like a tutorial or something? Or can anyone help me out?
Thanks!
Okay, first off, you need to get a good C reference book (I recommend "C A Reference Manual" by samuel harbison and guy ste ele jr.) File I/O with stdio.h is pretty easy, to open a file you do this command:
FILE *fp = fopen("filename", "r");
where fp is a pointer of FILE type, filename is the name of the file, and the r means read. There is also w, which is write and a, which is append. You can append a + symbol to any of them to mean multiple modes, but you can look those up on the net. To read from a file (assuming your using straight C), you use a fscanf:
fscanf(fp, "%i", &variable);
where again, fp is the file pointer, and the rest of the fscanf should look familiar. To write to a file use fprintf, which has the same structure as the fscanf. Also, after you write or read to/from a file, the file pointer is updated so that it points to a different part of the file, so you can read 20 characters, and the next character you read is going to be in position 21.
To test for end of file (EOF), you use this command:
feof(fp);
fp is the file pointer. It returns a non-zero value if it detects an EOF at the current position, else it returns a zero. So if you want to read from a file until you hit eof you''d use a while loop:
while (!feof(fp))
/* Code */
}
And at last, to close a file you use fclose:
fclose(fp);
Again, where fp is the file pointer. I don''t think there are any limits on how many files you can have open at the same time, but it used to be something like 3, and you should try to keep only a few files open at a time. Also, if you need to find positions in files, use fseek, and fsetpos. If you need to write or read only a single character, use fputc and fgetc. I hope I helped a lil bit, if u need any more help just e-mail me (whiteasian@cloxs.org).
--Cloxs (Jared Klumpp)
FILE *fp = fopen("filename", "r");
where fp is a pointer of FILE type, filename is the name of the file, and the r means read. There is also w, which is write and a, which is append. You can append a + symbol to any of them to mean multiple modes, but you can look those up on the net. To read from a file (assuming your using straight C), you use a fscanf:
fscanf(fp, "%i", &variable);
where again, fp is the file pointer, and the rest of the fscanf should look familiar. To write to a file use fprintf, which has the same structure as the fscanf. Also, after you write or read to/from a file, the file pointer is updated so that it points to a different part of the file, so you can read 20 characters, and the next character you read is going to be in position 21.
To test for end of file (EOF), you use this command:
feof(fp);
fp is the file pointer. It returns a non-zero value if it detects an EOF at the current position, else it returns a zero. So if you want to read from a file until you hit eof you''d use a while loop:
while (!feof(fp))
/* Code */
}
And at last, to close a file you use fclose:
fclose(fp);
Again, where fp is the file pointer. I don''t think there are any limits on how many files you can have open at the same time, but it used to be something like 3, and you should try to keep only a few files open at a time. Also, if you need to find positions in files, use fseek, and fsetpos. If you need to write or read only a single character, use fputc and fgetc. I hope I helped a lil bit, if u need any more help just e-mail me (whiteasian@cloxs.org).
--Cloxs (Jared Klumpp)
I tried to post earlier, but the forums were screwed up. I can email you the old Borland Programmer''s Guide (help file) if you''d like. It covers (with examples) all standard C and C++ functions (including all of stdio.h, not Windows though, that''s in another file). If you''d like me to email it to you, just ask.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
I''d be very interested in that help file, Null and Void.
email - spiderman@ghg.net
email - spiderman@ghg.net
A really good reference book to buy, one that will keep forever, is "A Book on C" by Al Kelley and Ira Pohl. It will serve you well!
Anyone who wants it, go here. I think those are the correct files, but it is hard to tell, since some files work for me even without their counterparts (so it''s hard to test)
. It is the file called BCHelp.zip. I will take it back down in a couple days.
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
![](smile.gif)
"Finger to spiritual emptiness underlying everything." -- How a C manual referred to a "pointer to void." --Things People Said
![Resist Windows XP''s Invasive Production Activation Technology!](http://www.crosswinds.net/~druidgames/resist.jpg)
http://www.gdarchive.net/druidgames/
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement