Home › Forums › C Programming › Read 1st col vals of i/pfile;create o/p fl for each diff val
- This topic has 2 replies, 2 voices, and was last updated 15 years, 2 months ago by GarnetHuynh.
- AuthorPosts
- August 20, 2009 at 1:23 pm #2225GarnetHuynhParticipant
Hi,
The following C code, at present does the following :-
Takes the Output 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 B1783157848231But now,I want to modify the above program to read each value of column 1 of my input file (ALLSMFAS.ref) and create Output file with name as the value at the column 1 of the input file. For each change in the value at column 1 of input file, a new output file should be created, 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,,
:
:
and so on..the following are 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 ….
The logic for taking the output filename name from character 1 of the input filename is as below :-
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 />I think I have to change at this place so that the code reads each value of column 1 of my input file (ALLSMFAS.ref) and
create Output file with name as the value at the column 1 of the input file. For each change in the value at column 1 of
input file, a new output file is created. I am unable to proceed further to implement this.Request you, if you could please help me on this.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361#include "p2geog.h"<br /><br />#define max_smf_lines 1000 * td_max<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 />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 />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 />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 />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 />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 />Thanks a lot.
Marconi. - August 21, 2009 at 3:58 pm #3606GWILouisaxwzklaParticipant
Heres some code that does what you want ( maybe you can splice this into your program ):
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798<br /><br />/****************************************************************<br />* File Name : c:programstempCG.cpp<br />* Date : August,10,2009<br />* Comments : new project<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications :<br />*<br />*<br />*<br />*<br />*<br />* Program Shell Generated At: 3:17:50 p.m.<br />=-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/<br /><br /><br /><br />#include < stdio.h ><br />//#include < conio.h ><br />//#include < math.h ><br />//#include < iomanip ><br />//#include < ctype.h ><br /><br />//using namespace std;<br /><br />//main function ******************************<br /><br />int main ( )<br />{<br /><br />//CHANGE FILE NAMES TO WHAT YOU WANT HERE !!!!!!!!!!!!!!!!!!!!!<br /><br />FILE * inputFile = fopen ( "c:\programs\data.txt", "r+" );<br /><br />FILE * outputFile;<br /><br /><br />//@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@<br /><br />char ch;<br />char newline = 10;<br />int commaCount;<br /><br /><br />char fileName [ 50 ] ;<br />strcpy ( fileName , "c:\programs\" );<br /><br /><br />fileName [ 12 ] = fgetc ( inputFile );<br />fileName [ 13 ] = fgetc ( inputFile );<br />fileName [ 14 ] = '.';<br />fileName [ 15 ] = 't';<br />fileName [ 16 ] = 'x';<br />fileName [ 17 ] = 't';<br />fileName [ 18 ] = 0;<br />fgetc ( inputFile );<br />while ( ! feof ( inputFile ) )<br />{<br /><br /><br /><br />ch = fgetc ( inputFile );<br />outputFile = fopen ( fileName , "a" );<br />commaCount = 0;<br />while ( ch != newline && commaCount < 4)<br />{<br />if ( ch == ',' )<br />commaCount ++;<br />else<br />fputc ( ch , outputFile );<br />ch = fgetc ( inputFile );<br />}<br />fputc ( ' ' , outputFile );<br />//flush rest of line<br />while ( ch != newline )<br />ch = fgetc ( inputFile );<br />fclose ( outputFile );<br /><br /><br /><br />fileName [ 12 ] = fgetc ( inputFile );<br />fileName [ 13 ] = fgetc ( inputFile );<br />fileName [ 14 ] = '.';<br />fileName [ 15 ] = 't';<br />fileName [ 16 ] = 'x';<br />fileName [ 17 ] = 't';<br />fileName [ 18 ] = 0;<br />fgetc ( inputFile );<br />}<br /><br /><br />return 0 ;<br />}<br /><br /><br /><br /> - September 3, 2009 at 4:41 pm #3607GarnetHuynhParticipant
I had modified my program with the following code to read each value of column 1 of my input file (*.ref) and create multiple Output files (*.brt) with name as the value at the column 1 of the input file. For each change in the value at column 1 of input file, a new output file (*.brt) should be created. But it doesn’t seem to do what is required and when I run the application it gets hung. Could you please help me with the changes to the following code so that it works :-
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215<br />************************************************************************<br />* Prog Language: VC++<br />* O.S.: Windows<br />* Compiler/Assembler : Visual C++ 6.0<br />* Modifications : Take the output file names (*.brt) from<br />* column 1 of the data of the input file<br />* (*.ref). New output file created for each<br />* change in the value of the first column of<br />* the input file.<br />*************************************************************************/<br /><br />void generate_brt_files(void)<br />{<br /><br />char f_name[MAX_PATH];<br />FILE *ref_file;<br />WIN32_FIND_DATA wfd;<br />HANDLE find_handle;<br />int seen_td_count = 0,<br />cnt;<br />char ch;<br />char newline = 10;<br />int commaCount;<br /><br />char this_td[3];<br /><br /><br />if (gen_brt_files)<br />{<br />if (!berth_step_dir[0])<br />{<br />MessageBox(main_handle, "No location has been set<br />for the berth stepping files.", "Error,,,",<br />MB_ICONERROR | MB_OK);<br />return;<br />}<br /><br />memset(&wfd, 0, sizeof(WIN32_FIND_DATA));<br /><br /><br />wsprintf(f_name, "%sallsmf*.ref",<br />berth_step_dir);<br /><br />if ((find_handle = FindFirstFile(f_name, &wfd)) == INVALID_HANDLE_VALUE)<br />{<br />MessageBox(main_handle, "No SMART berth<br />stepping files found.", "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 /><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, "rb"))<br /><br />{<br /><br />//Take the output file names (*.brt) from<br />// column 1 of the data of the input file<br />// (*.ref). New output file created for each<br />// change in the value of the first column of<br />// the input file.<br /><br />//memcpy(f_name,<br />// wfd.cFileName,2);<br />//this_td[2] = 0;<br /><br /><br />f_name[12] = fgetc(ref_file);<br />f_name[13] = fgetc(ref_file);<br />f_name[14] = '.';<br />f_name[15] = 'r';<br />f_name[16] = 'e';<br />f_name[17] = 'f';<br />f_name[18] = 0;<br /><br />fgetc(ref_file);<br /><br /><br />while(!feof(ref_file))<br /><br />{<br />ch = fgetc(ref_file);<br />//brt_file = fopen(f_name, "a");<br />commaCount = 0;<br />}<br /><br />while(ch != newline && commaCount < 4)<br />{<br />if (ch == ',')<br />{<br />commaCount ++;<br />}<br /><br />else<br />{<br />//fputc(ch,brt_file);<br />ch = fgetc(ref_file);<br />}<br />}<br /><br /><br />//fputc(' ',brt_file);<br /><br />//flush rest of line<br />while(ch != newline )<br />{<br />ch = fgetc(ref_file);<br />}<br /><br />//fclose(brt_file);<br /><br />//f_name[12]=fgetc(brt_file);<br />//f_name[13]=fgetc(brt_file);<br />//f_name[14]= '.';<br />//f_name[15]='b';<br />//f_name[12]='r';<br />//f_name[12]='t';<br />//f_name[12]=0;<br />//fgetc(brt_file);<br />//}<br /><br /><br />//return 0 ;<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, "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 /><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 /><br />//fwrite(buffer,<br />// 19,<br />// 1,<br />// brt_file);<br /><br /><br />fputc(' ',brt_file);<br /><br />//fputc(ch,brt_file);<br /><br />}<br /><br />fclose(brt_file);<br /><br />}<br /><br />else<br /><br />{<br />MessageBox(main_handle, "Unable to open file for writing", f_name,<br />MB_ICONERROR | MB_OK);<br /><br />return FALSE;<br />}<br /><br />//return TRUE;<br />//}<br /><br />f_name[12]=fgetc(brt_file);<br />f_name[13]=fgetc(brt_file);<br />f_name[14]= '.';<br />f_name[15]='b';<br />f_name[16]='r';<br />f_name[17]='t';<br />f_name[18]=0;<br />fgetc(brt_file);<br />//}<br /><br />return 0 ;<br /><br />}<br /><br />Thanks a lot,
Marconi.
- AuthorPosts
- The forum ‘C Programming’ is closed to new topics and replies.