File IO

Discuss Programming
Post Reply
X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

File IO

Post by X11 » Tue Jan 21, 2003 10:03 pm

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


X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 » Tue Jan 21, 2003 10:25 pm

thanks, seems simlar to that in php.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Tue Jan 21, 2003 10:28 pm

X11 wrote:thanks, seems simlar to that in php.
No, PHP is similar to that in C. :)

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 » Wed Jan 22, 2003 5:52 am

Say could you write up some small examples im just having a tiny bit of trouble understanding some of it.

User avatar
Void Main
Site Admin
Site Admin
Posts: 5716
Joined: Wed Jan 08, 2003 5:24 am
Location: Tuxville, USA
Contact:

Post by Void Main » Wed Jan 22, 2003 12:10 pm

http://www.cprogramming.com/tutorial.html
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!

}
C Example:

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;
}
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

X11
guru
guru
Posts: 676
Joined: Sun Jan 19, 2003 11:09 pm
Location: Australia
Contact:

Post by X11 » Thu Jan 23, 2003 6:16 am

Yea after reading those man pages i understood some source code much better.

Thanks!

Post Reply