user defined function and fopen customised file name

Hi,
I am trying to simplify my program using user defined function.
The function is basically going to be creating a file with the customised name.
My function is format(char x) and the file name outputs x.txt instead of 1188AC.txt. Is there anyway for me to save as 1188AC? Thanks
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35

#include "StdAfx.h"
#include <stdio.h>
#include <iostream>
#include <cstring>
using namespace std;
void format(char);

int main (void)
{

some if else statements
if ( strncmp (type_serial,"4",1) == 0 )// product is produced at FAT
{	
format(1188AC);

}
}

void format(char x)
{
	char bartender_commander;
	FILE *textfile, *print_file_read, *print_file_write;
	print_file_read= fopen("Scanned Numbers.TXT","r");

	print_file_write= fopen("x.TXT","w");

	do{	bartender_commander=fgetc(print_file_read);
		if (bartender_commander!=EOF)
		{	fputc(bartender_commander, print_file_write);	}
	}while (bartender_commander !=EOF);
	fclose(print_file_read);
	fclose(print_file_write);
	cout<<"Printing...\n\n";
}
Last edited on
you need a string not a char for this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
format("1188AC");
...
void format(const char *x)
{
char fn[100];
char bartender_commander;
FILE *textfile, *print_file_read, *print_file_write;
print_file_read= fopen("Scanned Numbers.TXT","r");

strcpy(fn, x);
strcat(fn, ".TXT");

print_file_write= fopen(fn,"w");
...


Please use code tags: [code]Your code[/code]
See: http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.