小编akm*_*mal的帖子

如何验证文件在Windows中是否有名称有效?

是否有一个Windows API函数,我可以传递一个字符串值,将返回一个值,指示文件名是否有效?

我需要验证文件名是否有效,并且我正在寻找一种简单的方法来实现它,而无需重新发明轮子.我正在直接使用C,但目标是Win32 API.

如果内置没有这样的功能,我将如何编写自己的功能?是否存在Windows用于确定文件名有效性的通用算法或模式?

c windows winapi filenames path

10
推荐指数
1
解决办法
5770
查看次数

内存泄漏问题

我有一个简单的程序,它创建一个线程,并在此线程结束时等待,然后程序也结束.当我用C(gcc)编译器编译这个程序,并用valgrind检查它时,没有问题,但是当我用C++(g ++)编译器编译它,并检查valgrind时,它表明我的程序有内存泄漏.可能是什么问题呢?

这是我的节目,

#include <pthread.h>
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>


unsigned char b = 0;


void* threadfunc1( void *pVoid )
{
    while( b == 0 )
    {
    usleep(10000);
    }
    pthread_exit(0);
}



int main(void)
{

    int status;
    pthread_attr_t tattr;
    pthread_t   thread1;

    status = pthread_attr_init(&tattr);
    status = pthread_attr_setdetachstate(&tattr,PTHREAD_CREATE_JOINABLE);
    status = pthread_attr_setscope(&tattr,PTHREAD_SCOPE_SYSTEM);

    if( pthread_create( &thread1, &tattr, threadfunc1, NULL ) != 0 )
    {
        exit(1);
    }

    usleep(1000000);
    b = 1;
    pthread_join( thread1, NULL);
    usleep(2000000);

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

这是结果,当我使用g ++编译它,并检查valgrind …

c c++ pthreads

8
推荐指数
2
解决办法
822
查看次数

为什么在分叉时终端和文件之间有不同的输出?

我正在学习如何工作fork(),我有一些问题.

请考虑以下代码:

#include <stdio.h>
#include <unistd.h>

int main()
{
   int i;

   for(i = 0; i < 5; i++)
   {
      printf("%d", i);

      if((i%2)==0)
         if(fork())
            fork();
   }
}
Run Code Online (Sandbox Code Playgroud)

当我输出到终端时,我得到了我期望的结果(即:)0,1,1,1,2,2,2,....但是当我输出到文件时,结果完全不同:

  • 案例1 :(输出到终端,例如:)./a.out:

    结果是: 0,1,1,1,2,2,2,...

  • 案例2:(输出到文件,例如:./a.out > output_file)

    结果是: 0,1,2,3,4,0,1,2,3,4,0,1,2,3,4,...

为什么会这样?

c fork

4
推荐指数
2
解决办法
146
查看次数

是否有良好的参数检查策略?

而不是用if条件检查每个输入指针参数if( in != NULL ),有更好的方法来做到这一点.一种方法是创建一些宏,比如

#define CHECK_ARG(x) if(!(x)) { return 0; }
Run Code Online (Sandbox Code Playgroud)

在代码中它看起来像这样

int some_func( int * first_arg, int * second_arg )
{
    CHECK_ARG( first_arg );
    CHECK_ARG( second_arg );

    // some operations
    //
}
Run Code Online (Sandbox Code Playgroud)

在我看来这是一个很好的方法,但在添加之前,我想知道有没有更好的方法呢?

提前致谢!

c argument-passing

2
推荐指数
1
解决办法
1042
查看次数

是否可以在linux中创建具有固定大小的文件?

我需要一个函数,它将在linux中创建一个固定大小的文件.功能像truncatefopen,fseek,fclose,不是解决方案,因为,他们将用零填充打开的文件,但它没有必要,我没有时间这个.那么是否有一些功能,它只会打开一个固定长度的文件而不是填充缓冲区?

提前致谢.

c linux file-io

1
推荐指数
1
解决办法
1281
查看次数

当执行`ls * .c`时,内部会发生什么?

最近,我对Linux内部产生了非常大的兴趣,目前正在尝试了解它们的工作原理。

我知道当我打字 ls

  • opendir() -函数被调用;
  • readdir() -为目录数据存储中的每个目录条目调用的函数;
  • stat() -如果需要,可以调用函数以获得有关文件的其他信息。

如果我缺少某些东西或有错,请纠正我。

对我来说,神秘的部分是文件名扩展(globbing)。

我比较了 strace ls

open(".", O_RDONLY|O_NONBLOCK|O_DIRECTORY|O_CLOEXEC) = 3
fstat(3, {st_mode=S_IFDIR|0755, st_size=270336, ...}) = 0
getdents(3, /* 14 entries */, 32768)    = 440
getdents(3, /* 0 entries */, 32768)     = 0
close(3)                                = 0
fstat(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 1), ...}) = 0
write(1, "2q.c  ds.c  fglob  fnoglob\n", 272q.c  ds.c  fglob  fnoglob
Run Code Online (Sandbox Code Playgroud)

并且strace ls *.c

stat("2q.c", {st_mode=S_IFREG|0664, st_size=0, ...}) = 0
lstat("2q.c", {st_mode=S_IFREG|0664, st_size=0, ...}) = 0
stat("ds.c", {st_mode=S_IFREG|0664, st_size=0, ...}) …
Run Code Online (Sandbox Code Playgroud)

linux shell wildcard linux-kernel

1
推荐指数
1
解决办法
133
查看次数