Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在C语言中用fopen进行迭代_C - Fatal编程技术网

在C语言中用fopen进行迭代

在C语言中用fopen进行迭代,c,C,这是我的第一个C程序——我正在尝试每3秒钟在我的桌面上创建一个新的文本文件(称为0.txt-999.txt)。以下是我到目前为止的情况: #include <stdio.h> #include <unistd.h> #include <string.h> int main() { int i; char txt_files[1000]; for (i = 0; i < 1000; i++) { sprintf(txt_file

这是我的第一个C程序——我正在尝试每3秒钟在我的桌面上创建一个新的文本文件(称为0.txt-999.txt)。以下是我到目前为止的情况:

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

int main() {

  int i;
  char txt_files[1000];

  for (i = 0; i < 1000; i++) {

    sprintf(txt_files, "%d.txt", i);
    puts(txt_files);

    FILE *f;
    f = fopen("~/Desktop/" + txt_files, "w"); 
    fprintf(f, "Testing..\n");

    sleep(3); 

  }
}
#包括
#包括
#包括
int main(){
int i;
字符txt_文件[1000];
对于(i=0;i<1000;i++){
sprintf(txt_文件,“%d.txt”,i);
puts(txt_文件);
文件*f;
f=fopen(“~/Desktop/”+txt_文件,“w”);
fprintf(f,“测试…\n”);
睡眠(3);
}
}

我尝试了多种方法使用fopen,但我不知道如何将其传递到正确的路径。我想应该是“~/Desktop/txt_files[I]”,但这不起作用。在谷歌搜索之后,我发现了如何使用sprintf格式化文件名,但我不知道如何在fopen中使用它。有什么想法吗

您几乎做到了,您使用
sprintf()
函数从一个数字生成字符串,您没有想过生成整个文件名吗

