【问题标题】:Creating a program that asks the user for a name and creates 10 files with names serialized using C [closed]创建一个程序,要求用户输入名称并创建 10 个名称使用 C 序列化的文件 [关闭]
【发布时间】:2019-05-16 14:40:01
【问题描述】:

我正在尝试解决文件处理问题,但我想不出一种方法来做到这一点。任何帮助,将不胜感激! 需要这样的东西:

#include<stdio.h>
int main()
{
        char string[10];
        FILE *fp1;

        printf("Enter the string");
        scanf("%s", string);

        fp1 = fopen(string, "w");


        /---- 


        fclose(fp1);
        return 0;
}

我不知道如何获取序列化文件 :( 我以为我可以制作 10 个 FILE* fptr 然后再做,但我不知道如何获得序列化部分

Bruno 的解决方案似乎奏效了。

#include<stdio.h>
#include<string.h>
int main()
{
        char string[10];
        FILE* fp[10];


        printf("Enter the string");
        scanf("%s", string);

        char fn[sizeof(string)+8];


        for(int i=0; i<=10; i++){
                sprintf(fn, "%s%d", string, i);
                if((fp[i] = fopen(fn, "w")) == 0)
                        printf("Cannot open %s\n", fn);
        }


        for(int i=0; i<=10; i++){
                fclose(fp[i]);
        }
        return 0;
This seems to be working. Thanks

应该是这样的:

输入:

Give me a name: Test

输出:

Created Test1.txt, Test2.txt, Test3.txt, .... Test10.txt

【问题讨论】:

  • 您在哪里遇到了麻烦?获取用户输入?创建文件名?创建文件?
  • 你试过什么?发布您迄今为止的尝试。
  • 请告诉我们您尝试了什么以及它是如何失败的。请阅读How to Ask页面!!另外:idownvotedbecau.se/noattempt
  • 要创建文本文件,只需查看以“w”模式打开的 fopen 函数。应该打开具有您给它的特定名称的新文件
  • 如第一条评论所述,见sprintfchar filename[256]; sprintf(filename, "%s%d.txt", user_input, the_number);

标签: c string file


【解决方案1】:

做这样的事

#include<stdio.h>
#include <assert.h>

#define N 10

int main()
{
  assert((N >= 0) && (N <= 999)); /* check the value of N if it is changed */

  char base[10]; /* the base name of the files */
  FILE *fp[N]; /* to save the file descriptors */

  printf("Enter the base name:");
  if (scanf("%9s", base) != 1)
    // EOF
    return -1;

  char fn[sizeof(base) + 7]; /* will contains the name of the files */

  for (int i = 0; i != N; ++i) {
    sprintf(fn, "%s%d.txt", base, i+1);
    if ((fp[i] = fopen(fn, "w")) == 0)
      printf("cannot open %s\n", fn);
  }

  /* ... */

  for (int i = 0; i != N; ++i) {
    if (fp[i] != NULL)
      fclose(fp[i]);
  }

  return 0;
}

I size fn 允许比 base 多 7 个字符,因为关于 N (assert) 的限制允许 3 位以上的数字“.txt”的4个字符

scanf("%9s", ..) 允许输入基本名称,限制为 9 个字符,与 base 的大小兼容(10 个也有位置用于结束字符串的空字符)

sprintf 类似于 printf,但除了在 stdout 上打印结果之外,将一个放在作为第一个参数的字符串中。注意每次在fn中重写base都是徒劳的,这个可以优化。

编译和执行:

pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra f.c
pi@raspberrypi:/tmp $ echo aze*
aze*
pi@raspberrypi:/tmp $ ./a.out
Enter the base name:aze
pi@raspberrypi:/tmp $ echo aze*
aze10.txt aze1.txt aze2.txt aze3.txt aze4.txt aze5.txt aze6.txt aze7.txt aze8.txt aze9.txt
pi@raspberrypi:/tmp $ 

在执行echo aze*之前产生aze*,因为没有以aze开头的文件,执行之后aze*表示创建的文件是预期的

【讨论】:

  • 这似乎确实有效.. 但你能告诉我我的代码有什么问题吗?我似乎只是在制作一个带有随机名称的文件: for(int i=0; i
  • @NeXT in sprintf(fn, "s%d", string, i) 你错过了一个 % ,必须是 sprintf(fn, "%s%d", string, i),因为你将字符串设置为 's' 然后(一部分)string
  • 谢谢,它似乎工作了!
  • @NeXT 欢迎您,祝您编码愉快
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-26
  • 2019-04-19
  • 2020-02-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多