Read 1st col values of i/pfile and create o/p files for each diff val

marconi_07's Avatar, Join Date: Aug 2009
Light Poster
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 B1783157848231

But 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 B0416041089375

BN.brt
--------
B0135009465625 B0338033765824 B0334033965824

CA.brt
--------
T SREC47221 T X17147221 T X17347221

CC.brt
--------
B1783157448231 B1783157648231 B1783157848231
and so on ....

The logic for taking the output filename name from character 1 of the input filename is as below :-

Code:
// Take the brt file name from character 1 of the input filename.
              memcpy(this_td,
                           wfd.cFileName,
                           1);
                           this_td[2] = 0;
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.

Code:
#include "p2geog.h"
#define max_smf_lines   1000 * td_max
 
// Reformat the structure of smf_file_layout 
struct smf_file_layout
{
 char      line_type[2];
 char      step_type;
 char      from_berth[4];
 char      to_berth[4];
 char      stanox[5];
 char      event_type;  
 char      thru_line;
 char      platform_id;
 int       event_time_offset;
 char      forward_line;
 char      forward_route;
 unsigned long    sort_key;
};
 
struct tds_from_smf
{
 char      td_id[2];
 int       sfl_ind;
 int       sfl_rec_count;
};
struct smf_file_layout   sfl[max_smf_lines];
struct tds_from_smf    tds_ive_seen[td_max];
int        sfl_count;
extern HWND      main_handle;
extern char      berth_step_dir[],
        td_maps_dir[];
extern BOOL      gen_brt_files;
extern struct td_header_tree tdh[td_max];
extern struct td_map_tree  tdm[td_map_max];
extern struct adjacent_maps  *am[];
extern int      map_count,
        adj_map_count,
        td_header_count;
extern FILE      *file_handle;
 
void generate_brt_files(void)
{
 
 char      f_name[MAX_PATH],
        this_td[3];
//        buffer[200];
 FILE      *ref_file;
 WIN32_FIND_DATA    wfd;
 HANDLE      find_handle;
 int       seen_td_count = 0,
        cnt;
//        ptr,
//        knt;
 
 if (gen_brt_files)
 {
  if (!berth_step_dir[0])
  {
   MessageBox(main_handle,
        "No location has been set for the berth stepping files.",
        "Error,,,",
        MB_ICONERROR | MB_OK);
   return;
  }
 
  memset(&wfd,
      0,
      sizeof(WIN32_FIND_DATA));
 
  // Change the input file type format. 
  wsprintf(f_name,
     "%sallsmf*.ref",
     berth_step_dir);
  if ((find_handle = FindFirstFile(f_name,
           &wfd)) == INVALID_HANDLE_VALUE)
  {
   MessageBox(main_handle,
        "No SMART berth stepping files found.",
        "Error...",
        MB_ICONERROR | MB_OK);
   return;
  }
  sfl_count = 0;
  memset(tds_ive_seen,
      0,
      sizeof(tds_ive_seen));
  memset(sfl,
      0,
      sizeof(sfl));
  while (1)
  {
   wsprintf(f_name,
      "%s%s",
      berth_step_dir,
      wfd.cFileName);
   if (ref_file = fopen(f_name,
         "rb"))
   {
 
       // Take the brt file name from character 1 of the input filename.
    memcpy(this_td,
        wfd.cFileName,
        1);
    this_td[2] = 0;
 
    for (cnt = 0; cnt < seen_td_count; cnt++)
    {
     if (!memicmp(&this_td,
         &tds_ive_seen[cnt].td_id,
         2))
      break;
    }
    if (cnt >= seen_td_count)
    {
 
     // only process the file if we haven't seen this TD already
     memcpy(&tds_ive_seen[seen_td_count].td_id,
         this_td,
         2);
     tds_ive_seen[seen_td_count].sfl_ind = sfl_count;
     read_smf_file(ref_file);
     tds_ive_seen[seen_td_count].sfl_rec_count = sfl_count - 
tds_ive_seen[seen_td_count].sfl_ind;
     if (gen_brt_files)
      write_brt_file(seen_td_count);
     seen_td_count++;
    }
    fclose(ref_file);
   }
   if (!FindNextFile(find_handle,
         &wfd))
    break;
  }
  FindClose(find_handle);
 } // end if (gen_brt_files)
 else
 {
  gen_brt_files = FALSE;
  wsprintf(f_name,
     "%std_ind.tmp",
     td_maps_dir);
  save_file(f_name);
  do_td_index();
  wsprintf(f_name,
     "%std_ind.new",
     td_maps_dir);
  file_handle = fopen(f_name,
       "rb");
  process_td_index();
  draw_tree();
 }
}
 