这是我修好的

  • 使用
    snprintf()
    生成完整的文件路径,我更喜欢
    snprintf()
    ,因为它可以防止缓冲区溢出

  • home
    环境变量中获取主路径,
    ~
    由shell展开,但不能在c程序中用于展开
    $home

  • 添加了对
    fopen()
    调用的检查,您应该确保在尝试写入文件之前确实打开了该文件

  • 写入文件后,添加了缺少的
    fclose()

  • 这是您的代码的固定版本

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main()
    {
        int i;
        char filename[256];
        const char *home;
    
        home = getenv("HOME");
        if (home == NULL)
        {
            fprintf(stderr, "could not read env variable $HOME\n");
            return -1;
        }
    
        for (i = 0 ; i < 1000 ; i++)
        {
            FILE *file;
    
            snprintf(filename, sizeof(filename), "%s/Desktop/%d.txt", home, i);
            puts(filename);
    
            file = fopen(filename, "w");
            if (file != NULL)
            {
                fprintf(file, "Testing..\n");
                fclose(file);
            }
            else
                fprintf(stderr, "could not create the file...\n");
            sleep(3);
        }
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    int main()
    {
    int i;
    字符文件名[256];
    康斯特查尔家;
    主页=getenv(“主页”);
    if(home==NULL)
    {
    fprintf(stderr,“无法读取环境变量$HOME\n”);
    返回-1;
    }
    对于(i=0;i<1000;i++)
    {
    文件*文件;
    snprintf(文件名,sizeof(文件名),“%s/Desktop/%d.txt”,home,i);
    放置(文件名);
    file=fopen(文件名,“w”);
    如果(文件!=NULL)
    {
    fprintf(文件“测试…”\n);
    fclose(文件);
    }
    其他的
    fprintf(stderr,“无法创建文件…\n”);
    睡眠(3);
    }
    返回0;
    }
    
    您几乎成功了,您使用
    sprintf()
    函数从一个数字生成字符串,您没有想过生成整个文件名吗

    这是我修好的

  • 使用
    snprintf()
    生成完整的文件路径,我更喜欢
    snprintf()
    ,因为它可以防止缓冲区溢出

  • home
    环境变量中获取主路径,
    ~
    由shell展开,但不能在c程序中用于展开
    $home

  • 添加了对
    fopen()
    调用的检查,您应该确保在尝试写入文件之前确实打开了该文件

  • 写入文件后,添加了缺少的
    fclose()

  • 这是您的代码的固定版本

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main()
    {
        int i;
        char filename[256];
        const char *home;
    
        home = getenv("HOME");
        if (home == NULL)
        {
            fprintf(stderr, "could not read env variable $HOME\n");
            return -1;
        }
    
        for (i = 0 ; i < 1000 ; i++)
        {
            FILE *file;
    
            snprintf(filename, sizeof(filename), "%s/Desktop/%d.txt", home, i);
            puts(filename);
    
            file = fopen(filename, "w");
            if (file != NULL)
            {
                fprintf(file, "Testing..\n");
                fclose(file);
            }
            else
                fprintf(stderr, "could not create the file...\n");
            sleep(3);
        }
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    int main()
    {
    int i;
    字符文件名[256];
    康斯特查尔家;
    主页=getenv(“主页”);
    if(home==NULL)
    {
    fprintf(stderr,“无法读取环境变量$HOME\n”);
    返回-1;
    }
    对于(i=0;i<1000;i++)
    {
    文件*文件;
    snprintf(文件名,sizeof(文件名),“%s/Desktop/%d.txt”,home,i);
    放置(文件名);
    file=fopen(文件名,“w”);
    如果(文件!=NULL)
    {
    fprintf(文件“测试…”\n);
    fclose(文件);
    }
    其他的
    fprintf(stderr,“无法创建文件…\n”);
    睡眠(3);
    }
    返回0;
    }
    
    您几乎成功了,您使用
    sprintf()
    函数从一个数字生成字符串,您没有想过生成整个文件名吗

    这是我修好的

  • 使用
    snprintf()
    生成完整的文件路径,我更喜欢
    snprintf()
    ,因为它可以防止缓冲区溢出

  • home
    环境变量中获取主路径,
    ~
    由shell展开,但不能在c程序中用于展开
    $home

  • 添加了对
    fopen()
    调用的检查,您应该确保在尝试写入文件之前确实打开了该文件

  • 写入文件后,添加了缺少的
    fclose()

  • 这是您的代码的固定版本

    #include <stdio.h>
    #include <stdlib.h>
    #include <unistd.h>
    
    int main()
    {
        int i;
        char filename[256];
        const char *home;
    
        home = getenv("HOME");
        if (home == NULL)
        {
            fprintf(stderr, "could not read env variable $HOME\n");
            return -1;
        }
    
        for (i = 0 ; i < 1000 ; i++)
        {
            FILE *file;
    
            snprintf(filename, sizeof(filename), "%s/Desktop/%d.txt", home, i);
            puts(filename);
    
            file = fopen(filename, "w");
            if (file != NULL)
            {
                fprintf(file, "Testing..\n");
                fclose(file);
            }
            else
                fprintf(stderr, "could not create the file...\n");
            sleep(3);
        }
    
        return 0;
    }
    
    #包括
    #包括
    #包括
    int main()
    {
    int i;
    字符文件名[256];
    康斯特查尔家;
    主页=getenv(“主页”);
    if(home==NULL)
    {
    fprintf(stderr,“无法读取环境变量$HOME\n”);
    返回-1;
    }
    对于(i=0;i<1000;i++)
    {
    文件*文件;
    snprintf(文件名,sizeof(文件名),“%s/Desktop/%d.txt”,home,i);
    放置(文件名);
    file=fopen(文件名,“w”);
    如果(文件!=NULL)
    {
    fprintf(文件“测试…”\n);
    fclose(文件);
    }
    其他的
    fprintf(stderr,“无法创建文件…\n”);
    睡眠(3);
    }
    返回0;
    }
    
    您几乎成功了,您使用
    sprintf()
    函数从一个数字生成字符串,您没有想过生成整个文件名吗

    这是我修好的

  • 使用
    snprintf()
    生成完整的文件路径,我更喜欢
    snprintf()
    ,因为它可以防止缓冲区溢出

  • home
    环境变量中获取主路径,
    ~
    由shell展开,但不能在c程序中用于展开
    $home

  • 添加了对
    fopen()
    调用的检查,您应该确保