#include #include #include #define MAX_LINE 2048 // Parse one CSV field, advancing the pointer char* get_field(char** line) { static char field[256]; char* start = *line; char* p = field; if (!*start) { field[0] = '\0'; return field; } while (*start) { if (*start == ',') { start++; break; } *p++ = *start++; } *p = '\0'; *line = start; return field; } int main(int argc, char* argv[]) { if (argc != 2) { fprintf(stderr, "Usage: %s input.csv\n", argv[0]); return 1; } FILE* in = fopen(argv[1], "r"); if (!in) { perror("Cannot open input file"); return 1; } FILE* out = fopen("vsx.dat", "w"); if (!out) { perror("Cannot open output file"); fclose(in); return 1; } char line[MAX_LINE]; int first = 1; while (fgets(line, sizeof(line), in)) { if (first) { // Skip header first = 0; continue; } char* ptr = line; char* field; char outbuf[300] = ""; // OID (7 chars) field = get_field(&ptr); sprintf(outbuf, "%-7s ", field ? field : ""); // Name (30 chars) field = get_field(&ptr); strcat(outbuf, field ? field : ""); int pad = 30 - strlen(field ? field : ""); for (int i = 0; i < pad; i++) strcat(outbuf, " "); strcat(outbuf, " "); // VarFlag (1 char + 2 spaces) field = get_field(&ptr); sprintf(outbuf + strlen(outbuf), "%s ", field ? field : ""); // RAdeg (9 chars) field = get_field(&ptr); sprintf(outbuf + strlen(outbuf), "%9.5f ", field && *field ? atof(field) : 0.0); // DEdeg (9 chars) field = get_field(&ptr); sprintf(outbuf + strlen(outbuf), "%9.5f ", field && *field ? atof(field) : 0.0); // Type (30 chars) field = get_field(&ptr); strcat(outbuf, field ? field : ""); pad = 30 - strlen(field ? field : ""); for (int i = 0; i < pad; i++) strcat(outbuf, " "); strcat(outbuf, " "); // LimitFlagOnMax (1 char) field = get_field(&ptr); strcat(outbuf, field && *field ? field : " "); strcat(outbuf, " "); // MagMax (7 chars) field = get_field(&ptr); if (field && *field) { sprintf(outbuf + strlen(outbuf), "%7.3f", atof(field)); } else { strcat(outbuf, " "); } strcat(outbuf, " "); // MaxUncertaintyFlag (skipped) field = get_field(&ptr); // MaxPassband (7 chars) field = get_field(&ptr); sprintf(outbuf + strlen(outbuf), "%-7s ", field && *field ? field : ""); // MinIsAmplitude (1 char) field = get_field(&ptr); sprintf(outbuf + strlen(outbuf), "%-1s ", field && *field ? field : ""); // LimitFlagOnMin (skipped) field = get_field(&ptr); // MagMin (7 chars) field = get_field(&ptr); if (field && *field) { sprintf(outbuf + strlen(outbuf), "%7.3f", atof(field)); } else { strcat(outbuf, " "); } strcat(outbuf, " "); // MinUncertaintyFlag (skipped) field = get_field(&ptr); // MinPassband (8 chars) field = get_field(&ptr); sprintf(outbuf + strlen(outbuf), "%-8s ", field && *field ? field : ""); // Epoch (14 chars) field = get_field(&ptr); if (field && *field) { sprintf(outbuf + strlen(outbuf), "%14.5f", atof(field)); } else { strcat(outbuf, " "); } strcat(outbuf, " "); // EpochUncertaintyFlag (skipped) field = get_field(&ptr); // LimitFlagOnPeriod (1 char) field = get_field(&ptr); strcat(outbuf, field && *field ? field : " "); strcat(outbuf, " "); // Period (19 chars) field = get_field(&ptr); if (field && *field) { sprintf(outbuf + strlen(outbuf), "%19.12f", atof(field)); } else { strcat(outbuf, " "); } strcat(outbuf, " "); // PeriodUncertaintyFlag (1 char) field = get_field(&ptr); strcat(outbuf, field && *field ? field : " "); strcat(outbuf, " "); // SpectralType (remaining) field = get_field(&ptr); strcat(outbuf, field && *field ? field : ""); //strcat(outbuf, "\n"); fputs(outbuf, out); } fclose(in); fclose(out); return 0; }