TOP

open

File * fp=fopen(fname,"r");
if(fp==NULL) {
    fprintf(stderr,"%s open failed\n",fname);
    exit(EXIT_FAILURE);
}

read a line

while(NULL != fgets(buf,BUFSIZE,fp)){
}

rename

int rename(const char *OLD, const char *NEW); /* stdio.h */
alt
use link count of hard linked files.
link(old,new);unlink(old);

exist(file)

stat
  • if not exist, ENOENT
man stat
int stat(const char *path, struct stat *buf);

struct stat {
    dev_t     st_dev;     /* ファイルがあるデバイスの ID */
    ino_t     st_ino;     /* inode 番号 */
    mode_t    st_mode;    /* アクセス保護 */
    nlink_t   st_nlink;   /* ハードリンクの数 */
    uid_t     st_uid;     /* 所有者のユーザ ID */
    gid_t     st_gid;     /* 所有者のグループ ID */
    dev_t     st_rdev;    /* デバイス ID (特殊ファイルの場合) */
    off_t     st_size;    /* 全体のサイズ (バイト単位) */
    blksize_t st_blksize; /* ファイルシステム I/O での
                             ブロックサイズ */
    blkcnt_t  st_blocks;  /* 割り当てられた 512B のブロック数 */
    time_t    st_atime;   /* 最終アクセス時刻 */
    time_t    st_mtime;   /* 最終修正時刻 */
    time_t    st_ctime;   /* 最終状態変更時刻 */
};

get file size

not 'fopen'ed yet
#include <sys/stat.h> /* if missing, error"aggregate `stat stat_buf' has incomplete type" will appear*/
struct stat buf;
stat("makefile", &buf); 
printf("file size=%ld\n", (long)buf.st_size);
'fopen'ed.
int fd ;
struct stat buf;
FILE* fp = fopen( "makefile", "rb" );
fd = fileno(fp) ;
fstat(fd,&buf) ;
printf("size = %ld\n",(long)buf.st_size);
or
long filesize;
FILE* fp = fopen( "makefile", "rb" );
fseek( fp, 0L, SEEK_END );
filesize = ftell( fp );
fclose( fp );
printf("file size=%ld\n", filesize);

remove

int remove(char *FILENAME); // stdio.h

fseek

int fseek( FILE *stream, long offset, int origin );
void rewind( FILE *stream );
  • origin
SEEK_CURCurrent position of file pointer.
SEEK_ENDEnd of file.
SEEK_SETBeginning of file.
  • if(successful) return 0;

ftell

long ftell( FILE *stream );
int fgetpos( FILE *stream, fpos_t *pos ); // returns 0 if successful
  • get current position

C++/CLR

open

       sr = gcnew StreamReader(fname) ;
        if(sr->CurrentEncoding == System::Text::Encoding::UTF8){
            // no bomb utf
            sw = gcnew StreamWriter(tempFileName,false,gcnew System::Text::UTF8E
ncoding(false));
        } else {
            sw = gcnew StreamWriter(tempFileName,false,sr->CurrentEncoding);
        }
        sw->NewLine = "\n";

read a line

String^ line;
while (( line = sr-&gt;ReadLine() ) &#33;= nullptr) {
}

rename

public:static void File::Move (String^ sourceFileName, String^ destFileName);

exist

public: static bool File::Exists (String^ path);

remove

public:
static void File::Delete (String^ path);

C++ stream

open

#include <fstream>
#include <iostream>
std::fstream fr( fname.c_str(), std::ios::in ); // text mode open , use ios::binary for "rb"
if( fr.fail() ){
    cerr << "failed to open" << fname << endl;
    exit(1);
}
std fstream * fw = new std::fstream( workFname.c_str(), std::ios::out| ios:: binary );

read a line

 while( !fr.eof() ){
     getline(fr,line);
     if( !fr.fail() ){
           // ...
     }
 }

rewind

fr.clear(); // reset eof flag
fr.seekg(0,std::ios::beg);

管理人/副管理人のみ編集できます