c - Get files and operate on their file size -
i've algorithm
1. scan files in directory (done using c)
2. file size of current file in loop (done using c)
3. if less 8 kb , store next immediate file name in array (seems there no back upwards associative array in c)
i've done in php due unforeseeable events, needs written in c. did go through quite few tutorials on c , speaking underestimated time thought require basics right.
after quite considerable time, managed hands on code lists out files in directory.
#include <dirent.h> #include <stdio.h> #include <conio.h> int main(void) { char path = "d:\\ffmpeg\\bin\\frames\\"; dir *d; struct dirent *dir; char test; d = opendir("d:\\ffmpeg\\bin\\frames\\"); if (d) { while ((dir = readdir(d)) != null) { printf("%s\n", dir->d_name); } closedir(d); } getchar(); return(0); }
now it's obvious current file in loop represented dir->d_name
. thing i'm stuck take , append "d:\\ffmpeg\\bin\\frames\\"
path becomes "d:\\ffmpeg\\bin\\frames\\somename.jpg"
this help me direct path of file. 1 time have required info move step 2. problem i'm facing right string concatenation. tried strcat()
didn't work out.
so i'm looking is
while ((dir = readdir(d)) != null) { // merge "path" , "dir->d_name" similar // "d:\\ffmpeg\\bin\\frames\\somename.jpg" }
any help, suggestions?
the recommended solution in plain c snprintf
:
char buf[max_path]; snprintf(buf, sizeof(buf), "%s%s", path, dir->d_name);
don't utilize strcat()
or strncat()
.
if using msvc, c implementation 24 years out of date, , snprintf()
not available. options are:
use _snprintf()
, buf[sizeof(buf)-1] = '\0';
workaround.
use c++ , std::string
.
use cygwin.
c string
No comments:
Post a Comment