Thread: crash my code.

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    crash my code.

    I'm tired of looking at this code and trying to figure out why AllocateFile doesn't work. Please let me know if you guys see anything wrong with my code. I have two problems.
    1) in GetFile if a newline is entered as the first input, code crashes. Let me know what i'm not seeing please.

    2)Allocatefile doesn't seem to even be filling up the buffer because i put a test statement in there and nothing prints out.
    Code:
    /*csv dedupper, this program goes through a a csv text file, allocates contents
    to a struct, and provides sevreal functions to work on that struct and then
    prints the output to a new text file. functions inlude: FindDupes, Timer etc...*/
    
    #include<stdio.h>/*for file i/o*/
    #include<string.h>/*for strchr and strncpy*/
    #include <errno.h>
    #define MAX 150
    #define STARTOVER 0
    typedef struct CSV{
    
           char field1[MAX],field2[MAX],field3[MAX],field4[MAX],field5[MAX];
           char field6[MAX],field7[MAX],field8[MAX],field9[MAX],field10[MAX];
           struct CSV *left, *right;/*for linked list later and dynamic allocation*/
           }RECORD;
    
    FILE *GetFile(void);/*opens file for reading and work and returns its addy*/
    int NumOfRecs(FILE *);/*goes throug a given file and counts num of records*/
    int AllocateFile(FILE *,long );/*allocate each contents of file to struct*/
    
    int main(void){
        FILE *filename;/*for getfile return value*/
        long total_records;/*for num of records return value*/
        
        
        
        filename=GetFile();/*retuns the address of a file so can be worked on*/
        total_records=NumOfRecs(filename);/*gives us how many lines in the file*/
        AllocateFile(filename,total_records);/*put all text content to the struct*/
    
       getchar();
       return 0;
    }
    
    /*function definitions */
    FILE *GetFile(void){
         /*first thing to do is get the name of the file to parse and work on*/
       char filename[MAX];
       char *find;
       FILE *fp;
       /*this should be a loop until a proper file name is given*/
       puts("What is the name of the file and its extension");
       
     /*should also get additional info like how many fields are in the file*/
     while( (fgets(filename,MAX,stdin)) !=NULL && filename[0]!='\n'){
            if( (find=strchr(filename,'\n'))!=NULL)
                *find='\0';
       
       if ( (fp=fopen(filename,"r")) == NULL){
            perror("error:");
            puts("enter file name or newline to quit:");
            }
            
         else{
          puts("file opened");
          return fp;
         } 
     
    }/*end of while, return null since unable to open file */
     return NULL;       
    /*this crashes if a users presses a '\n' why? trace program state*/
    }
    
    int NumOfRecs(FILE *thefile){
    /*the file is already opened and its addy is passed as a parameter to each funtion
    so just work on it*/
      long records=1;/*i think it starts counting at zero so add 1 for the first line*/
      int character;/*what character looking at*/
      while( (character = fgetc(thefile)) !=EOF){
             if ( (character == '\n'))
                  records+=1; 
                  }
                  
      fprintf(stdout,"num of records in file is %ld\n",records);
      return records;
    }
             
    
    int AllocateFile(FILE *filename,long num_of_records){
    /*iterates through file contents and adds each character to the appropiate field*/
     
        int ndx=0;
        int field=0;/*ndx for field ndx*/
        size_t word_length=0;
        int character;
        int comma_count=0;
        char buffer[BUFSIZ];/*really holding integers should put it to char*/
        RECORD person[num_of_records];/*array of records named person to hold each line in file*/
       
    /*go through file and puts contents in the array*/
    while ( (character=fgetc(filename)) !=EOF){
             buffer[ndx++]=character; /*test*/putchar(character);
             word_length+=1;
              if ( (buffer[ndx] ==',') ){
                buffer[ndx]='\0';/*terminate and make buffer a string*/
                comma_count+=1;/*update comma count so know where we are in file*/
                switch(comma_count){
                  case 1:
                  strncpy(person[field].field1,buffer,word_length);
                  ndx=0 ;/*buffer ndx goes to zero*/
                  word_length=0;
                  break;
                }
              }
      
       }
    }
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Code:
    if ( (buffer[ndx] ==',') ){
    You've already incremented ndx, so this comparison wont work. Maybe you meant to do:
    Code:
    if ( (character ==',') ){

  3. #3
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    The NumOfRecs function read thru the file putting the file pointer at the end of the file. In AllocateFile rewind the file using fseek and then read the file...

  4. #4
    Registered User
    Join Date
    Mar 2004
    Posts
    536
    Quote Originally Posted by caroundw5h
    Please let me know if you guys see anything wrong with my code.
    1) in GetFile if a newline is entered as the first input, code crashes.
    Code:
        
        filename=GetFile();/*retuns the address of a file so can be worked on*/
        total_records=NumOfRecs(filename);/*gives us how many lines in the file*/
    What value does GetFile return if you enter '\n' for the file name?

    What happens in NumOfRecs when you call it with the argument equal to this value?

    D

  5. #5
    Information Crocodile
    Join Date
    Dec 2004
    Posts
    204
    It returns NULL and even if it returns NULL it will still try to open the file that doesnt exist.


    The main() should be..
    Code:
    int main(void){
        FILE *filename;/*for getfile return value*/
        long total_records;/*for num of records return value*/
        
        
        
       if( ( filename=GetFile() ) != NULL ) {/*retuns the address of a file so can be worked on*/
    
           total_records=NumOfRecs(filename);/*gives us how many lines in the file*/
           AllocateFile(filename,total_records);/*put all text content to the struct*/
      }
      else {
          printf( "ERROR: Null Pointer for File\n" );
      }
       getchar();
       return 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2005
    Location
    Mountaintop, Pa
    Posts
    1,058
    1) in GetFile if a newline is entered as the first input, code crashes. Let me know what i'm not seeing please.
    Your Getfile was a little confusing to me. Maybe it should look something like the following:

    Code:
    FILE *GetFile(void)
    {
        FILE *fp;
        char filename[MAX] = {0};
        /* Get data from the keyboard.  */
        puts("What is the name of the file and its extension\n");
        printf("please enter text => ");
        while( (fgets(filename,MAX,stdin)) &&  filename[0]!='\n')
        {
            if(filename[strlen(filename)-1] == '\n')
                filename[strlen(filename)-1] = '\0';
            if ( (fp=fopen(filename,"r")) == NULL){
                perror("error:");
                puts("enter file name or newline to quit:");
            }
            else
            {
                puts("file opened");
                return fp;
            } 
        }
        return NULL;
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Well if you're all going to fuss so much over checking for just '\n' being entered, you might as well do it up proper, and check for a string of just whitespace.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    bob,dave,bithub and quazah thank you all very much for pointing out those glaring mistakes. I've been away from coding or even reading C code for the whole summer here, so i'm a little rusty. Thanks again. I tackle it again.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Updated sound engine code
    By VirtualAce in forum Game Programming
    Replies: 8
    Last Post: 11-18-2004, 12:38 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM