File IO
File IO
Okay i have been writing many stupid programs in C/C++ but i do not have a clue about doing File IO in C/C++. I have done it in "Microsoft Basic" and it is pretty easy, and also PHP. How do i go about this in C/C++. This includes everything from appending to readeding, ect, ect, ect
http://voidmain.kicks-ass.net/man/?docT ... =3%20fopen
http://voidmain.kicks-ass.net/man/?docT ... 3%20fclose
http://voidmain.kicks-ass.net/man/?docT ... =3%20fread
http://voidmain.kicks-ass.net/man/?docT ... 3%20fwrite
http://voidmain.kicks-ass.net/man/?docT ... =3%20fgets
http://voidmain.kicks-ass.net/man/?docT ... =3%20fputs
http://voidmain.kicks-ass.net/man/?docT ... 3%20fscanf
http://www.google.com/
:)
http://voidmain.kicks-ass.net/man/?docT ... 3%20fclose
http://voidmain.kicks-ass.net/man/?docT ... =3%20fread
http://voidmain.kicks-ass.net/man/?docT ... 3%20fwrite
http://voidmain.kicks-ass.net/man/?docT ... =3%20fgets
http://voidmain.kicks-ass.net/man/?docT ... =3%20fputs
http://voidmain.kicks-ass.net/man/?docT ... 3%20fscanf
http://www.google.com/
:)
http://www.cprogramming.com/tutorial.html
C++:
C Example:
You might want to do some googlin' and find out some dangers when using fgets (be careful about overflows etc). And of course these are just some very simple examples using straight text in files. You can also read/write binary data. There are millions of examples out there. I can't believe you could have done a google search. AND, have you ever heard of Linux? You should try it if you haven't already. Then you'll have the source code for thousands of applications that read/write data to files for great examples.
Google search terms:
c programming file i/o
c programming file i/o fopen example
etc
C++:
Code: Select all
#include <fstream.h>
#include <iostream.h>
int main() {
char str[10];
ofstream a_file("example.txt");
//Creates an instance of ofstream, and opens example.txt
a_file<<"This text will now be inside of example.txt";
//Outputs to example.txt through a_file
a_file.close();
//Closes up the file
ifstream b_file("example.txt");
//Opens for reading the file
b_file>>str;
//Reads one string from the file
cout<<str;
//Should output 'this'
b_file.close();
//Do not forget this!
}
Code: Select all
#include <stdio.h>
#include <stdlib.h>
int main() {
FILE *fp;
char line[100];
/* open "example.txt" for writing and reading */
if ((fp = fopen("example.txt", "w+")) ==NULL) {
printf("Error opening file\n");
return 1;
}
/* write a line of text to a file called "example.txt" */
fprintf(fp,"This is some text that will go into example.txt\n");
fprintf(fp,"This is some more text that will go into example.txt\n");
/* Reset file pointer to beginning of file */
fseek(fp,0L,SEEK_SET);
/* read/display "example.txt" */
while ( fgets(line,sizeof(line),fp) != NULL ) {
printf("%s",line);
}
/* close "example.txt" file */
fclose(fp);
return 0;
}
Google search terms:
c programming file i/o
c programming file i/o fopen example
etc