Jump to content

Trouble with text file reading and writing

- - - - -

  • Please log in to reply
4 replies to this topic

#1
Alan Cartier

Alan Cartier

    Newbie

  • Members
  • Pip
  • 3 posts
I wrote a program to sort through 300,000 lines of formatted data in a text file to try to save some time. I am having a few problems.

It's either locking up or getting into an infinite loop I cant figure out. When I put it through GDB and did backtrace it showed this

0 ... in __kernel_vsyscall ()
1 ... in read () from /lib/libc.do.6
2 ... in _IO_file_underflow () from /lib/libc.so.6
3 ... in _IO_default_uflow () from /lib/libc.so.6
4 ... in __uflow () from /lib/libc.so.6
5 ... in _IO_getline_info () from /lib/libc.so.6
6 ... in _IO_getline () from /lib/libc.so.6
7 ... in fgets () from /lib/libc.so.6
8 ... in find_subrec

Any help would be greatly appreciated. I'll post some of the code if that would help but it is almost 400 lines. Thanks in advance.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
Umm, it's obviously in an infinite loop because of your while(true), no wait, it's just taking a while because you're using an O(n^3) algorithm, no wait.... perhaps some code would help.

In all seriousness, black-box debugging never works well.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Alan Cartier

Alan Cartier

    Newbie

  • Members
  • Pip
  • 3 posts
Sorry here is the function the problem is occurring in.


void find_subrec(FILE* ifp, FILE* ofp, char* line)

{

  char* subrec = "STARTSUBRECORD: IVSUBREC";

  char* partnum = "PARTNUMB:";

  char* temp = malloc_array(STR);


  fgets( temp, 50, ifp);

  

  while( (strncmp( temp, subrec, 24) != 0) && (strncmp( temp, partnum, 9) != 0 ) )

    fgets( temp, 50, ifp);


  while( (strncmp( temp, subrec, 24) != 0) && (strncmp( temp, partnum, 9) == 0 ) )

  {

    fprintf( ofp, "%s\n", line);

    printf("line to file\n");


    free(temp);


    line = clear_array(line, LINE);

   

    return;

  }

  free(temp);

  

  sort_vendor(ifp, ofp, line);

  

  return;

}


and here is the function that is leading into it,


void sort_mfr(FILE* ifp, FILE* ofp, char* line)

{

  char* subrec = "STARTSUBRECORD: IVSUBREC";

  char* new = "NEWRECORD";

  char* mfr = "MFR:";

  char* temp = malloc_array(STR);

  size_t x = 0;


  fscanf(ifp, "%49s", temp);


  while( (strncmp(temp, mfr, 4) != 0) && (strncmp(temp, subrec, 24) != 0) && (strncmp(temp, new, 9) != 0) ) 

    fscanf(ifp, "%49s", temp);


  if( (strncmp(temp, mfr, 4) != 0) &&  (strncmp(temp, subrec, 24) == 0) && (strncmp(temp, new, 9) != 0) ) 

  {

    sprintf( line, "MFR:\t null\t\t");

    fprintf( ofp, "%s", line);

    printf("line to file\n");


    line = clear_array(line, LINE);


    sprintf( line, "\t\t\t\t");


    free(temp);


    sort_vendor(ifp, ofp, line);


    return;

  }


  if( (strncmp(temp, mfr, 4) != 0) &&  (strncmp(temp, subrec, 24) != 0) && (strncmp(temp, new, 9) == 0) ) 

  {

    sprintf( line, "MFR:\t null\t\t");

    fprintf( ofp, "%s", line);

    printf("line to file\n");


    line = clear_array(line, LINE);


    free(temp);

   

    return;

  }

  fscanf(ifp, "%49s", temp);

  x = strlen(temp);

  sprintf( line, "MFR:\t "); 

  snprintf( line, x, "%s ", temp); 


  find_newline( ifp, ofp, line );


  free(temp);

  

  find_subrec(ifp, ofp, line);

  

  return;

}



#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
I don't see the code for sort_vendor, which could be causing the problem, nor do I see where sort_mfr is called. Complete, compilable code would really help.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Alan Cartier

Alan Cartier

    Newbie

  • Members
  • Pip
  • 3 posts
Sorry its rather long which is why i didn't post it at first


#include<stdio.h>

#include<stdlib.h>

#include<stddef.h>

#include<string.h>

#define LINE (1000)

#define STR (51)


void sort_partnumb(FILE* ifp, FILE* ofp, char* line);

void find_subrec(FILE* ifp, FILE* ofp, char* line);

void sort_vendor(FILE* ifp, FILE*ofp, char* line);

void sort_mfrpart(FILE* ifp, FILE* ofp, char* line);

void sort_lead(FILE* ifp, FILE* ofp, char* line);

void sort_unit(FILE* ifp, FILE* ofp, char* line);

void sort_price(FILE* ifp, FILE* ofp, char* line);

void sort_mfr(FILE* ifp, FILE* ofp, char* line);

void find_newline(FILE* ifp, FILE* ofp, char* line);

char* malloc_array (int howmany);

char* clear_array(char* array, int howmany);


int main()

{ /* begin main */

  FILE* ifp = fopen("./ivrectemp.txt", "r");

  

  if ( ifp == NULL )

  {

    printf("error opening iv.txt\n");

    exit(1);

  }

  printf("iv.txt opened for search\n");


  FILE* ofp = fopen("tmp.txt", "w");


  if ( ofp == NULL )

  {

    printf("error opening tmp.txt\n");

    exit(1);

  }

  printf("tmp.txt opened to write\n");


  char* line = malloc_array(LINE); 

  int check;


  check = fgetc(ifp);


  while ( check != EOF )

  {

    sort_partnumb( ifp, ofp, line );

    check = fgetc(ifp);

  }


  free(line);


  fclose(ifp);

  fclose(ofp);

  printf("files closed\n");  


  getchar ();

  return 0;

} /* end of main */


void sort_partnumb(FILE* ifp, FILE* ofp, char* line)

{

  char* partnum = "PARTNUMB:";

  char* temp = malloc_array(STR);

  size_t x;


  fscanf(ifp, "%49s", temp);


  while ( strncmp(temp, partnum, 9) != 0 )

    fscanf(ifp, "%49s", temp);


  fscanf(ifp, "%49s", temp);

  x = strlen(temp);

  printf("x is %d\n", x);

  sprintf( line, "PARTNUMB:\t "); 

  snprintf( line, x, "%s ", temp); 


  find_newline( ifp, ofp, line );


  free(temp);


  find_subrec( ifp, ofp, line );


  return;

}


void find_subrec(FILE* ifp, FILE* ofp, char* line)

{

  char* subrec = "STARTSUBRECORD: IVSUBREC";

  char* partnum = "PARTNUMB:";

  char* temp = malloc_array(STR);


  fgets( temp, 50, ifp);

  

  while( (strncmp( temp, subrec, 24) != 0) && (strncmp( temp, partnum, 9) != 0 ) )

    fgets( temp, 50, ifp);


  while( (strncmp( temp, subrec, 24) != 0) && (strncmp( temp, partnum, 9) == 0 ) )

  {

    fprintf( ofp, "%s\n", line);

    printf("line to file\n");


    printf("%s\n", line);


    free(temp);


    line = clear_array(line, LINE);

   

    return;

  }

  free(temp);

  

  sort_vendor(ifp, ofp, line);

  

  return;

}


void sort_vendor(FILE* ifp, FILE*ofp, char* line)

{

  char* vendor = "VENDOR:";

  char* mfrpart = "MFRPART:";

  char* leadtime = "LEADTIME:";

  char* unit = "PURCUNIT:";

  char* temp = malloc_array(STR);

  size_t x;


  fscanf(ifp, "%49s", temp);


  while ( (strncmp(temp, vendor, 7) != 0) && (strncmp(temp, mfrpart, 8) != 0) 

	  && (strncmp(temp, leadtime, 9) != 0) && (strncmp(temp, unit, 9) != 0) )

    fscanf(ifp, "%49s", temp);


  if ( (strncmp(temp, vendor, 7) != 0) && ( (strncmp(temp, mfrpart, 8) == 0) 

	|| (strncmp(temp, leadtime, 9) == 0) || (strncmp(temp, unit, 9) == 0) ) )

  {

    sprintf( line, "VENODR:\tnull\t\t");


    free(temp);


    sort_mfrpart(ifp, ofp, line);


    return;

  }

  fscanf(ifp, "%49s", temp);

  x = strlen(temp);

  printf("x is %d\n", x);

  sprintf( line, "VENDOR:\t "); 

  snprintf( line, x, "%s ", temp); 


  find_newline( ifp, ofp, line );

 

  free(temp);

  

  sort_mfrpart(ifp, ofp, line);


  return;

}


void sort_mfrpart(FILE* ifp, FILE* ofp, char* line)

{

  char* mfrpart = "MFRPART:";

  char* leadtime = "LEADTIME:";

  char* unit = "PURCUNIT:";

  char* temp = malloc_array(STR);

  size_t x;


  fscanf(ifp, "%49s", temp);


  while( (strncmp(temp, mfrpart, 8) != 0) && (strncmp(temp, leadtime, 9) != 0) && (strncmp(temp, unit, 9) != 0) )

    fscanf(ifp, "%49s", temp);


  if( (strncmp(temp, mfrpart, 8) != 0) && ((strncmp(temp, leadtime, 9) != 0) || (strncmp(temp, unit, 9) != 0) ) )

  {

    sprintf( line, "MFRPART:\tnull\t\t");


    free(temp);


    sort_lead(ifp, ofp, line);


    return;

  }

  fscanf(ifp, "%49s", temp);

  x = strlen(temp);

  printf("x is %d\n", x);

  sprintf( line, "MFRPART:\t "); 

  snprintf( line, x, "%s ", temp); 


  find_newline( ifp, ofp, line );

  

  free(temp);

  

  sort_lead(ifp, ofp, line);


  return;

}


void sort_lead(FILE* ifp, FILE* ofp, char* line)

{

  char* leadtime = "LEADTIME:";

  char* unit = "PURCUNIT:";

  char* temp = malloc_array(STR);

  size_t x;


  fscanf(ifp, "%49s", temp);


  while( (strncmp(temp, leadtime, 9) != 0) && (strncmp(temp, unit, 9) != 0) ) 

    fscanf(ifp, "%49s", temp);


  if( (strncmp(temp, leadtime, 9) != 0) && (strncmp(temp, unit, 9) == 0) ) 

  {

    sprintf( line, "LEADTIME:\t 1\t\t");


    free(temp);


    sort_unit(ifp, ofp, line);


    return;

  }

  fscanf(ifp, "%49s", temp);

  x = strlen(temp);

  printf("x is %d\n", x);

  sprintf( line, "LEADTIME:\t "); 

  snprintf( line, x, "%s ", temp); 


  find_newline( ifp, ofp, line );


  free(temp);

  

  sort_unit(ifp, ofp, line);


  return;

}


void sort_unit(FILE* ifp, FILE* ofp, char* line)

{

  char* unit = "PURCUNIT:";

  char* price = "PURCHPRICE:";

  char* temp = malloc_array(STR);

  size_t x;


  fscanf(ifp, "%49s", temp);


  while( (strncmp(temp, unit, 9) != 0) && (strncmp(temp, price, 11) != 0) )

    fscanf(ifp, "%49s", temp);


  if( (strncmp(temp, unit, 9) != 0) && (strncmp(temp, price, 11) == 0) )

  {

    sprintf( line, "PURCUNIT:\t one\t\t" );


    free(temp);


    sort_price(ifp, ofp, line);


    return;

  }

  fscanf(ifp, "%49s", temp);

  x = strlen(temp);

  printf("x is %d\n", x);

  sprintf( line, "PURCUNIT:\t "); 

  snprintf( line, x, "%s ", temp); 


  find_newline( ifp, ofp, line );

  

  free(temp);

  

  sort_price(ifp, ofp, line);


  return;

}


