Home › Forums › C Programming › Read 1st col vals of i/pfile;create o/p fl for each diff val › Re: Re: Read 1st col vals of i/pfile;create o/p fl for each diff val
September 3, 2009 at 4:41 pm
#3607
GarnetHuynh
Participant
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 :-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | <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.