Welcome, Guest. Please login or register.
Did you miss your activation email?
Your Ad Here
Pages: [1]   Go Down
  Print  
Author Topic: [C]File writing/reading example  (Read 1100 times)
0 Members and 1 Guest are viewing this topic.
jurka
Staff
Member
*
*

Reputation: 5
Offline Offline
Posts: 188
Referrals: 0
Activity
0%

Awards
« on: August 07, 2009, 09:16:57 AM »

Writing to file is really simple in C there's only three functions(fopen, fprintf, fclose) which you should know and one structure(FILE). This code snippet creates a file with 0 - 100 lines of numbers.

Code
GeSHi (c):
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char **argv) {
  4.        FILE *fp;
  5.        char *filename = "database";
  6.  
  7.        // Open file for writing
  8.        fp = fopen(filename, "w");
  9.  
  10.        // Check for problems with opening file
  11.        if (fp == NULL) {
  12.                printf("Problem with opening file %s\n", filename);
  13.                return 1;
  14.        }
  15.  
  16.        int i;
  17.        for (i = 0; i <= 100; i++) {
  18.                // Write to file similar to function printf
  19.                fprintf(fp, "%d\n", i);
  20.        }
  21.  
  22.        fclose(fp);
  23.  
  24.        return 0;
  25.  
  26. }
  27.  
  28.  
Created by GeSHI 1.0.7.20

And the reading is also easy new function is scanf() and constant EOF.

Code
GeSHi (c):
  1. #include <stdio.h>
  2.  
  3. int main(int argc, char **argv) {
  4.        FILE *fp;
  5.        char *filename = "database";
  6.  
  7.        // Open file for reading
  8.        fp = fopen(filename, "r");
  9.  
  10.        // Check for problems with opening file
  11.        if (fp == NULL) {
  12.                printf("Problem with opening file %s\n", filename);
  13.                return 1;
  14.        }
  15.        int temp;
  16.  
  17.        // EOF stands for end of file, so it checks if
  18.        // it's not already in the end
  19.        while(fscanf(fp, "%d", &temp) != EOF) {
  20.                // Make some arithmetics
  21.                temp += 10;
  22.                printf("%d\n", temp);
  23.        }
  24.  
  25.        // Close file
  26.        fclose(fp);
  27.  
  28.        return 0;
  29.  
  30. }
  31.  
  32.  
Created by GeSHI 1.0.7.20
« Last Edit: August 07, 2009, 09:33:54 AM by jurka » Logged
Javaforums.net/forum :: A community for software developers
« on: August 07, 2009, 09:16:57 AM »

Your Ad Here
 Logged
trey
Member
*

Reputation: 0
Offline Offline
Posts: 16
Referrals: 0
Activity
0%

Awards
« Reply #1 on: December 07, 2009, 09:30:20 PM »

Eh, well you covered some of the formatted IO functions (fscanf and fprintf), but there are plenty of other IO functions:

fread(void*, size_t, size_t, FILE*)
fgetc(FILE*) (or getc(FILE*) if you wish to use it as a macro)
fgets(char*, int, FILE*)

Those are only a couple off the top of my head, but there are a few others for general stdin IO. You may also want to explain formatted IO a bit, as far as what the different conversion specifiers mean (%i, %s, %d, %x, etc.)
Logged
Javaforums.net/forum :: A community for software developers
   


 Logged
Pages: [1]   Go Up
  Print  
 
Jump to: