Home › Forums › C Programming › Please help…
- This topic has 0 replies, 1 voice, and was last updated 15 years, 2 months ago by GarnetHuynh.
- AuthorPosts
- August 19, 2009 at 2:23 pm #2224GarnetHuynhParticipant
Hi,
I have the following C code, which does the following :-
Takes the brt file name from character 1 of the input filename (ALLSMFAS.ref) and creates a new file (A.brt) as follows :-
ALLSMFAS.ref
AD,B,0424,0415,89375,,A,2,,80,,
AD,B,0424,0417,89375,,A,1,,82,,
AD,B,0416,0410,89375,,B,2,,-7,2,
BN,B,0135,0094,65625,,D,,,0,1,H
BN,B,0338,0337,65824,,B,,,-18,1,
BN,B,0334,0339,65824,,C,,,65,,
CA,T,,SREC,47221,,C,,R,0,,
CA,T,,X171,47221,,A,6,,60,,
CA,T,,X173,47221,,A,5,,60,,
CC,B,1783,1574,48231,,C,1,,60,,
CC,B,1783,1576,48231,,C,2,,60,,
CC,B,1783,1578,48231,,C,3,,60,,A.brt
B0424041589375 B0424041789375 B0416041089375 B0135009465625 B0338033765824 B0334033965824
T SREC47221 T X17147221 T X17347221 B1783157448231 B1783157648231 B1783157848231123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368#include "p2geog.h"<br /><br />#define max_smf_lines 1000 * td_max<br /><br /><br />// Reformat the structure of smf_file_layout<br /><br />struct smf_file_layout<br />{<br />char line_type[2];<br />char step_type;<br />char from_berth[4];<br />char to_berth[4];<br />char stanox[5];<br />char event_type;<br />char thru_line;<br />char platform_id;<br />int event_time_offset;<br />char forward_line;<br />char forward_route;<br />unsigned long sort_key;<br />};<br /><br /><br /><br />struct tds_from_smf<br />{<br />char td_id[2];<br />int sfl_ind;<br />int sfl_rec_count;<br />};<br /><br />struct smf_file_layout sfl[max_smf_lines];<br />struct tds_from_smf tds_ive_seen[td_max];<br />int sfl_count;<br /><br />extern HWND main_handle;<br />extern char berth_step_dir[],<br />td_maps_dir[];<br />extern BOOL gen_brt_files;<br />extern struct td_header_tree tdh[td_max];<br />extern struct td_map_tree tdm[td_map_max];<br />extern struct adjacent_maps *am[];<br />extern int map_count,<br />adj_map_count,<br />td_header_count;<br />extern FILE *file_handle;<br /><br /><br /><br />void generate_brt_files(void)<br />{<br /><br />char f_name[MAX_PATH],<br />this_td[3];<br />// buffer[200];<br />FILE *ref_file;<br />WIN32_FIND_DATA wfd;<br />HANDLE find_handle;<br />int seen_td_count = 0,<br />cnt;<br />// ptr,<br />// knt;<br /><br /><br />if (gen_brt_files)<br />{<br />if (!berth_step_dir[0])<br />{<br />MessageBox(main_handle,<br />"No location has been set for the berth stepping files.",<br />"Error,,,",<br />MB_ICONERROR | MB_OK);<br />return;<br />}<br /><br /><br />memset(&wfd,<br />0,<br />sizeof(WIN32_FIND_DATA));<br /><br /><br />// Change the input file type format.<br /><br />wsprintf(f_name,<br />"%sallsmf*.ref",<br />berth_step_dir);<br /><br />if ((find_handle = FindFirstFile(f_name,<br />&wfd)) == INVALID_HANDLE_VALUE)<br />{<br />MessageBox(main_handle,<br />"No SMART berth stepping files found.",<br />"Error...",<br />MB_ICONERROR | MB_OK);<br />return;<br />}<br /><br />sfl_count = 0;<br /><br />memset(tds_ive_seen,<br />0,<br />sizeof(tds_ive_seen));<br />memset(sfl,<br />0,<br />sizeof(sfl));<br /><br />while (1)<br />{<br /><br />wsprintf(f_name,<br />"%s%s",<br />berth_step_dir,<br />wfd.cFileName);<br /><br />if (ref_file = fopen(f_name,<br />"rb"))<br />{<br /><br /><br />// Take the brt file name from character 1 of the input filename.<br /><br />memcpy(this_td,<br />wfd.cFileName,<br />1);<br />this_td[2] = 0;<br /><br /><br />for (cnt = 0; cnt < seen_td_count; cnt++)<br />{<br />if (!memicmp(&this_td,<br />&tds_ive_seen[cnt].td_id,<br />2))<br />break;<br />}<br /><br />if (cnt >= seen_td_count)<br />{<br /><br />// only process the file if we haven't seen this TD already<br /><br />memcpy(&tds_ive_seen[seen_td_count].td_id,<br />this_td,<br />2);<br /><br />tds_ive_seen[seen_td_count].sfl_ind = sfl_count;<br /><br />read_smf_file(ref_file);<br /><br />tds_ive_seen[seen_td_count].sfl_rec_count = sfl_count -<br /><br />tds_ive_seen[seen_td_count].sfl_ind;<br /><br />if (gen_brt_files)<br />write_brt_file(seen_td_count);<br /><br />seen_td_count++;<br />}<br /><br />fclose(ref_file);<br />}<br /><br />if (!FindNextFile(find_handle,<br />&wfd))<br />break;<br />}<br /><br />FindClose(find_handle);<br />} // end if (gen_brt_files)<br />else<br />{<br /><br />gen_brt_files = FALSE;<br />wsprintf(f_name,<br />"%std_ind.tmp",<br />td_maps_dir);<br />save_file(f_name);<br />do_td_index();<br />wsprintf(f_name,<br />"%std_ind.new",<br />td_maps_dir);<br />file_handle = fopen(f_name,<br />"rb");<br />process_td_index();<br />draw_tree();<br />}<br /><br />}<br /><br /><br />BOOL read_smf_file(FILE *file)<br />{<br /><br />char buffer[100],<br />*ptr,<br />*mem;<br />unsigned int knt;<br /><br /><br />while (!feof(file))<br />{<br />memset(&buffer,<br />0,<br />sizeof(buffer));<br /><br />fgets(buffer,<br />sizeof(buffer),<br />file);<br /><br /><br />memset(&sfl[sfl_count],<br />' ',<br />sizeof(struct smf_file_layout));<br /><br /><br />// Remove the stanox_int<br /><br />sfl[sfl_count].event_time_offset =<br />sfl[sfl_count].sort_key = 0;<br /><br />for (knt = 0; knt < strlen(buffer); knt++)<br />{<br /><br />if (buffer[knt + 1] == 13 ||<br />buffer[knt + 1] == 10)<br />break;<br /><br />if ((buffer[knt] == ',') &&<br />(buffer[knt + 1] == ','))<br />{<br />memmove(&buffer[knt + 2],<br />&buffer[knt + 1],<br />strlen(buffer) - knt);<br />buffer[knt + 1] = ' ';<br />}<br />}<br /><br />ptr = buffer;<br /><br />if ((mem = strtok(ptr,<br />",")) && (mem[0] > ' '))<br />memcpy(sfl[sfl_count].line_type,<br />mem,<br />2);<br /><br />if ((mem = strtok(NULL,<br />",")) && (mem[0] > ' '))<br />memcpy(&sfl[sfl_count].step_type,<br />mem,<br />1);<br />if ((mem = strtok(NULL,<br />",")) && (mem[0] > ' '))<br />memcpy(&sfl[sfl_count].from_berth,<br />mem,<br />4);<br /><br />if ((mem = strtok(NULL,<br />",")) && (mem[0] > ' '))<br />memcpy(sfl[sfl_count].to_berth,<br />mem,<br />4);<br /><br /><br />// Remove the trust_event, rttm_event, stanox_int,<br />// tiploc and direction<br /><br /><br />if ((mem = strtok(NULL,<br />",")) && (mem[0] > ' '))<br />{<br />memcpy(sfl[sfl_count].stanox,<br />mem,<br />5);<br />}<br /><br /><br />if ((mem = strtok(NULL,<br />",")) && (mem[0] > ' '))<br />memcpy(&sfl[sfl_count].event_type,<br />mem,<br />1);<br /><br />if ((mem = strtok(NULL,<br />",")) && (mem[0] > ' '))<br />memcpy(&sfl[sfl_count].thru_line,<br />mem,<br />1);<br /><br />if ((mem = strtok(NULL,<br />",")) && (mem[0] > ' '))<br />memcpy(&sfl[sfl_count].platform_id,<br />mem,<br />1);<br /><br />if ((mem = strtok(NULL,<br />",")) && (mem[0] > ' '))<br />sfl[sfl_count].event_time_offset = atoi(mem);<br /><br />if ((mem = strtok(NULL,<br />",")) && (mem[0] > ' '))<br />memcpy(&sfl[sfl_count].forward_line,<br />mem,<br />1);<br /><br />if ((mem = strtok(NULL,<br />",")) && (mem[0] > ' '))<br />memcpy(&sfl[sfl_count].forward_route,<br />mem,<br />1);<br /><br />if ((mem = strtok(NULL,<br />",")) && (mem[0] > ' '))<br />sfl[sfl_count].sort_key = atoi(mem);<br /><br />sfl_count++;<br />}<br /><br />return TRUE;<br />}<br /><br /><br /><br />BOOL write_brt_file(int tis_ind)<br />{<br /><br />FILE *brt_file;<br />char f_name[MAX_PATH],<br />buffer[100];<br />int cnt,<br />last_rec;<br /><br /><br />wsprintf(f_name,<br />"%s%.2s.brt",<br />berth_step_dir,<br />tds_ive_seen[tis_ind].td_id);<br /><br />if (brt_file = fopen(f_name,<br />"wb"))<br />{<br />last_rec = tds_ive_seen[tis_ind].sfl_rec_count + tds_ive_seen[tis_ind].sfl_ind;<br />for (cnt = tds_ive_seen[tis_ind].sfl_ind; cnt < last_rec; cnt++)<br />{<br />wsprintf(buffer,<br />"%c%.4s%.4s%.5s ",<br />sfl[cnt].step_type,<br />sfl[cnt].from_berth,<br />sfl[cnt].to_berth,<br />sfl[cnt].stanox);<br />fwrite(buffer,<br />19,<br />1,<br />brt_file);<br />}<br />fclose(brt_file);<br />}<br />else<br />{<br />MessageBox(main_handle,<br />"Unable to open file for writing",<br />f_name,<br />MB_ICONERROR | MB_OK);<br />return FALSE;<br />}<br /><br />return TRUE;<br />}<br />But now, I need to create output files from the column 1 of the input file and each time the value in column 1 changes,
a new file is created.
So for input file :-
ALLSMFAS.ref
AD,B,0424,0415,89375,,A,2,,80,,
AD,B,0424,0417,89375,,A,1,,82,,
AD,B,0416,0410,89375,,B,2,,-7,2,
BN,B,0135,0094,65625,,D,,,0,1,H
BN,B,0338,0337,65824,,B,,,-18,1,
BN,B,0334,0339,65824,,C,,,65,,
CA,T,,SREC,47221,,C,,R,0,,
CA,T,,X171,47221,,A,6,,60,,
CA,T,,X173,47221,,A,5,,60,,
CC,B,1783,1574,48231,,C,1,,60,,
CC,B,1783,1576,48231,,C,2,,60,,
CC,B,1783,1578,48231,,C,3,,60,,
:
:the following needs to be the output files.
AD.brt
B0424041589375 B0424041789375 B0416041089375BN.brt
B0135009465625 B0338033765824 B0334033965824CA.brt
T SREC47221 T X17147221 T X17347221CC.brt
B1783157448231 B1783157648231 B1783157848231and so on ….
In the above code, I am stuck up at the following place, where I believe the logic for reading the first column of the file
and for each change in the value, a new file is created has to be put.
1234567// Take the brt file name from character 1 of the input filename.<br /><br />memcpy(this_td,<br />wfd.cFileName,<br />1);<br />this_td[2] = 0;<br />Request you, if you could please help me with the appropriate code for this.
Thanks a lot.
Marconi.
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.