save new file each time?

Lately I've been messing around with something at work and I can't figure out how to make a new filename each time the user completes the program. What I'm trying to do is make serialnumber.csv have a number in it's filename, indicating what file it is. I was thinking of just having the double value for serialnumber be the filename but I am unsure how to do that. Every time I use %d with the double "serialnumber" it doesn't seem to work. Should I try something other than Fopen "w" in order to create the file?

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include "stdafx.h"
#include <iostream>
#include<Windows.h>
//#include "mainwindow.h"
using namespace std;
int main(void) {
	
//FreeConsole(); //makes the window invisible
double ct;// ct= how many rows in excel you want
int serialnumber;
char stringread[80];
char str[80];
char str2[80];
char str3[80];
char str4[80];
char str5[80];
char str6[80];
char str7[80];
char str8[80];
char str9[80];
char str10[80];
char str11[80];
char str12[80];
char str13[80];
char str14[80];
char str15[80];
char str16[80];
char str17[80];
char serialnumber1[80]="Serial number";
char anode1[80]="Anode 1";
char anode2[80]="Anode 2";
char anode3[80]="Anode 3";
char anode4[80]="Anode 4";
char anode5[80]="Anode 5";
char assemblylot[80]="assembly lot number";
char Capacitence[80]="capacitence";
char cathodelot[80]="cathode lot number";
char esr[80]="ESR";
char headassembly[80]="head assembly number";
char headerglassinglot[80]="Header Glassing Lot";
char leakage[80]="Leakage";									// a bunch of strings for the template
char tantalumlot[80]="Tantalum lot number";
char teflonspacer[80]="Teflon Spacer 1";
char teflonspacer2[80]="Teflon Spacer 2";
char teflonspacer3[80]="Teflon Spacer 3";
char teflonspacerlotnumber[80]="Teflon Spacer Lot number";
char serialnumbertokit[80]="Serial number to kit";
char directory[80]="category path = Root\\Kit Serial Numbers";
printf("enter serial number:\t");  // asks for serialnumber as an integer
scanf("%d",&serialnumber);  //stores the value in "serialnumber"
printf("enter assembly lot number:\t");
scanf("%79s",str5);  // stores whatever the user inputs to "assembly lot number" as a string
printf("enter capacitence:\t");
scanf("%79s",str6);
printf("enter Cathode Lot Number:\t");
scanf("%79s",str7);
printf("enter ESR:\t");
scanf("%79s",str8);
printf("enter Head assembly number:\t");
scanf("%79s",str9);
printf("enter header glassing lot number:\t");
scanf("%79s",str10);
printf("enter Leakage:\t");
scanf("%79s",str11);
printf("enter Tantalum lot number:\t");
scanf("%79s",str12);
printf("enter teflon spacer lot number:\t");
scanf("%79s",str16);
printf("enter Serial number to kit:\t");
scanf("%79s",str17);
FILE *outfile; // declares a pointer to a file 
outfile=fopen("z:\\database\\serialnumber%d.csv","w"),serialnumber; //opens or creates(if the file doesn't already exist) a file to write to
fprintf(outfile,"%s\n",directory); //inputs all of these strings into the first row in excel
fprintf(outfile,"%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",serialnumber1,anode1,anode2,anode3,anode4,anode5,assemblylot,Capacitence,cathodelot,esr,headassembly,headerglassinglot,leakage,tantalumlot,teflonspacer,teflonspacer2,teflonspacer3,teflonspacerlotnumber,serialnumbertokit); //inputs all of these strings into the first row in excel which generates the template
for(ct=0;ct<40;++ct){//determines number of rows for example ct<40 gives 40 rows
printf("Enter anode 1's lot number:\t");
scanf("%79s",stringread);
printf("enter anode 2's lot number:\t");
scanf("%79s",str);								//loops 40 times
printf("enter anode 3's lot number:\t");
scanf("%79s",str2);
printf("enter anode 4's lot number:\t");
scanf("%79s",str3);
printf("enter anode 5's lot number:\t");
scanf("%79s",str4);
printf("enter teflon spacer 1:\t");
scanf("%79s",str13);
printf("enter teflon spacer 2:\t");
scanf("%79s",str14);
printf("enter teflon spacer 3:\t");
scanf("%79s",str15);
fprintf(outfile,"\n%d,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s",serialnumber,stringread,str,str2,str3,str4,str5,str6,str7,str8,str9,str10,str11,str12,str13,str14,str15,str16,str17);//fills in 40 cells with all of this data
serialnumber=serialnumber+1;
}
}
Every time I use %d with the double "serialnumber" it doesn't seem to work.

That's because fopen() doesn't work like printf() it doesn't know you want to add the serial number to the string using the "%d" specifier. You'll need to use something like sprintf() to create the open "string" before you use it in fopen().

1
2
3
4
char file[1000];
int serialNumber = 100;
sprintf(file, "z:\\database\\serialnumber%d.csv", serialNumber);
FILE *outfile = fopen(file);


Since it appears that you're just learning the language, is there a reason you picked C instead of C++? And with either language it looks like you should consider a structure or class to group the "parts".

And you really should find and use an indentation style you like, it'll make reading your program much easier. http://en.wikipedia.org/wiki/Indent_style
I did it in C because I'm more used to it. I haven't been taught C++ fully yet, so every now and then I use a lot of C stuff
Topic archived. No new replies allowed.