简体   繁体   English

打开标题为int和char变量的文件

[英]Opening a file with a title including int and char variables

I want to open a file with the title eg "File 10-9B-g06.dat" where "B" and "File" are repeated for all files but the rest are variable. 我想打开一个标题为“ File 10-9B-g06.dat”的文件,其中对所有文件重复“ B”和“ File”,但其余都是可变的。 The problem start when I have the "g" in the title. 问题开始于标题中有“ g”的地方。 I used the "g" variable as a string, but it did not work as well. 我将“ g”变量用作字符串,但效果不佳。

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

int main()
{
 int CaseSize, Type, Counter;
 char name[100], zahl[20], char Template[1];

FILE *stream;

scanf("%d", &CaseSize );
scanf("%d", &Type );
scanf("  %c", &Template );
scanf("%d", &Counter );

strcpy(name, "File ");
sprintf(zahl, "%d", CaseSize);
strcat(name, zahl);
strcat(name, "-");
sprintf(zahl, "%d", Type);
strcat(name, zahl);
strcat(name, "B-");
sprintf(name, "%c", Template);
strcat(name, zahl);

sprintf(zahl, "%2d", Counter);
strcat(name, zahl);
strcat(name, ".dat");
stream=fopen(name, "a");

fclose(stream);
return 0;
}

I was wondering if anyone can help explain this to me or if they can direct me to anywhere I can get some examples I can look through. 我想知道是否有人可以帮助我解释这个问题,或者他们是否可以引导我到任何我可以浏览的示例中。

Thanks. 谢谢。

The code has a number of issues with format specifiers and data types, but apart from that is remarkably inefficient and unnecessarily complex. 该代码在格式说明符和数据类型方面存在许多问题,但除此之外,效率极低且不必要地复杂。

Changing char Template[1] to just char Template , and removing the redundant zahl[] , the 12 lines you have for constructing name can be reduced to just: char Template[1]更改为char Template ,并删除多余的zahl[] ,用于构造name的12行代码可以简化为:

sprintf( name, "%s %d-%dB-%c%02d.dat", "File", CaseSize, Type, Template, Counter ) ;

One problem with C strings is that they contain no length information, so functions such as strcat() must iterate from the beginning searching for the nul terminator; C字符串的一个问题是它们不包含长度信息,因此诸如strcat()函数必须从开始搜索nul终止符开始进行迭代。 here you are using strcat() repeatedly on an increasingly long string. 在这里,您在越来越长的字符串上重复使用strcat() In this short string construction, thus is perhaps not significant, but in some cases it can become prohibitively slow . 因此,在这种短弦构造中,可能并不重要,但在某些情况下,它可能会变得过慢 Apart from that, more lines of code simply mean more code to maintain and debug. 除此之外,更多行代码仅意味着需要维护和调试更多代码。 Here the entire format is described in one line of code, so it is mich easier in maintenance to see what it is that should be constructed. 在这里,整个格式在一行代码中进行了描述,因此维护起来很容易就能看出应该构造什么。

First problem seems to be that you're using spaces in scanf when reading variable Template. 第一个问题似乎是在读取变量Template时在scanf中使用空格。 See hints here: C scanf with spaces problem 在此处查看提示: C scanf出现空格问题

The two main problems are these: 两个主要问题是:

scanf(" %c", &Template ); scanf(“%c”,&Template);

should be 应该

scanf("  %c", &Template[0]);

sprintf(name, "%c", Template); sprintf(name,“%c”,模板);

should be 应该

sprintf(zahl, "%c", Template[0]);

Been testing a little, this works: 经过一点测试,这可行:

int main()
{
  int CaseSize, Type, Counter;
  char name[100];
  char zahl[20];
  char Template;

  FILE *stream;

  scanf("%d", &CaseSize);
  scanf("%d", &Type);
  scanf(" %c", &Template);
  scanf("%2d", &Counter);

  strcpy(name, "File ");
  sprintf(zahl, "%d", CaseSize);
  strcat(name, zahl);
  strcat(name, "-");
  sprintf(zahl, "%d", Type);
  strcat(name, zahl);
  strcat(name, "B-");
  sprintf(zahl, "%c", Template);
  strcat(name, zahl);

  sprintf(zahl, "%02d", Counter);
  strcat(name, zahl);
  strcat(name, ".dat");
  printf("%s\n", name);
  stream=fopen(name, "a");
  fclose(stream);
  return 0;
}

You can do all that in a single sprintf statement: 您可以在一个sprintf语句中完成所有操作:

sprintf( name, "File %d-%dB-%c%02d.dat", CaseSize, Type, Template, Counter );

Given how you're using Template , you should not declare it as a 1-element array of char ; 鉴于您使用Template ,您不应将其声明为char的1元素数组; the types of the expressions &Template and Template will be char (*)[1] and char * respectively, which is not what you want. 表达式&TemplateTemplate的类型分别为char (*)[1]char * ,这不是您想要的。 Declare it as a plain char instead. 改为将其声明为纯char

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM