Home Forums C Programming Read 1st col vals of i/pfile;create o/p fl for each diff val

Viewing 2 reply threads
  • Author
    Posts
    • #2225
      GarnetHuynh
      Participant

      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 :-

      // 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.

      #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.

    • #3606
      GWILouisaxwzkla
      Participant

      Heres some code that does what you want ( maybe you can splice this into your program ):



      /****************************************************************
      * File Name : c:programstempCG.cpp
      * Date : August,10,2009
      * Comments : new project
      * Compiler/Assembler : Visual C++ 6.0
      * Modifications :
      *
      *
      *
      *
      *
      * Program Shell Generated At: 3:17:50 p.m.
      =-=-=-=-=-=-=-=--=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=*/



      #include < stdio.h >
      //#include < conio.h >
      //#include < math.h >
      //#include < iomanip >
      //#include < ctype.h >

      //using namespace std;

      //main function ******************************

      int main ( )
      {

      //CHANGE FILE NAMES TO WHAT YOU WANT HERE !!!!!!!!!!!!!!!!!!!!!

      FILE * inputFile = fopen ( "c:\programs\data.txt", "r+" );

      FILE * outputFile;


      //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

      char ch;
      char newline = 10;
      int commaCount;


      char fileName [ 50 ] ;
      strcpy ( fileName , "c:\programs\" );


      fileName [ 12 ] = fgetc ( inputFile );
      fileName [ 13 ] = fgetc ( inputFile );
      fileName [ 14 ] = '.';
      fileName [ 15 ] = 't';
      fileName [ 16 ] = 'x';
      fileName [ 17 ] = 't';
      fileName [ 18 ] = 0;
      fgetc ( inputFile );
      while ( ! feof ( inputFile ) )
      {



      ch = fgetc ( inputFile );
      outputFile = fopen ( fileName , "a" );
      commaCount = 0;
      while ( ch != newline && commaCount < 4)
      {
      if ( ch == ',' )
      commaCount ++;
      else
      fputc ( ch , outputFile );
      ch = fgetc ( inputFile );
      }
      fputc ( ' ' , outputFile );
      //flush rest of line
      while ( ch != newline )
      ch = fgetc ( inputFile );
      fclose ( outputFile );



      fileName [ 12 ] = fgetc ( inputFile );
      fileName [ 13 ] = fgetc ( inputFile );
      fileName [ 14 ] = '.';
      fileName [ 15 ] = 't';
      fileName [ 16 ] = 'x';
      fileName [ 17 ] = 't';
      fileName [ 18 ] = 0;
      fgetc ( inputFile );
      }


      return 0 ;
      }



    • #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 :-


      ************************************************************************
      * Prog Language: VC++
      * O.S.: Windows
      * Compiler/Assembler : Visual C++ 6.0
      * Modifications : 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.
      *************************************************************************/

      void generate_brt_files(void)
      {

      char f_name[MAX_PATH];
      FILE *ref_file;
      WIN32_FIND_DATA wfd;
      HANDLE find_handle;
      int seen_td_count = 0,
      cnt;
      char ch;
      char newline = 10;
      int commaCount;

      char this_td[3];


      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));


      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 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.

      //memcpy(f_name,
      // wfd.cFileName,2);
      //this_td[2] = 0;


      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 ;



      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);


      fputc(' ',brt_file);

      //fputc(ch,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;
      //}

      f_name[12]=fgetc(brt_file);
      f_name[13]=fgetc(brt_file);
      f_name[14]= '.';
      f_name[15]='b';
      f_name[16]='r';
      f_name[17]='t';
      f_name[18]=0;
      fgetc(brt_file);
      //}

      return 0 ;

      }

      Thanks a lot,
      Marconi.

Viewing 2 reply threads
  • The forum ‘C Programming’ is closed to new topics and replies.