BOOL read_smf_file(FILE   *file)
{
 char      buffer[100],
        *ptr,
        *mem;
 unsigned int    knt;
 
 while (!feof(file))
 {
  memset(&buffer,
      0,
      sizeof(buffer));
  fgets(buffer,
     sizeof(buffer),
     file);
 
  memset(&sfl[sfl_count],
      ' ',
      sizeof(struct smf_file_layout));
 
  // Remove the stanox_int 
   sfl[sfl_count].event_time_offset =
   sfl[sfl_count].sort_key = 0;
  for (knt = 0; knt < strlen(buffer); knt++)
  {
 
   if (buffer[knt + 1] == 13 ||
    buffer[knt + 1] == 10)
    break;
   if ((buffer[knt] == ',') &&
    (buffer[knt + 1] == ','))
   {
    memmove(&buffer[knt + 2],
      &buffer[knt + 1],
      strlen(buffer) - knt);
    buffer[knt + 1] = ' ';
   }
  }
  ptr = buffer;
  if ((mem = strtok(ptr,
        ",")) && (mem[0] > ' '))
   memcpy(sfl[sfl_count].line_type,
       mem,
       2);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].step_type,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].from_berth,
       mem,
       4);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(sfl[sfl_count].to_berth,
       mem,
       4);
 
                // Remove the trust_event, rttm_event, stanox_int,
  // tiploc and direction
 
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
  {
   memcpy(sfl[sfl_count].stanox,
       mem,
       5);
  }
 
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].event_type,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].thru_line,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].platform_id,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   sfl[sfl_count].event_time_offset = atoi(mem);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].forward_line,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   memcpy(&sfl[sfl_count].forward_route,
       mem,
       1);
  if ((mem = strtok(NULL,
        ",")) && (mem[0] > ' '))
   sfl[sfl_count].sort_key = atoi(mem);
  sfl_count++;
 }
 return TRUE;
}
 
BOOL write_brt_file(int   tis_ind)
{
 FILE      *brt_file;
 char      f_name[MAX_PATH],
        buffer[100];
 int       cnt,
        last_rec;
 
 wsprintf(f_name,
    "%s%.2s.brt",
    berth_step_dir,
    tds_ive_seen[tis_ind].td_id);
 if (brt_file = fopen(f_name,
       "wb"))
 {
  last_rec = tds_ive_seen[tis_ind].sfl_rec_count + tds_ive_seen[tis_ind].sfl_ind;
  for (cnt = tds_ive_seen[tis_ind].sfl_ind; cnt < last_rec; cnt++)
  {
   wsprintf(buffer,
      "%c%.4s%.4s%.5s     ",
      sfl[cnt].step_type,
      sfl[cnt].from_berth,
      sfl[cnt].to_berth,
      sfl[cnt].stanox);
   fwrite(buffer,
       19,
       1,
       brt_file);
  }
  fclose(brt_file);
 }
 else
 {
  MessageBox(main_handle,
       "Unable to open file for writing",
       f_name,
       MB_ICONERROR | MB_OK);
  return FALSE;
 }
 return TRUE;
}
Thanks a lot.
Marconi.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Seems to be a duplicate of another thread
http://www.go4expert.com/showthread.php?t=19094

Why doesn't my response in that thread help you?
Are you simply looking for someone to write the code for you or do you want help solving the problem yourself?
0
marconi_07's Avatar, Join Date: Aug 2009
Light Poster
Hi,

Please help me with the code.

Thanks,
Marconi.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
I already have done. Try responding to the questions asked then I may be able to help further.
0
naimish's Avatar
Banned
Check this link : http://www.go4expert.com/showthread.php?t=19094
0
SaswatPadhi's Avatar, Join Date: May 2009
~ Б0ЯИ Τ0 С0δЭ ~
Duplicate of http://www.go4expert.com/showthread.php?t=19094.

As xpi0t0s has already posted a response to the other thread, I AM CLOSING THIS THREAD.

@ marconi_07 :: Don't open duplicate threads and try to respond to existing threads.
0
marconi_07's Avatar, Join Date: Aug 2009
Light Poster
Hi,

I modified my above 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 work. Could you please help me with the changes to the following code so that it works. I already seem to have messed up on this Trivial change :-

Code:
// Take the output file names (*.brt) from column 1 of the data of the input file (*.ref). 
// New output file created for each change in the value of the first column of the 
// input file.       
 
                f_name[12] = fgetc(ref_file);
                f_name[13] = fgetc(ref_file);
                f_name[14] = '.';
                f_name[15] = 'r';
                f_name[16] = 'e';
                f_name[17] = 'f';
                f_name[18] = 0;
                fgetc(ref_file);
                while(!feof(ref_file))
 
    {
            ch = fgetc(ref_file);
               // brt_file = fopen(f_name, "a");
                  commaCount = 0;
    }
   while(ch != newline && commaCount < 4) 
         {
              if (ch == ',')
    {
                 commaCount ++;
    }
              else
     {
                  //fputc(ch,brt_file);
                  ch = fgetc(ref_file);
     } 
         }
 
          //fputc(' ',brt_file);
       //flush rest of line
         while(ch != newline )
   {
            ch = fgetc(ref_file);
   }
         //fclose(brt_file); 
 
       //f_name[12]=fgetc(brt_file);
       //f_name[13]=fgetc(brt_file);
       //f_name[14]= '.';
       //f_name[15]='b';
       //f_name[12]='r';
       //f_name[12]='t';
       //f_name[12]=0;
       //fgetc(brt_file);
    //}
 
    //return 0 ;

Thanks a lot.
Marconi.