T
T
Themidis Koudes2020-06-12 20:11:09
C++ / C#
Themidis Koudes, 2020-06-12 20:11:09

How to combine numbers and strings into one for writing to a file?

In each line, number, surname, group, number 1, number 2, number 3
It is necessary to isolate each element from all lines and write it in a separate line: number 1, number 2
, ...
etc.
It is necessary to write down in the general line formatting.

int k, i, nom, o1, o2, o3,pp;
char fam[12], gr[8];
char *fams, *grs, *oc1, *oc2, *oc3, *noms;
char *g, l[12];
noms = new char[k * 12];
fams = new char[k * 12];
grs = new char[k * 12];
oc1= new char[k * 12];
oc2 = new char[k * 12];
oc3 = new char[k * 12];
for (int i = 0; i < k; i++) {
//получение g
sscanf(g, "%d %s %s %d %d %d", &nom, gr, fam, &o1, &o2, &o3);
sprintf(l, "%-12d",  nom);
strcat(noms, l);
sprintf(l, "%-12s", gr);
strcat(grs, l);
sprintf(l, "%-12s", fam);
strcat(fams, l);
sprintf(l,"%-12d", o1);
strcat(oc1, l);
sprintf(l,"%-12d",o2);
strcat(oc2, l);
sprintf(l,"%-12d",o3);
strcat(oc3, l);
}

Do not look that memory is allocated as in C ++, the rest is needed in C

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-06-13
@Spargwy

Instead of writing each line to a separate one-dimensional array, write to a two-dimensional one. As a result, you should get a base that stores all these lines. Then create an array with a length of, say, 1000 characters.
Well, then, using cycles, read the contents of a two-dimensional array into a one-dimensional one.
It's so offhand.
By the way, I didn’t quite understand what to format when recording?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question