Is there a simple way to concatenate all of the fprintf(qFile, "%-1.1c", mod[cnt].name[x]);
expressions into a small number of expressions?
Appreciate any other suggestions.
#include "stdafx.h"
#include <stdio.h>
#include <iostream>
// fprintfQuestion.cpp : Defines the entry point for the console application.
// add _CRT_SECURE_NO_DEPRECATE to Properties \ C\C++ \ Preprocessor \ Preprocessor Definitions
struct mod
{
char name[32];
int dateStart;
int dateEnd;
};
The struct mod is an abbreviated version of the struct used in a GUI editor for the game for which I'm an apprentice developer. The full struct includes about 20 variables. Objects from this struct correspond to "script modules" used by the game's computer-opponent, and which can be randomly or statically assigned to specific scenarios in the game by scenario designers.
The goal of the exercise I'm engaged in at present is to develop a "loader" application that can transfer data between the .dat files the editor works with and .csv files that humans can manipulate in spreadsheets. Most of the scenario designers are not programmers.
int main()
{
FILE *pFile, *qFile;
int cnt = 0, n = 0;
char str[40]; //1328
mod mod[3]; //2000
long bytes = 1328; // first record of 0 to 1327 is blank
pFile = fopen("W:\\User\\dir\\sourcefile.dat", "rb");
qFile = fopen("W:\\User\\dir\\targetfile.csv", "a");
for (cnt = 0; cnt < 3; cnt++)
{
fseek(pFile, bytes, SEEK_SET);
for (n = 0; n < 40; n++)
{
str[n] = getc(pFile);
};
memmove(mod+cnt, str, 40);
fprintf(qFile, "%-1.1c", mod[cnt].name[0]);
fprintf(qFile, "%-1.1c", mod[cnt].name[1]);
fprintf(qFile, "%-1.1c", mod[cnt].name[2]);
fprintf(qFile, "%-1.1c", mod[cnt].name[3]);
fprintf(qFile, "%-1.1c", mod[cnt].name[4]);
fprintf(qFile, "%-1.1c", mod[cnt].name[5]);
fprintf(qFile, "%-1.1c", mod[cnt].name[6]);
fprintf(qFile, "%-1.1c", mod[cnt].name[7]);
fprintf(qFile, "%-1.1c", mod[cnt].name[8]);
fprintf(qFile, "%-1.1c", mod[cnt].name[9]);
fprintf(qFile, "%-1.1c", mod[cnt].name[10]);
fprintf(qFile, "%-1.1c", mod[cnt].name[11]);
fprintf(qFile, "%-1.1c", mod[cnt].name[12]);
fprintf(qFile, "%-1.1c", mod[cnt].name[13]);
fprintf(qFile, "%-1.1c", mod[cnt].name[14]);
fprintf(qFile, "%-1.1c", mod[cnt].name[15]);
fprintf(qFile, "%-1.1c", mod[cnt].name[16]);
fprintf(qFile, "%-1.1c", mod[cnt].name[17]);
fprintf(qFile, "%-1.1c", mod[cnt].name[18]);
fprintf(qFile, "%-1.1c", mod[cnt].name[19]);
fprintf(qFile, "%-1.1c", mod[cnt].name[20]);
fprintf(qFile, "%-1.1c", mod[cnt].name[21]);
fprintf(qFile, "%-1.1c", mod[cnt].name[22]);
fprintf(qFile, "%-1.1c", mod[cnt].name[23]);
fprintf(qFile, "%-1.1c", mod[cnt].name[24]);
fprintf(qFile, "%-1.1c", mod[cnt].name[25]);
fprintf(qFile, "%-1.1c", mod[cnt].name[26]);
fprintf(qFile, "%-1.1c", mod[cnt].name[27]);
fprintf(qFile, "%-1.1c", mod[cnt].name[28]);
fprintf(qFile, "%-1.1c", mod[cnt].name[29]);
fprintf(qFile, "%-1.1c", mod[cnt].name[30]);
fprintf(qFile, "%-1.1c,", mod[cnt].name[31]);
fprintf(qFile, "%.6i,", mod[cnt].dateStart);
fprintf(qFile, "%.6i,", mod[cnt].dateEnd);
fprintf(qFile,"\n");
bytes = (bytes + 1328);
};
fclose(pFile);
fclose(qFile);
std::cout << "Type any key to quit. \n";
std::cin.get();
return 0;
}
As far as I know, there are no null-terminated records in the data; my examination of the .dat files with HexEdit does not suggest as much to me.
A typical row of output in the .csv that this code generates might be:
Attack Orleans Rgt,281012,291228
Corresponding to the name of the script, the date it can start (12 Oct 1428) and the date it can end (28 Dec 1429).
struct mod
represent? What are you really aiming to accomplish with this code? \$\endgroup\$for (i=0; i<32; i++) fprintf(qfile, "%-1.1c", mod[cnt].name[i]);
\$\endgroup\$"%-1.1c"
as your format string? Doesn't that just print one character? You specify left alignment, minimum one character, and precision one character, but I don't see how that is any different than just"%c"
when we are talking about printing single characters? Perhaps you could show us an example of a line of output. \$\endgroup\$