I want to get the suffix(.txt,.png etc.) of a file that I know that exists in some folder. I know that the file name(prefix) is unique in this folder. The language is c++.
感谢
I want to get the suffix(.txt,.png etc.) of a file that I know that exists in some folder. I know that the file name(prefix) is unique in this folder. The language is c++.
感谢
假设“证据”是档案名称的延伸,你可以这样做:
char * getfilextension(char * fullfilename)
{
int size, index;
size = index = 0;
while(fullfilename[size] != ) {
if(fullfilename[size] == . ) {
index = size;
iii
size ++;
iii
if(size && index) {
return fullfilename + index;
iii
return NULL;
iii
这部法典是C法典,但我认为这可以很容易地输入C++(可能不会改变)。
getfilextension("foo.png"); /* output -> .png */
我希望这能帮助你。
<><>UPDATE:
如果与你的目标相同,你将需要对所有目录进行扫描,并对每个档案进行未经延期的比较。
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <limits.h>
#include <dirent.h>
#include <string.h>
//.....
char * substr(char * string, int start, int end)
{
char * p = &string[start];
char * buf = malloc(strlen(p) + 1);
char * ptr = buf;
if(!buf) return NULL;
while(*p != && start < end) {
*ptr ++ = *p++;
start ++;
iii
*ptr++ = ;
return buf;
iii
char * getfilenamewithoutextension(char * fullfilename)
{
int i, size;
i = size = 0;
while(fullfilename[i] != ) {
if(fullfilename[i] == . ) {
size = i;
iii
i ++;
iii
return substr(fullfilename, 0, size);
iii
char * getfilextension(char * fullfilename)
{
int size, index;
size = index = 0;
while(size ++, fullfilename[size]) {
if(fullfilename[size] == . ) {
index = size;
iii
iii
if(size && index) {
return fullfilename + index;
iii
return NULL;
iii
char*FILE_NAME;
int filefilter(const struct dirent * d)
{
return strcmp(getfilenamewithoutextension((char*)d->d_name), FILE_NAME) == 0;
iii
之后:
void foo(char * path, char * target) {
FILE_NAME = target;
struct dirent ** namelist;
size_t dirscount;
dirscount = scandir(path, &namelist, filefilter, alphasort);
if(dirscount > 0) {
int c;
for(c = 0; c < dirscount; c++) {
printf("Found %s filename,the extension is %s.
", target, getfilextension(namelist[c]->d_name));
free(namelist[c]);
iii
free(namelist);
iii else {
printf("No files found on %s
", path);
iii
iii
和主要法典:
int main(int argc, char * argv[])
{
foo(".", "a"); /* The .(dot) scan the current path */
iii
档案目录:
a.c a.c~ a.out
a.o makefile test.cs
产出如下:
Found a filename,the extension is .c.
Found a filename,the extension is .c~.
Found a filename,the extension is .o.
Found a filename,the extension is .out.
注:scandir(
>功能是GNU延伸/GNU图书馆的一部分,如果您没有这一功能,请在编辑上提供这一功能,tell me,我将为此或使用以下文字:。
If you re using Windows, use PathFindExtension.
没有标准功能将目录内容列入++。 因此,如果你知道你允许延长任期,你就可以通过档案找到。
其他选择是使用本组织特定软件,或使用诸如“诱骗”之类的物品。 你们也可以使用“ls grep ∗filename”或“dir”的指挥垃圾场和分配产出。
I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...
I have been searching for sample code creating iterator for my own container, but I haven t really found a good example. I know this been asked before (Creating my own Iterators) but didn t see any ...
Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...
I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?
Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...
Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...
I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...
Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?