A friend left me a code that should open 2 files solution.txt and priority.csv and generate a 3rd file result.txt as an output.
Files structures:
solution.txtcontains an ordered list of target, names and values separated by commas, and I know how many line the file contains:
target0,name0,value0,name1,value1,name2,value2,name3,value3 target1,name4,value4,name5,value5,name6,value6,name7,value7 target2,name8,value8,name9,value9,name10,value10,name11,value11 ...etc
- some of
solution.txt's lines contain only atargetvalue:
target3
target4
target5 ...etc.
priority.csvcontains an indexed list of colornames, I know how many colornames there are:
1,colorname1
2,colorname2
3,colorname3
The code below should do the following:
- ask how many lines of data solution.txt contains
- ask how many colornames are contained in
priority.csv - put the
name(s) and associatedvalue(s) ofsolution.txtin the order defined bypriority.csv - create an array that contains as many columns as the number of colors, and as many lines as in
solution.txt - fill this array with the
value(s) associated to eachline&colorname - fill the other cases with 0
The code doesn't work for some reasons I don't understand. If you have the courage to compile it, here are examples to put in solution.txt and priority.txt :
solution.txt
99985,CIN,0.624049619347,OR0,0.36925123875,K2M,0.00387491644559,gY6D,0.00282422545715
99986,CIN,0.624372658354,OR0,0.369683600811,K2M,0.00365124527159,gY6D,0.00229249556329
99987,CIN,0.624695697361,OR0,0.370115962872,K2M,0.0034275740976,gY6D,0.00176076566943
99988,CIN,0.625018736368,OR0,0.370548324933,K2M,0.0032039029236,gY6D,0.00122903577557
99989,CIN,0.625341775375,OR0,0.370980686994,K2M,0.00298023174961,gY6D,0.00069730588171
99990,CIN,0.625664814382,OR0,0.371413049055,K2M,0.00275656057561,gY6D,0.000165575987851
priority.csv
1,CIN
2,K2M
3,gY6D
4,OR0
In this example the amonut of lines is 6 and there are 4 colors
And that's the code, I listed possible errors and I get error2 and error4 printed on the console:
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <stdio.h>
#include <string.h>
using namespace std;
int main() {
int COLORS = 0;
int LINES = 0;
cout << endl << "Original Data (solution.txt):" << endl << endl << "How many lines?" << endl;
cin >> LINES;
cout << "How many colors in the list?" << endl;
cin >> COLORS;
char Coul[COLORS][LINES];
//1- reading the file priority.csv
for (int i=0; i<COLORS; i++) Coul[i][0]='\0';
FILE *Read=fopen("priority.csv","rt");
if (Read == NULL) {
printf("Error 1");
return(0);
}
char line[120];
int N;
for (int i=0; i<COLORS; i++)
{
char name[8];
fgets(line,15,Read);
sscanf(line,"%d,%s",&N,name);
if (N == i) strcpy(Coul[i], name);
else {
printf("Error 2"); // Error2
break;
}
}
fclose(Read);
//2- reading the file solution.txt and writing the result.
Read=fopen("solution.txt","rt");
if (Read == NULL) {
printf("Error 3"); // Error3
return(0);
}
FILE *Write=fopen("result.txt","wt");
for (int i=1; i<LINES; i++)
{
char c[4][8]; // 4 color names
float v[4]; // 4 values
fgets(line,119,Read);
sscanf(line,"%d,%s,%f,%s,%f,%s,%f,%s,%f",
&N, c[0], &v[0], c[1], &v[1], c[2], &v[2], c[3], &v[3]);
if (N == i)
{
if (strlen(c[0]) == 0) // the line is empty
{
fprintf(Write,"%d ",i);
for (int i=0; i<COLORS; i++) fprintf(Write,"0 ");
fprintf(Write,"\n");
}
else
{
fprintf(Write,"%d ",i);
int r[4];
// we search the rang of c[ordre]
for (int order=0; order<4; order++)
{
for (int ir=0; ir<COLORS; ir++)
{
if (strcmp(c[order], Coul[ir]) == 0) r[order]=ir;
}
}
for (int ir=0; ir<r[0]; ir++) fprintf(Write,"0 ");
fprintf(Write,"%d ",v[0]);
for (int ir=r[0]+1; ir<r[1]; ir++) fprintf(Write,"0 ");
fprintf(Write,"%d ",v[1]);
for (int ir=r[1]+1; ir<r[2]; ir++) fprintf(Write,"0 ");
fprintf(Write,"%d ",v[2]);
for (int ir=r[2]+1; ir<r[3]; ir++) fprintf(Write,"0 ");
fprintf(Write,"%d ",v[3]);
for (int ir=r[3]+1; ir<57; ir++) fprintf(Write,"0 ");
fprintf(Write,"\n");
}
}
else {
printf("Error 4");
break;
} //Error4
}
fclose(Write);
fclose(Read);
}
could you please help debugging? I'm lost! Adrien
printfandstd::cout(they use different buffers, so e.g. flushingstd::coutwill not flush the output fromprintf).