简体   繁体   中英

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. The problem start when I have the "g" in the title. I used the "g" variable as a string, but it did not work as well.

#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:

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; here you are using strcat() repeatedly on an increasingly long string. 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. See hints here: C scanf with spaces problem

The two main problems are these:

scanf(" %c", &Template );

should be

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

sprintf(name, "%c", Template);

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( 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 ; the types of the expressions &Template and Template will be char (*)[1] and char * respectively, which is not what you want. Declare it as a plain char instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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