void sort_price(FILE* ifp, FILE* ofp, char* line)

{

  char* price = "PURCHPRICE:";

  char* mfr = "MFR:";

  char* temp = malloc_array(STR);

  size_t x;


  fscanf(ifp, "%49s", temp);


  while( (strncmp( temp, price, 11) != 0) && (strncmp( temp, mfr, 4) != 0) )

    fscanf(ifp, "%49s", temp);


  if( (strncmp( temp, price, 11) != 0) && (strncmp( temp, mfr, 4) == 0) )

  {

    sprintf( line, "PURCHPRICE:\t null\t\t");


    free(temp);


    sort_mfr(ifp, ofp, line);


    return;

  }

  fscanf(ifp, "%49s", temp);

  x = strlen(temp);

  printf("x is %d\n", x);

  sprintf( line, "PURCHPRICE:\t "); 

  snprintf( line, x, "%s ", temp); 


  find_newline( ifp, ofp, line );


  free(temp);

  

  sort_mfr(ifp, ofp, line);


  return;

}


void sort_mfr(FILE* ifp, FILE* ofp, char* line)

{

  char* subrec = "STARTSUBRECORD: IVSUBREC";

  char* new = "NEWRECORD";

  char* mfr = "MFR:";

  char* temp = malloc_array(STR);

  size_t x;


  fscanf(ifp, "%49s", temp);


  while( (strncmp(temp, mfr, 4) != 0) && (strncmp(temp, subrec, 24) != 0) && (strncmp(temp, new, 9) != 0) ) 

    fscanf(ifp, "%49s", temp);


  if( (strncmp(temp, mfr, 4) != 0) &&  (strncmp(temp, subrec, 24) == 0) && (strncmp(temp, new, 9) != 0) ) 

  {

    sprintf( line, "MFR:\t null\t\t");

    fprintf( ofp, "%s", line);

    printf("line to file\n");


    printf("%s\n", line);


    line = clear_array(line, LINE);


    sprintf( line, "\t\t\t\t");


    free(temp);


    sort_vendor(ifp, ofp, line);


    return;

  }


  if( (strncmp(temp, mfr, 4) != 0) &&  (strncmp(temp, subrec, 24) != 0) && (strncmp(temp, new, 9) == 0) ) 

  {

    sprintf( line, "MFR:\t null\t\t");

    fprintf( ofp, "%s", line);

    printf("line to file\n");


    printf("%s\n", line);


    line = clear_array(line, LINE);


    free(temp);


    return;

  }

  fscanf(ifp, "%49s", temp);

  x = strlen(temp);

  printf("x is %d\n", x);

  sprintf( line, "MFR:\t ");

  snprintf( line, x, "%s ", temp); 


  find_newline( ifp, ofp, line );


  free(temp);

  

  find_subrec(ifp, ofp, line);

  

  return;

}


void find_newline(FILE* ifp, FILE* ofp, char* line)

{

  char c = fgetc(ifp);


  while( c != '\n' )

  {

    sprintf( line, "%c", c);

    c = fgetc(ifp);

  }


  sprintf( line, "\t");

}


char* malloc_array (int howmany)

{

  int i;

  char* data;


  data = malloc ( howmany * sizeof(char) );


  if ( data == 0 )

  {

    printf("Could not allocate %d ints\n", howmany);

    exit (1);

  }


  for ( i = 0; i < howmany; i ++ )

    data[i] = ' ';


  return data;

}


char* clear_array(char* array, int howmany)

{

  int i;


  for ( i = 0; i < howmany; i ++ )

    array[i] = ' ';


  return array;

} 






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users