Author Topic: What code is it used to read & write text files in C?  (Read 13456 times)

cesar7160

  • Guest
What code is it used to read & write text files in C?
« on: May 20, 2009, 04:28:09 pm »
What code is it used to create, read & write text files in C?

Where can i find a tutorial or a guide?

cesar7160

  • Guest
Re: What code is it used to read & write text files in C?
« Reply #1 on: May 20, 2009, 05:44:03 pm »
I found it, here it is:


#include <stdio.h>

int main ( void )

{

static const char filename[] = "file.txt";

FILE *file = fopen ( filename, "r" );

if ( file != NULL )

{

char line [ 128 ]; /* or other suitable maximum line size */

 

while ( fgets ( line, sizeof line, file ) != NULL ) /* read a line */

{

fputs ( line, stdout ); /* write the line */

}

fclose ( file );

}

else

{

perror ( filename ); /* why didn't the file open? */

}

return 0;

}

Offline ollydbg

  • Developer
  • Lives here!
  • *****
  • Posts: 5910
  • OpenCV and Robotics
    • Chinese OpenCV forum moderator
Re: What code is it used to read & write text files in C?
« Reply #2 on: May 20, 2009, 05:46:21 pm »
Off topic here... :shock:
Take care for being locked.
If some piece of memory should be reused, turn them to variables (or const variables).
If some piece of operations should be reused, turn them to functions.
If they happened together, then turn them to classes.

Offline Jenna

  • Administrator
  • Lives here!
  • *****
  • Posts: 7255
Re: What code is it used to read & write text files in C?
« Reply #3 on: May 21, 2009, 06:37:48 am »
Off topic here... :shock:
Take care for being locked.

Locked !